Unidirectional One-to-many relationships in Hibernate

Source: Internet
Author: User

Read a book before, the introduction of hibernate in the time repeatedly stressed not easy to set a one-way relationship

The reason for the explanation in the book is that it affects performance (the resulting SQL statements become more).

one-way, many-to-1 Relationships

foreign Key dependency: by following the @ManytoOne @CasCade

First persist from the table records, and then persist the main table record: when the main table does not have a corresponding record, from the table waiting for the main table insert record and then insert (2 sql)

Connection Table:

There is no dependency on the two entity class tables, and the join table relies on the two entity class Tables. Two entity class table data which persisted first (no entity data is inserted to determine the wait process, but there are 3 SQL Statements---two entity class data persisted, join table information Inserted)

The book says that in this case, the connection table is better in this way . (for specific Reasons)

one-way, 1-to-many Relationships

Foreign Key dependency

cascading settings: found @onetomany can not keep up with the @CasCade (cascadetype.all), will Error. (maybe I set the wrong METHOD)

Do not set cascading: first persist from the table record, then persist the main table record: persist the record from the table but not set the foreign key value, and then persist the main Table record, update the foreign key value from the table that record (3 sql)

First persist the main table record, then persist from the table Record: first insert the main table record, and then insert the foreign key value from the table record but not set the foreign key value, then update the values from these records (3 sql)

Join Table: not discussed at this Stage.

A one-way, 1-to-many relationship always requires a single operation of N (in addition to using SQL directly in hibernate).

Although this is logically reasonable, it is really uncomfortable in the Program.

One-to-many relationships in 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.