论坛首页 Java企业应用论坛

问题 id ;generator ;unsaved-value 三者的设置和影响

浏览 3758 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2003-10-22  
我用 id ;generator 搜索,一直没有找到有关这三者设定的具体描述和他们设置的影响,再hibernate的文当,描述的也很简单,但是我读了这了论坛的文章发现似乎大家对这个问题都有些困惑,希望斑竹能总结一下。
   发表时间:2003-10-22  
引用

unsaved-value (optional - defaults to null): An identifier property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from transient instances that were saved or loaded in a previous session.

The unsaved-value attribute is important! If the identfier property of your class does not default to null, then you should specify the actual default.


引用

assigned
lets the application to assign an identifier to the object before save() is called.


引用

The unsaved-value attribute is used to specify the identifier value of a newly instantiated instance. unsaved-value defaults to "null", which is perfect for a Long identifier type. If we would have used a primitive identitifier property, we would need to specify


引用

The following code will update parent and child and insert newChild.

//parent and child were both loaded in a previous session
parent.addChild(child);
Child newChild = new Child();
parent.addChild(newChild);
session.update(parent);
session.flush();

Well, thats all very well for the case of a generated identifier, but what about assigned identifiers and composite identifiers? This is more difficult, since unsaved-value can't distinguish between a newly instantiated object (with an identifier assigned by the user) and an object loaded in a previous session. In these cases, you will probably need to give Hibernate a hint; either

1. set unsaved-value="none" and explicitly save() newly instantiated children
2. set unsaved-value="any" and explicitly update() loaded children

before calling update(parent). The first option is probably more sensible and so that is the default unsaved-value for assigned and composite identifiers.

There is one further possibility. There is a new Interceptor method named isUnsaved() which lets the application implement its own strategy for distinguishing newly instantiated objects.
0 请登录后投票
   发表时间:2003-10-22  
to yehs220,多谢整理,有了此贴豁然开朗。
0 请登录后投票
   发表时间:2003-11-11  
补充一下:
unsaved-value: This is the value used to determine if a class has been made persistent, i.e., stored to the database. If the value of the id attribute is null, Hibernate knows that this object has not been persisted. This is important when calling the saveOrUpdate() method, discussed later.
0 请登录后投票
   发表时间:2003-11-11  
引用
The generator element describes the method used to generate primary keys. I’ve chosen to use the hilo generator for purposes of illustration. The hilo generator will use a supporting
table to create the key values. If this method doesn’t appeal to you, don’t worry. In Hibernate2.0, ten primary key generation methods are available and it’s possible to create your own
mechanism, including composite primary keys.
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics