开发者

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 an开发者_JAVA技巧yone give a short description for understanding.

Thanks :)


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).


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


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.


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜