"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