Spring + Hibernate

xiaoxiao2021-03-14  206

When I simulate one-to-one, I have a problem, see the Hibernate document:

There Are Two Varieties of One-to-One Association:

Primary Key Associations Unique Foreign Key Associations

Primary key associations do not need an extra table column; if two rows are related by the association then the two table rows share the same primary key value So if you want two objects to be related by a primary key association, you must make. Sure That They Are Assigned The Same Identifier Value!

Alternative, A Foreign Key with a Unique Constraint, from Employee To Person, May Be Expressed As:

There are two kinds of primary keys / MANY-TO-One.

There is no additional column when the primary key is the same. If 2 tables do not share a primary key, you must pay attention to the main key of the 2 objects must be the same!

Another problem:

Save 3 objects, in the last error, probably illegal to associate a collection to 2 session. It took a long time, and I used Merge () to solve the problem with merge () instead of Update. After discussion analysis, the fundamental reason is that OpenSessionInViewFilter's Singleton is set to false.

Documentation:

If set to false, Each Data Access Operation or Transaction Will Use ITS OWN Session (LIKE WITHOUT OPEN SESSION in View).

Alternatively, turn this filter into deferred close mode, by specifying "singleSession" = "false": It will not use a single session per request then, but rather let each data access operation or transaction use its own session (like without Open Session in VIEW). Each of Those Sessions Will Be Registered for Deferred Close, Though, Actually Processed At Request Completion.

转载请注明原文地址:https://www.9cbs.com/read-129398.html

New Post(0)