Hibernate5-1 to multiple (1:n)-Cascade delete-cascade= "DELETE-ORPHANL"

Source: Internet
Author: User

1. Create project, project name HIBERNATEDEMO17, directory structure

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8F/69/wKioL1jdxCnSkUuzAAAfuoGe0Kg130.png-wh_500x0-wm_ 3-wmp_4-s_2870489832.png "title=" Qq20170331105027.png "alt=" Wkiol1jdxcnskuuzaaafuoge0kg130.png-wh_50 "/>


2. Create a Lib directory storage jar file in the project, directory structure

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M00/8F/6C/wKiom1jdxUnyIIRiAABZkoXrEg0408.png-wh_500x0-wm_ 3-wmp_4-s_2700650879.png "title=" Qq20170331105458.png "alt=" Wkiom1jdxunyiiriaabzkoxreg0408.png-wh_50 "/>


3. Create the entity Bean Forum, package name (Com.mycompany.demo.bean) in the SRC directory,

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M00/8F/69/wKioL1jdxW_AUbpuAAA271goiHw872.png-wh_500x0-wm_ 3-wmp_4-s_3934487456.png "title=" Qq20170331105556.png "alt=" Wkiol1jdxw_aubpuaaa271goihw872.png-wh_50 "/>


4. Entity Bean Forum content is as follows
Package Com.mycompany.demo.bean;import Java.util.set;public class Forum {private int fid;private String name;private Set <ForumPost> forumposts;public int Getfid () {return FID;} public void Setfid (int fid) {this.fid = FID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public set<forumpost> getforumposts () {return forumposts;} public void Setforumposts (set<forumpost> forumposts) {this.forumposts = forumposts;}}


5. In the SRC directory, create the Entity Bean Forum Mapping file Forum.hbm.xml, package name (Com.mycompany.demo.bean),

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8F/6C/wKiom1jdxdXxsSXLAAA1xcD-89E125.png-wh_500x0-wm_ 3-wmp_4-s_611862003.png "title=" Qq20170331105738.png "alt=" Wkiom1jdxdxxssxlaaa1xcd-89e125.png-wh_50 "/>


6. The contents of the mapping file Forum.hbm.xml are as follows
<?xml version= "1.0"  encoding= "Utf-8"? ><! doctype hibernate-mapping public   "-//hibernate/hibernate mapping dtd//en"   "HTTP://WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD" > <!--Package: Specify <class/> packages to be located    -->


7. Create the entity Bean Forumpost, package name (Com.mycompany.demo.bean) in the SRC directory,

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/8F/6C/wKiom1jdxgSitAoEAAA1iUHcH8k102.png-wh_500x0-wm_ 3-wmp_4-s_4085026321.png "title=" Qq20170331105719.png "alt=" Wkiom1jdxgsitaoeaaa1iuhch8k102.png-wh_50 "/>


8. The contents of the entity Bean Forumpost are as follows
Package Com.mycompany.demo.bean;public class Forumpost {private int pid;private String subject;private Forum Forum; public int getpid () {return PID;} public void setpid (int pid) {this.pid = pid;} Public String Getsubject () {return subject;} public void Setsubject (String subject) {this.subject = subject;} Public forum Getforum () {return forum;} public void Setforum (Forum forum) {this.forum = Forum;}}


9. Create a mapping file for entity Bean Forumpost in the SRC directory ForumPost.hbm.xml, package name (Com.mycompany.demo.bean),

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M00/8F/69/wKioL1jdxlaSMmb1AAA8RJpMY4g833.png-wh_500x0-wm_ 3-wmp_4-s_3704357965.png "title=" Qq20170331105947.png "alt=" Wkiol1jdxlasmmb1aaa8rjpmy4g833.png-wh_50 "/>


10. The contents of the mapping file ForumPost.hbm.xml are as follows
<?xml version= "1.0"  encoding= "Utf-8"? ><! doctype hibernate-mapping public   "-//hibernate/hibernate mapping dtd//en"   "HTTP://WWW.HIBERNATE.ORG/DTD/HIBERNATE-MAPPING-3.0.DTD" > <!--Package: Specify <class/> packages to be located    -->


11. Create the tool class Hbnutil, package name (com.mycompany.demo.util) in the SRC directory,

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M01/8F/69/wKioL1jdxpWhnwz5AAAu34UFnec992.png-wh_500x0-wm_ 3-wmp_4-s_45022310.png "title=" Qq20170331110049.png "alt=" Wkiol1jdxpwhnwz5aaau34ufnec992.png-wh_50 "/>


12. The contents of the tool class Hbnutil are as follows
Package Com.mycompany.demo.util;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.cfg.configuration;public class Hbnutil {private static sessionfactory sessionfactory;public static Session getsession () {if (sessionfactory = = NULL | | sessionfactory.isclosed ()) {sessionfactory = new Configuration (). Configure (). Buildsessionfactory ();} return sessionfactory.getcurrentsession ();}}


13. Create the Hibernate profile Hibernate.cfg.xml in the SRC directory,

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/8F/6C/wKiom1jdxsugmKZVAAArJV4kQhU416.png-wh_500x0-wm_ 3-wmp_4-s_471516763.png "title=" Qq20170331110143.png "alt=" Wkiom1jdxsugmkzvaaarjv4kqhu416.png-wh_50 "/>


The contents of the 14.Hibernate configuration file Hibernate.cfg.xml are as follows
<?xml version= "1.0"  encoding= "Utf-8"? ><! doctype hibernate-configuration system  "http://www.hibernate.org/dtd/ Hibernate-configuration-3.0.dtd ">


15. Create the test directory in the project to store testing files, file name TestApp, package name (Com.mycompany.demo.bean), directory structure

650) this.width=650; "Src=" https://s1.51cto.com/wyfs02/M02/8F/69/wKioL1jdxwKSdvbmAAAn4fNiUVs541.png-wh_500x0-wm_ 3-wmp_4-s_4202021260.png "title=" Qq20170331110233.png "alt=" Wkiol1jdxwksdvbmaaan4fniuvs541.png-wh_50 "/>


The contents of the 16.TESTAPP test class are as follows
package com.mycompany.demo.bean;import java.util.hashset;import java.util.set;import  org.hibernate.session;import org.junit.before;import org.junit.test;import  com.mycompany.demo.util.hbnutil;public class testapp {private session session;@ Beforepublic void init () {session = hbnutil.getsession ();} /* *  one-to-many bidirectional association-add  *  need to set the Set property in Forum.hbm.xml to Cascade= "All"  */@Testpublic  void  testonetomanyadd () {try {session.begintransaction (); Forumpost forumpost1 = new forumpost (); Forumpost1.setsubject ("A"); Forumpost forumpost2 = new forumpost (); Forumpost2.setsubject ("B"); Set<forumpost> forumposts = new hashset<forumpost> (); ForumPosts.add (ForumPost1 ); Forumposts.add (FORUMPOST2); Forum forum = new forum (); Forum.setname ("Foruma"); forum.setforumposts (forumPosts); Session.save (forum); Session.gettransaction (). commit ();}  catch  (exception e)  {session.gettransaction (). rollback (); E.printstacktrace ();}} /* *  one-to-many bidirectional association-delete  *  need to set the Set property in Forum.hbm.xml to Cascade= "Delete-orphan"  */@Testpublic  void testonetomanyremove () {try {session.begintransaction (); Forum forum = session.get (FORUM.CLASS,&NBSP;10); Set<forumpost> forumposts = forum.getforumposts (); Forumpost forumpost = session.get (forumpost.class, 11); Forumposts.remove (ForumPost); Session.gettransaction (). commit ();}  catch  (exception e)  {session.gettransaction (). rollback (); E.printstacktrace ();}}

650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/8F/6C/wKiom1jdx1WQdxhNAABwCKMO5XE820.png-wh_500x0-wm_ 3-wmp_4-s_1851555295.png "title=" Qq20170324102905.png "alt=" Wkiom1jdx1wqdxhnaabwckmo5xe820.png-wh_50 "/>

This article from the "Vegetarian Yan" blog, declined to reprint!

Hibernate5-1 to multiple (1:n)-Cascade delete-cascade= "DELETE-ORPHANL"

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.