Hibernate about one-way, bidirectional Association mappings

Source: Internet
Author: User


"Hibernate" About one-way, bidirectional Association mappings


First we look at Hibernate's official explanation for one-to-one unidirectional association:


The one-way and one-way associations based on the foreign Key association are almost the same as the unidirectional many-to-one association. The only difference is a one-way off.

The Foreign key field in the union has a uniqueness constraint.


OK, then we can also understand that one-to-many is a special form of association, we know that hibernate is configured in two ways, namely, Annotations,XML two configuration mode!


Annotations One-way Association mappings


Person (User) and * * * (Card)

@Entity @table (name= "T_user") publicclass user {    private integer id;     private String name;    private Card card;          @OneToOne      @JoinColumn (name= "card_id", Unique=true)//name is the column name of the custom Association foreign Key     public card getcard ()  {        returncard;    }    publicvoid setcard ( Card card)  {       this.card = card;     }     @Id      @GeneratedValue     public  Integer getid ()  {       returnid;    }     publicvoid setid (Integerid)  {       this.id  = id;    }         @Column (name= "name")      Public string getname ()  {       returnname;     }    publicvoid setname (Stringname)  {        this.name = name;    }
 @Entity @table (name= "T_card") publicclass card {     private Integer id;    private String num;          @Id      @GeneratedValue     public  integer getid ()  {       returnid;    }     publicvoid setid (Integerid)  {        this.id = id;    }     @Column (name= "card_id")      public string getnum ()  {       returnnum;     }    publicvoid setnum (Stringnum)  {        this.num = num;    }} 


XML One-Way Association

Person and the Group , a person can only be in a group

publicclass group {    private  integer id;    private string name;    public  Integer getid ()  {       returnid;    }     publicvoid setid (Integerid)  {       this.id  = id;    }    public string getname ()  {        returnname;    }     Publicvoid setname (Stringname)  {       this.name = name ;     }} 
publicclass person {    private integer id;     private string name;    private integer age;     Private group group;    public group getgroup ()  {        returngroup;    }    publicvoid  Setgroup (Groupgroup)  {       this.group = group;     }    public integer getid ()  {        returnid;    }    publicvoid setid (Integerid)  {        this.id = id;    }     public string getname ()  {       return name;     } &nbSp;  public void setname (Stringname)  {        This.name = name;    }    public integer getage ()  {       return age;    }     publicvoid setage (integer age) {       this.age =  age;    }}
<?xml version= "1.0"? ><! doctype hibernate-mapping public      "-//hibernate/hibernate mapping  dtd 3.0//en "    " HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-MAPPING-3.0.DTD " >
<?xml version= "1.0"? ><! doctype hibernate-mapping public      "-//hibernate/hibernate mapping  dtd 3.0//en "    " HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-MAPPING-3.0.DTD " >

Ok, here, everyone is a little puzzled, why our annotaions configuration is one-to-one and our XML configuration is Many-to-one?

Of course, the XML configuration is my reference to hibernate's documentation, because one on the other is a multi-pair special form,

But configuration Many-to-one gives us a hint that multiple numbers correspond to one person, so hibernate provides unique uniqueness to confirm this situation!


Annotations one-to-one bidirectional correlation mapping


User and the Card

@Entity @table (name= "T_user") publicclass user {    private integer id;     private String name;    private Card card;      @OneToOne      @JoinColumn (name= "card_id", unique=true)// Name is primarily a custom association foreign key Column name     public card getcard ()  {        returncard;    }    publicvoid setcard (Cardcard)  {       this.card = card;    }      @Id      @GeneratedValue     public Integer  GetId ()  {       returnid;    }     publicvoid setid (Integerid)  {       this.id = id;     }     @Column (name= "name")     public string getname ()  {        returnname;    }    publicvoid  SetName (Stringname)  {       this.name = name;     }}
@Entity @table (name= "T_card") publicclass card {    private integer id;     private String num;    private User user;          @OneToOne (mappedby= "card")//mappedby means to specify the card in user as the associated foreign key. Otherwise the User and card will appear foreign Key     public user getuser ()  {        returnuser;    }    publicvoid setuser (Useruser)  {       this.user = user;    }      @Id      @GeneratedValue     public Integer  GetId ()  {       returnid;    }     publicvoid setid (Integerid)  {       this.id = id;     }     @Column (name= "card_id")     public string getnum ()  {        returnnum;    }    publicvoid  setnum (Stringnum)  {       this.num = num;     }}


Ok? So we can see that the configuration in Annotaions is Ont-to-one, so what are our configurations in XML?

<?xml version= "1.0"? ><! doctype hibernate-mapping public      "-//hibernate/hibernate mapping  dtd 3.0//en "    " HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-MAPPING-3.0.DTD " >
<?xml version= "1.0"? ><! doctype hibernate-mapping public      "-//hibernate/hibernate mapping  dtd 3.0//en "    " HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-MAPPING-3.0.DTD " >


OK, to here a one-way correlation mapping is basically the case, essays, there are problems, please leave a message!






This article from "Promise always attached to the small wood 、、、" blog, please be sure to keep this source http://1936625305.blog.51cto.com/6410597/1567291

Hibernate about one-way, bidirectional Association mappings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.