Rabbit Eight Brother Note 15 (1): Parental Relationship in Hibernate

zhaozj2021-02-16  63

Rabbit Eight Brother Note 15: Parental Relationship in Hibernate

Mailbox: ltf_ty@163.net

This article translates the ninth chapter of Hibernate Help Document.

The first thing for Hibernate's new users with Hibernate is likely to build a data model for establishing a parent-child relationship. To achieve this purpose, there are two ways: because there are different situations, the route of the most convenient (especially for a new user of Hibernate is to establish 2 entities at the same time, one is Parent, the other is Child, then establish A associated from Parent to Child. Another way is life child as a Parent's . Now, it turns out that default semantics of a one to many association (in Hibernate) are much less close to the usual semantics of a parent / child relationship than those of a composite element mapping. (Read the sentence did not quite understand). Below we will show you how to use the Bidirectional One to Many Association with cascad to build an elegant and efficient parent-child relationship model, it is not difficult!

9.1 Summary About a Collection (a Note About Collections)

The Hibernate collection is considered to be an entity's own logical part, not the entity being included. This difference is crucial!

When we add or delete an object from a collection, the version number of the collection owner will increase.

If the object deleted from the collection is an instance of a value type (EG, A Composite Element), the object will no longer last, its status will be completely deleted from the database. Similarly, when an instance of a value type is added to the collection, its state will be held immediately.

On the other hand, if an entity is from a collection (a one-to-many or many-way, "is removed, the associated entity will not be deleted (deleted) by remoded, by default. This behavior is completely consistent with the following statement: changes in the internal state of other entities should not cause disappearance of the associated entity (Vanish)! Similarly, an entity is added to the collection, and by default, the entity is not persisted.

Therefore, when an entity is added to the collection, it is only a connection between two entities (LINK), and when deleting, only delete the connection (LINK), this rule is suitable for a variety of various (all sorts) The situation. However, there is also a case where it is not suitable: In the parent-child relationship, Child's life is bound to the life of the Parent object (Lifecycle).

9.2 Bidirectional One-to-Many (Bidirectional One To Many)

Suppose we have a simple from the Parent to Child object:

If we want to perform the following code:

Parent P = .....

Child C = new child ();

p.GetChildren (). Add (c);

Session.save (c);

Session.flush ();

Hibernate will be converted to 2 SQL statements:

2 a insert statement for creating a child

2 UPDATE statement that creates a relationship between Parent to Child

This is not only efficient, but it violates the NOT NULL constraints of the Parent_ID column.

The following motivation is: From the connection of Parent to Child (foreign key: parent_id) is not considered as part of the status of Child, so it is not created in INSERT. So, we will establish a connection between the Child mapping section:

We also need to add a Parent property for the Child class.

Now the Child entity is managed as a connection state, we use the Inverse to tell the collection Do not change the connection.

The following code will add a new child:

Parent P = (PARENT) Session.Load (Parent.class, PID);

Child C = new child ();

C.SETPARENT (P);

p.GetChildren (). Add (c);

Session.save (c);

Session.flush ();

Now, only one INSERT statement is executed!

Here we add an AddChild () method for PARENT:

Public void addChild (child c) {

C.SETPARENT (THIS);

Children.Add (c);

}

Now, add a child code should look like this:

Parent P = (PARENT) Session.Load (Parent.class, PID);

Child C = new child ();

P.ADDCHILD (C);

Session.save (c);

Session.flush ();

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

New Post(0)