Hibernate/JPA是否考虑了瞬态修饰符(而不是注释) [英] Is Hibernate / JPA taking in consideration the transient modifier ( not the annotation )
问题描述
我想避免序列化(在JMS/AMF中),但仍然使用JPA/Hibernate保留该字段.
I want to avoid serialisation ( in JMS / AMF ) but still persist the field with JPA/Hibernate.
transient
修饰符是我的朋友吗? @Transient
注释和transient
修饰符是否相关?
Is the transient
modifier my friend ? Are @Transient
annotation and the transient
modifier related or not a all ?
java规范精确地指出,系统服务不会将瞬态字段保存到持久性存储中.但是休眠是系统服务吗? (我不这么认为) http://java.sun.com/docs /books/jls/second_edition/html/classes.doc.html#78119
The java specification precise that a transient field will not be saved to a persistent storage by a system service. But is hibernate a system service ? ( i dont think so ) http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78119
和java.io.Serialisable
接缝以指示调用out.writeObject
and in.readObject
进行序列化
http://download.oracle.com/javase /1.4.2/docs/api/java/io/Serializable.html
And java.io.Serialisable
seams to indicate that a out.writeObject
and in.readObject
are called for serialisation
http://download.oracle.com/javase/1.4.2/docs/api/java/io/Serializable.html
有见识吗?
也许我应该写一个快速测试,但是我对一份规范会更有信心.
Maybe should i just write a quick test, but i will be more confident with a piece of spec.
谢谢!
推荐答案
transient
修饰符是我的朋友吗?@Transient
注释和transient
修饰符是否相关?
Is the
transient
modifier my friend ? Are@Transient
annotation and thetransient
modifier related or not a all ?
它们与真的无关,但是恐怕它们也不会成为您的朋友,Hibernate/JPA不会保留transient
属性. JPA规范如下所示:
They are not really related but I'm afraid they won't be your friend anyway, transient
properties aren't persisted by Hibernate/JPA. The JPA specification puts it like this:
2.1.1持久字段和属性
实体的持久状态为 持久性提供者访问 通过JavaBeans样式运行时 属性访问器或通过实例 变量.单一访问类型(字段 或财产访问权)适用于 实体层次结构.当注释是 使用了映射的位置 关于持久性的注释 的字段或持久属性 实体类指定访问类型 作为字段-或属性- 分别基于访问.
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.
- 如果实体具有基于字段的访问权限,则持久性提供程序
运行时访问实例变量
直接地. 所有非
transient
不是的实例变量 用Transient
注释 注释是持久的.什么时候 使用基于字段的访问, 对象/关系映射注释 为实体类注释 实例变量. - 如果实体具有基于属性的访问权限,则持久性提供程序
运行时通过以下方式访问持久状态
属性访问器方法.全部
未用注释的属性
Transient
注释是持久的. 属性访问器方法必须是 公开或受保护的.什么时候 使用基于属性的访问, 对象/关系映射注释 为实体类注释 吸气剂属性访问器. - 映射注释不能应用于以下字段或属性:
是
transient
或Transient
. - 如果将映射注释应用于 持久字段和属性 或XML描述符指定使用 内的不同访问类型 类层次结构.
- 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
are
transient
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节持久字段
- JPA 1.0 specification
- Section 2.1.1 Persistent Fields
这篇关于Hibernate/JPA是否考虑了瞬态修饰符(而不是注释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
References