Configuration of Hibernate-to-multi-affinity mappings and its cascade deletion problems

Source: Internet
Author: User


Let's start with a simple one-to-many-bi-directional configuration: One end: Questiontype class
Package Com.exam.entity;import Java.util.set;public class Questiontype {private String typename;private Char Typeuniqueness;private Set quesion;public String gettypename () {return typeName;} public void Settypename (String typeName) {this.typename = TypeName;} Public Char gettypeuniqueness () {return typeuniqueness;} public void settypeuniqueness (char typeuniqueness) {this.typeuniqueness = typeuniqueness;} Public Set getquesion () {return quesion;} public void Setquesion (Set quesion) {this.quesion = quesion;}}
Configuration file:
Multi-end: Question Class
Package Com.exam.entity;import Java.util.date;public class Question {private int questionno;private Questiontype questiontype;private String questionstitle;public int Getquestionno () {return questionno;} public void Setquestionno (int questionno) {This.questionno = Questionno;} Public Questiontype Getquestiontype () {return questiontype;} public void Setquestiontype (Questiontype questiontype) {this.questiontype = Questiontype;} Public String Getquestionstitle () {return questionstitle;} public void Setquestionstitle (String questionstitle) {this.questionstitle = Questionstitle;}}
Configuration file:
Let's start by explaining some of the commonly used properties:<many-to-one> elements contain the following properties: Name: Sets the name of the property for the persisted class of the map
Column: The foreign key of the table that corresponds to the properties of the set and persisted class
Class: Sets the type of properties for a persisted class
Cascade: Setting whether to cascade
Lazy: Sets whether lazy loading <set> elements contain the following properties: Name: Sets the name of the property for the persisted class of the map
Cascade: Setting whether to cascade
Inverse: Set reverse control, if True then one end does not maintain foreign keys
&LT;KEY&GT: Sets the foreign key for the table corresponding to the persisted class that is associated.
One-to-many: Setting the associated persistence class if you want to cascade delete a one-to-many association map, you can configure it by the example above: first, you see the end of a:
<set name= "Quesion" inverse= "true" cascade= "delete" ><key column= "Question_type_name"/><one-to-many class= "Question"/></set>

Setting inverse here means that one side does not maintain a foreign key, and setting cascade= "delete" means deleting one end and deleting all objects that are associated with much more.

See more of the end:
<many-to-one name= "Questiontype" column= "Question_type_name"/>

The column here indicates the name of the foreign key, and it needs to be consistent with the column in the key label set at one end, indicating that the same key value is maintained.

You can follow the following code to perform the delete operation:
Session.begintransaction (); Questiontype Questiontype = (questiontype) session.load (Questiontype.class, "judgment question"); Session.delete (QuestionType); Session.gettransaction (). commit ();

The object used here is a persistent state (persistent), and only an object of the persistent state can be cascade deleted using the Session.delete () operation, and the object created by new is in the transient state. The Session.delete () operation cannot be performed.

Configuration of Hibernate-to-multi-affinity mappings and its cascade deletion problems

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.