Mohamed Saligh
Mohamed Saligh

Reputation: 12339

How annotation mapping is done in java persistence?

We use annotations for mapping the entity class with the database table by simply specifying @Entity and more like @Id, table joins and many things. I do not know how these entity variables are getting mapped with database table. Can anyone give a short description for understanding.

Thanks :)

Upvotes: 1

Views: 1912

Answers (4)

Ravi Parmar
Ravi Parmar

Reputation: 1452

in your case annotation working means mapping with tablename with entity class is look like as ....


@Entity

@Table(name = "CompanyUser")

public class CompanyUserCAB implements java.io.Serializable

    {
    private long    companyUserID;

    private int     companyID;

        @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "companyUserID")
    public long getCompanyUserID()
    {
        return this.companyUserID;
    }
    public void setCompanyUserID(long companyUserID)
    {
        this.companyUserID = companyUserID;
    }

    @Column(name = "companyID")
    public int getCompanyID()
    {
        return this.companyID;
    }
    public void setCompanyID(int companyID)
    {
        this.companyID = companyID;
    }
}

Upvotes: 0

Daff
Daff

Reputation: 44205

Well the idea is to translate your objects and their connections with other objects into a relational database. These two ways of representing data (objects defined by classes and in tables in a database) are not directly compatible and that is where a so called Object Relational Mapper framework comes into play. So a class like

class MyObject
{
    private String name;
    private int age;
    private String password;

    // Getters and setters
}

Will translate into a database table containing a column name which is of type varchar, age of type int and password of type varchar.

Annotations in Java simply add additional information (so called meta data) to your class definitions, which can be read by any other class (e.g. JavaDoc) and in the case of the Java Persistence API will be used by an ORM framework like Hibernate to read additional information you need to translate your object into the database (your database table needs a primary id and some information - like what type of a relation an object has to another - can't be automatically determined by just looking at your class definition).

Upvotes: 3

user364939
user364939

Reputation: 233

annotations are just metadata on a class, nothing magical. You can write your own annotations. Those annotations are given retention policies of runtime (which means you have access to that metadata at runtime). When you call persist etc the persistence provider iterates through the fields (java.lang.reflect.Field) in your class and checks what annotations are present to build up your SQL statement. Try writing your own annotation and doing something with it. It won't seem very magical after that.

Upvotes: 0

Costis Aivalis
Costis Aivalis

Reputation: 13728

Annotations are very well explained here:
http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/

Upvotes: 0

Related Questions