瞬态修饰符是我的朋友吗? @Transient注释和瞬态修饰符是否相关?
java规范精确地指出,瞬态字段不会被系统服务保存到持久存储中.但是hibernate是一个系统服务吗? (我不这么认为)
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78119
并且java.io.Serialisable接缝指示调用out.writeObject和in.readObject进行序列化
http://download.oracle.com/javase/1.4.2/docs/api/java/io/Serializable.html
任何见解?
也许我应该写一个快速测试,但我会对一个规范更有信心.
谢谢 !
解决方法
Is the
transient
modifier my friend ? Are@Transient
annotation and thetransient
modifier related or not a all ?
他们并没有真正相关,但我担心他们不会成为你的朋友,Hibernate / JPA不会保留瞬态属性. JPA规范如下:
2.1.1 Persistent Fields and Properties
The persistent state of an entity is
accessed by the persistence provider
runtime either via JavaBeans style
property accessors or via instance
variables. A single access type (field
or property access) applies to an
entity hierarchy. When annotations are
used,the placement of the mapping
annotations on either the persistent
fields or persistent properties of the
entity class specifies the access type
as being either field – or property –
based access respectively.
- If the entity has field-based access,the persistence provider
runtime accesses instance variables
directly. All non-transient
instance variables that are not
annotated with theTransient
annotation are persistent. When
field-based access is used,the
object/relational mapping annotations
for the entity class annotate the
instance variables.- If the entity has property-based access,the persistence provider
runtime accesses persistent state via
the property accessor methods. All
properties not annotated with the
Transient
annotation are persistent.
The property accessor methods must be
public or protected. When
property-based access is used,the
object/relational mapping annotations
for the entity class annotate the
getter property accessors.- Mapping annotations cannot be applied to fields or properties that
aretransient
orTransient
.- The behavior is unspecified if mapping annotations are applied to
both persistent fields and properties
or if the XML descriptor specifies use
of different access types within a
class hierarchy.…
参考
> JPA 1.0规范
>第2.1.1节持久字段
> Hibernate核心参考指南
> 2.2.2. Mapping simple properties
相关问题