Hibernate on one-to-many, multi-pair, two-way association mappings

Source: Internet
Author: User



"Hibernate" on one-to-many, multi-pair, bidirectional Association mappings


Because one-to-many, and many-to-two bidirectional association mappings are basically the same, so write it down together!

Annotations Configuration

@Entity @table (name= "T_group") publicclass Group {    private Integer ID;    private String name;    Private set<person> persons=newhashset<person> ();//set does not allow duplication, best suited for database model    @Id    @GeneratedValue Public    Integer getId () {       returnid;    }    Publicvoid setId (integerid) {       this.id = ID;    }    @Column (name= "T_name") public    String GetName () {       returnname;    }    Publicvoid SetName (stringname) {       this.name = name;    }    @OneToMany (mappedby= "group")//consciousness is to tell Hibernate that the association relationship should be based on person, more than one side//  only Onetoone,onetomany, Mappedby attribute is available on Manytomany, Manytoone does not exist; public    set<person>getpersons () {       returnpersons;    }    Publicvoidsetpersons (set<person> persons) {       this.persons = persons;    }}

@Entity @table (name= "T_person") publicclass person {    private Integer ID;    private String name;    Private Integer age;    Private group group;         @ManyToOne public    Group Getgroup () {       returngroup;    }    Publicvoid Setgroup (groupgroup) {       this.group = group;    }    @Id    @GeneratedValue public    Integer getId () {       returnid;    }    Publicvoid setId (integerid) {       this.id = ID;    }    @Column (name= "P_name") public    String GetName () {       returnname;    }    Publicvoid SetName (stringname) {       this.name = name;    }    @Column (name= "p_age") public    Integer Getage () {       returnage;    }    Publicvoid Setage (integerage) {       this.age = age;    }}

XML configuration


<?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 ">

Can everyone get a question?

Why are bidirectional associationsGroupinKeyneed to specifycolumnand in PersonYou also need to specify thecolumnit?

What happens if one of these is not specified?

if the specified column What will happen if there is inconsistency?

Everyone can try!





Hibernate on one-to-many, multi-pair, two-way 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.