FAQs about many-to-many relationships in hibernate

Source: Internet
Author: User
1. Where can I use cascade = "..."?
  
The cascade attribute is not required for many-to-many relationships. With this attribute, we only make it easier to insert or delete the object, as long as we insert or delete the object at the cascade source, all cascade relationships are automatically inserted or deleted. To make cascade correct, unsaved-value is a very important attribute. Hibernate uses this attribute to determine whether an object should be saved or updated. If the Object ID is unsaved-value, it means that this object is not a persistence object and must be saved (insert ); if the ID is not unsaved-value, it indicates that this object is a persistence object (which already exists in the Database) and only needs to be updated. The saveorupdate method also uses this mechanism.
  
   2. Where should I use inverse = "ture "?
  
The default inverse attribute is false, that is, the link is maintained at both ends of the link. This means that if there is a student, teacher and teacherstudent table, student and teacher have many-to-many relationships, which are represented by the teacherstudent table. So when will we insert or delete records in the teacherstudent table to maintain the relationship? When Hibernate is used, we will not perform operations on the teacherstudent table. The operation on teacherstudent is done by hibernate for us. Hibernate is to check who is specified in the HBM file to maintain the relationship. When "who" is inserted or deleted, it will perform operations on the relational table. The premise is that the object "who" already knows the link, that is, the object at the other end of the link has been set or added to the object "who. As mentioned above, the default value of inverse is false, which means that both ends of the link maintain the relationship. Any operation will be performed on the table. When inverse = "true" is used at one end of a link, such as the bag or set in student, it indicates that the link is maintained by a teacher ). That is to say, when student is inserted, the teacherstudent table is not operated, even if student already knows the relationship. The Link Table operation is performed only when teacher is inserted or deleted. Therefore, it is incorrect to use inverse = "true" at both ends of the link, so that no operation is performed on the Link Table. When both ends are inverse = "false" or the default value is, the maintenance of the link display in the Code is also incorrect, resulting in two relationships inserted in the relationship table.
  
In one-to-many relationships, inverse makes more sense. In many-to-many scenarios, the inverse = "true" has almost the same effect (in terms of efficiency ). However, in one-to-many scenarios, if one party needs to maintain the relationship, it will update every object associated with this "1" Object in "multiple" when "one" side is inserted or deleted. If you want to maintain a "multi" relationship, there will be no update operation, because the relationship is in the Multi-Party object, just insert or delete the Multi-Party object. Of course, we also need to traverse the changes in the operation repair relationship displayed for each object of the "multi" side to reflect in the DB. In any case, it makes the relationship maintained by the "multi" Party more intuitive.
  
   3. What is the difference between cascade and inverse?
  
As you can understand, cascade defines the cascading relationship between objects at both ends of the relationship to the object, while inverse defines the cascading relationship between the relationship and the object.
  
   4. net. SF. hibernate. objectdeletedexception: Deleted Object wocould be re-Saved by cascade (remove deleted object from associations): 2, of Class: XXXXX
  
This problem occurs at the end of the link to be deleted. For example, to delete a teacher that has a relationship with student. This exception is thrown only when Tx. Commit. In this case, a set or list in the student object at the other end of the link removes the display of the teacher object, and then session. Delete (this teacher );. This is to prevent the teacher object from being stored back to the dB when cascade is available on the student end. Therefore, this exception occurs only when the relation definition of student includes cascade = "..." and the relationship is not removed as shown above. So the method to prevent this exception is: 1. If you do not use cascade on student, 2, or cascade, the relationships in the object will be deleted. 3. Use cascade on the teacher side
  
   5. net. SF. hibernate. hibernateexception: identifier of an instance of my. myobject altered from N to n
  
This exception is not frequently encountered in many-to-many scenarios, but the prompt of this exception is not make sense. So it is mentioned that the type in the ID Java object is different from that defined in the HBM file, for example, long is used in Java, type = "integer" is used in HBM, and identity is used in generator.

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.