About Unsaved-Value [Resource: Javaeye]

xiaoxiao2021-03-06  75

When you explicitly use session.save () or session.Update (), you don't do unsaved-value. In some cases (parent sidelic table related to saving), when you do not explicitly use Save or Update a lasting object in the program, Hibernate needs to determine whether the object being operated is a persistent persistent object, is a Memory temporary objects that have not been persisted. E.g:

Java code:

1 session session = ...;

2 Transaction tx = ...;

3

4 Parent Parent =

(PARENT

Sense.

Load

(PARENT.

Class, ID

);

5

6 Child Child =

New child

(

);

7 Child.

SetParent

(PARENT

);

8 child.

SetName

("Sun"

);

9

10 parent.

AddChild

(Child)

);

11 s.

Update

(PARENT

);

12

13 s.

Flush

(

);

14 TX.

Commit

(

);

15 s.

Close

(

);

In the above example, the program does not have an explicit session.save (child); then Hibernate needs to know that Child is a temporary object, or has a persistent object in the database. If Child is a newly created temporary object (this is this case), Hibernate should automatically generate SESSION.SAVE (Child) operations, if Child is a persistent object already in the database, then Hibernate should be automatically Generate SESSION.UPDATE (CHILD) operations. So we need to hint Hibernate, and if the Child object should automatically save it or Update. In the above example, it is apparent that we should suggest Hibernate automatic Save instead of automatic Update. So how does Hibernate judges to CHILD is SAVE or UPDATE? It will take a lower key attribute child.getID (), here, assuming the ID is a java.lang.integer type. If the ID value taken is equal to the Unsave-Value specified in the HBM mapping file, Hibernate believes that Child is a new memory temporary object, send save, if not, Hibernate thinks Child is an object that has already been persisted and sends Update. Unsaved-value = "null" (default, suitable for most object types "Integer / long / string / ...) When Hibernate takes a child ID, NULL (in the previous example is definitected) It is null, and the Unsaved-Value setting value is equal, send save (child) When Hibernate takes a child ID, it is not null, then the UNSAVED-VALUE setting value is not equal, send Update (Child), for example Case:

Java code:

1 session session = ...;

2 Transaction tx = ...;

3

4 Parent Parent =

(PARENT

Sense.

Load

(PARENT.

Class, ID

);

5 Child Child =

(Child)

Sense.

Load

CHILD.

Class, childid

);

6

7 Child.

SetParent

(PARENT

);

8 child.

SetName

("Sun"

);

9

10 parent.

AddChild

(Child)

);

11 s.

Update

(PARENT

);

12

13 s.

Flush

(

);

14 TX.

Commit

(

);

15 s.

Close

(

);

Child has been in the database. It is a persistent object, not new created, so we hope that Hibernate sends Update (Child), Hibernate takes child.getid (), and unsave-value specified Null compared to the matter, then it is not equal, then send Update (Child). BTW: Parent objects don't need to worry, because the program explicitly has a load operation and Update for PARENT, does not need hibernate to judge what save or Update. We have to pay attention to the operation of the Child object. The additional unsaved-value is defined in the primary key properties of the Child class.

Java code:

1

Class name = "child" Table = "child">

2

Unsaved-value = "

NULL ">

3

Class = "Identity" />

4

5 ...

6

Class>

If the primary key attribute is not an object type, it is a basic type, such as int / long / double / ..., then you need to specify a numeric Unsaved-Value, for example:

Java code:

1 unsaved-

null = "

0 "

Here, many people think that the primary key attribute is defined as int / long, which is high-efficient than the integer / long run. It is considered that the basic type does not need to perform object packages and deconstruction, so I like to define the primary key as int / long. of. But in fact, Hibernate always converts the primary key to the object type, even if you are defined as int / long type, Hibernate should also perform an object construct operation inside, when you return to you, you have to decompose it. Efficiency may be negative or low. So everyone must reverse a point of view. In Hibernate, the primary key attribute is defined as the basic type, and it is not possible to be high, and there are many troubles, and there are many troubles, so I suggest that you use the object-type Integer / long definition. Primary key. Unsaved-value = "none" and unsaved-value = "any" main use at the primary key attribute is not generated by hibernate, but the program is time SETID (). Here more, it is highly recommended to use Hibernate ID Generator, or you can extend Hibernate ID Generator, pay special attention not to use the field with actual meaning as a primary key! For example, the user class User, many people like to use the user to log in as the primary key, this is a very bad habit, when the user class and other entity classes are related to the relationship, if you need to modify the user login name, change Need to change the data in a few tables. The coupling is too high, and if you use the id generator that does not have a business meaning, modify the user name, you only modify the USER table. This problem is discussed, if you design the database in strict accordance with this principle, then you don't use it for hand-to-order (), you use Hibernate ID Generator to OK. So you don't need to know what is meaningful when unsaved-value = "none" and unsaved-value = "any" have any meaning. If you don't use assigned, you will continue to explain: Unsaved-value = "none", because regardless of the primary key attribute is any value, it is impossible to be none, so Hibernate always sends Update (Child) UNSAVED to the Child object. -Value = "any", since the primary key attribute is any value, it is certainly any, so Hibernate always sends Save (Child) in most cases, you can avoid using Assigned, only when you use composite When the primary key is to manually setid (), this time you need to consider how to set up unsaved-value, depending on your own needs. BTW: GAVIN KING strongly does not recommend using the Composite-ID, which is highly recommended to use Usertype. Therefore, if you follow the principle of the system: 1. Use hibernate ID generator to generate uncomfortable primary keys, do not use a field of business meaning to make a primary key, do not use Assigned. 2. Use the object type (string / integer / ...) to do the primary key without using the basic type (int / long / ...), do not use the Composite-ID to handle the case of the composite primary key, Use UserType to process this situation.

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

New Post(0)