Hibernate Association configuration-bidirectional one-to-multiple/multiple-to-one ing configuration based on the connection table

Source: Internet
Author: User

entity:

Package bi. one2workflow. jointable; import Java. util. hashset; import Java. util. set; public class Emperor {private int ID; private string name; private set <Minister> Ministers = new hashset <Minister> (); Public Emperor () {} public Emperor (int id, string name, set <Minister> ministers) {super (); this. id = ID; this. name = Name; this. ministers = ministers;} public int GETID () {return ID;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public set <Minister> getministers () {return ministers;} public void setministers (set <Minister> ministers) {This. ministers = ministers;} package bi. one2workflow. jointable; Public Class Minister {private int ID; private string name; private Emperor; Public Minister () {} public minister (int id, string name, Emperor) {super (); this. id = ID; this. name = Name; this. emperor = emperor;} public int GETID () {return ID;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public Emperor getemperor () {return emperor;} public void setemperor (Emperor) {This. emperor = emperor ;}}

HBM configuration file:

<Hibernate-mapping> <class name = "bi. one2workflow. jointable. emperor "> <ID name =" ID "column =" Eid "> <generator class =" native "/> </ID> <property name =" name "type =" String "column =" name "> </property> <! -- Confirure the uniredirection Association --> <set name = "Ministers" table = "emperorminister" inverse = "true"> <key column = "Eid" not-null = "true"> </key> <subtitle-to-duplicate column = "mid" unique = "true" class = "bi. one2workflow. jointable. minister "/> </set> </class> 

Test file:

 
Public class test {@ Org. JUnit. testpublic void testadd () {sessionfactory Sf = hibernateutil. getsessionfactory (); Session session = SF. getcurrentsession (); Session. begintransaction (); Minister M1 = new minister (); m1.setname (""); Minister m2 = new minister (); m2.setname (" "); emperor = new emperor (); emperor. setname ("Qiwei Wang"); // emperor. getministers (). add (M1); // emperor. getministers (). add (m2); m1.setemperor (Emperor); m2.setemperor (Emperor); Session. save (Emperor); Session. save (M1); Session. save (m2); Session. begintransaction (). commit ();}}

Test results:

Hibernate: insert into Emperor (name) values (?) Hibernate: insert into Minister (name) values (?) Hibernate: insert into emperorminister (Eid, mid) values (?, ?) Hibernate: insert into Minister (name) values (?) Hibernate: insert into emperorminister (Eid, mid) values (?, ?)

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.