Multiple table operations for hibernate

Source: Internet
Author: User
Tags set set

I. One-to-many1. Table Design: Primary FOREIGN Key Association
2. Persistent class design: One party holds multiple set sets, and the parties hold the object of one party
3. Configuration file: One side configures cascading operations; one side abandons foreign key maintenance
two. Many-to-many relationships:1. table Design: Use the intermediate table to map relationships, add a foreign key field each 2. Persistent class design: One set set for each other3. configuration file: The default both sides will maintain the intermediate table, if both sides set up the other's data, must have one side to voluntarily abandon the foreign key maintenance (inverse= "true"), otherwise it will cause the intermediate table to add data repeatedly, an exception occurs.
 three. Multi-table query:Lazy Loading of object navigation (lazy property) uses the default policy: one-check multiple-delay-loading, check-in-load immediately  Key Words:1.cascade Cascade Operations an object to manipulate its associated object at the same time Once the save operation, multiple INSERT statements are issued, so that all the objects become persistent state;
2.inverse Discard foreign key maintenance is a property of <oneToMany> (one side) foreign Key maintenance has two meanings: 1) Primary foreign key mapping (that line) 2) Foreign key column normal add value (NOT NULL)
the ability to maintain foreign key conditions:1) A foreign Key Association 2 is configured in the configuration file) holds the data to be maintained; 3) have the right to maintainNote: inverse= "True/false" only specify when both sides can maintain the default by which side to maintain foreign keys, can not determine the final maintenance of which side;For example: one party does not give up the right to maintain, but the party does not hold multiple data resulting in the inability to maintain foreign keys (have the right to not ability), it can only give up to many parties to maintain foreign keys.3. What is the set set of object mappings initialized to what is 0? Save Memory
4. Transient anomaly (one-to-many):org.hibernate.TransientObjectException: object references an unsaved transient instance-save the transient instance before Flushing:com.itheima.domain.LinkM an occurrence of transient anomalies: when you want to omit the save operation, but do not configure cascading, it may occur (not necessarily)
The occurrence condition of transient state anomaly: in the process of committing a transaction, persisting data to the database, when one of the parties maintains the foreign key data but cannot find the data (the other party does not persist to the database), a transient state anomaly occurs .1. There is no cascade, only one party (maintain the foreign key), in the database side will be unable to find the data of the multi-party report exception2. There is no cascade, only one side (multi-party maintenance foreign key), in the database party does not need to process multiple data, in this case there is no exception, but there is no data from the table.
3. There is no cascade, only save the multiparty (multi-party maintenance foreign key), in the database will be unable to find a side in the report of the exception4. There is no cascade, only save the multi-party (one side maintains foreign keys), in the database in the multi-party can not find the side of the report exception
Case 1 Demo: Sir, with the roster, he's on the battlefield, and he's not named for the soldiers.Case 2 Demo: Sir, there's no need to do anything to get a soldier to report on the battlefield.Case 3 Demo: Soldiers arrive at the battlefield, report to the chief, shouting: "Captain Tom, where are you ..." Little Shenyang: "Sorry, no captain."Case 4 Demo: Soldiers reach the battlefield shouting: "Who is my chief?" (this is also the reason for the redundant UPDATE statement)
Note: Although the class of one and many parties hold each other's data, transient anomalies occur at the time of database operation because the mapping of the primary foreign key cannot be done, and the data that needs to be used is not persisted to the database;Resolve transient anomalies: simple, configure cascade Save (simple) or Save all objects (too cumbersome), so that in the database both have data; 5.cascade and inverse do not have a direct relationship, when learning not to stir together

Multiple table operations for hibernate

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.