Hibernate One2many (one-way association)

Source: Internet
Author: User

Classes class:

 PackageCom.cnblogs.hibernate_first;ImportJava.util.Set; Public classClasses {Private intID; PrivateString name; //Hibernate extends the set so that it can be loaded lazily, and it needs to be queried for set when needed. For example, there are 1000 students, which are loaded when needed.    PrivateSet students;  PublicSet getstudents () {returnstudents; }     Public voidsetstudents (Set students) { This. Students =students; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

Student:

 Public classStudent {Private intID; PrivateString name;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

Classes.hbm.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-Mapping        Public "-//hibernate/hibernate mapping DTD 3.0//en" Package        = " Com.cnblogs.hibernate_first ">    <class name=" Classes "table=" t_classes ">        <id name=" id " column= "t_id" >            class= "Native" ></generator>        </id>        <property name= " Name "column=" T_name "length=" "/>        <set name=" Students ">            <key column=" Classesid "></ Key>            Class= "Com.cnblogs.hibernate_first. Student "/>        </set>    </class>

Student.hbm.xml:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-Mapping        Public "-//hibernate/hibernate mapping DTD 3.0//en" Package        = " Com.cnblogs.hibernate_first ">    <class name=" Student "table=" t_student ">        <id name=" id " column= "t_id" >            class= "Native" ></generator>        </id>        <property name= " Name "column=" T_name "length="/>    </class>

Test class:

 PackageCom.cnblogs.hibernate_first;ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Set;Importorg.hibernate.Session;Importjunit.framework.TestCase; Public classOne2manytestextendsTestCase { Public voidTestSave1 () {Session session=NULL; Try{Session=hibernateutils.getsession ();            Session.begintransaction (); //Establish an associationStudent Student1 =NewStudent (); Student1.setname ("Zhang San"); Student Student2=NewStudent (); Student2.setname ("John Doe"); Classes Classes=NewClasses (); Classes.setname ("Power Node"); Set<Student> students =NewHashset<student>();            Students.add (STUDENT1);            Students.add (Student2);            Classes.setstudents (students); //because student is not a persistent state, he is transient stateSession.save (classes);        Session.gettransaction (). commit (); } Catch(Exception e) {e.printstacktrace ();        Session.gettransaction (). rollback (); } finally{hibernateutils.closesession (session); }    }         Public voidTestSave2 () {Session session=NULL; Try{Session=hibernateutils.getsession ();            Session.begintransaction (); //Establish an associationStudent Student1 =NewStudent (); Student1.setname ("Zhang San");                        Session.save (STUDENT1); Student Student2=NewStudent (); Student2.setname ("John Doe");                        Session.save (Student2); Classes Classes=NewClasses (); Classes.setname ("Power Node"); Set<Student> students =NewHashset<student>();            Students.add (STUDENT1);            Students.add (Student2);            Classes.setstudents (students); //because student is not a persistent state, he is transient stateSession.save (classes);        Session.gettransaction (). commit (); } Catch(Exception e) {e.printstacktrace ();        Session.gettransaction (). rollback (); } finally{hibernateutils.closesession (session); }    }     Public voidTestLoad1 () {Session session=NULL; Try{Session=hibernateutils.getsession ();            Session.begintransaction (); Classes Classes= (Classes) session.load (Classes.class, 1); System.out.println ("Classes.name =" +classes.getname ()); Set Students=classes.getstudents ();  for(Iterator iter =students.iterator (); Iter.hasnext ();) {Student Student=(Student) iter.next (); System.out.println ("Student in Classes" +student.getname ());        } session.gettransaction (). commit (); } Catch(Exception e) {e.printstacktrace ();        Session.gettransaction (). rollback (); } finally{hibernateutils.closesession (session); }    }}

Hibernate one-to-many association mappings (unidirectional classes--"Student)
Hibernate extends the set so that it can be loaded lazily, and it needs to be queried for set when needed. For example, there are 1000 students, which are loaded when needed.
A one-to-many and many-to-one mapping principle is the same, all at the end of the many to join a foreign key point to one end
Their difference is that the maintenance relationship is not
Many-to-one maintenance relationship: Multi-point-to-one relationship, if the maintenance of a multi-point-to-one relationship, then load more time will be loaded up.
One-to-many maintenance relationship: a point-to-many relationship, if you maintain a point-to-many relationship, then loading one will load up.

Maintain a defect at one end of a relationship
* Because one end of the student do not know the existence of classes (that is, student does not maintain the relationship with classes), so when saving student, the relationship field classid is null,
If you set the relationship field to non-empty, you will not be able to save the data.
* In addition, because student does not maintain relationships, and classes maintains relationships, classes will issue redundant update statements to ensure classes and student are related, so that loading classes
To load the classes corresponding student collection.

Hibernate One2many (one-way association)

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.