Hibernate / JPA考虑到transiant修饰符(不是注释) [英] Is Hibernate / JPA taking in consideration the transiant modifier ( not the annotation )
问题描述
我想避免序列化(在JMS / AMF中),但仍然使用JPA / Hibernate持久化该字段。
$ b 瞬态
@Transient
注解和瞬变
修饰符是否相关? java规范精确指出瞬态字段不会被系统服务保存到持久存储中。但是休眠一个系统服务? (我不这么认为)
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
有何洞见?
p>也许我应该写一个快速测试,但是我会对一个规范更有信心。
谢谢!
瞬态
修饰符我的朋友 ?@Transient
注释和瞬态
修饰符是否相关?
blockquote>
它们并不是真正的 ,但恐怕它们不会成为你的朋友,
transient
属性不会被Hibernate / JPA持久化。 JPA规范如下所示:
$ b
2.1.1持久字段和属性
持久化提供者
运行时通过JavaBeans风格的
属性访问器或通过实例
变量访问实体的持久化状态是
。单一访问类型(字段
或属性访问)适用于
实体层次结构。当批注使用
时,映射
注释在持久
字段或
实体类的持久性属性上的放置将访问类型
指定为字段或属性 - 分别基于
访问。
- 如果实体具有基于字段的访问权限,则持久性提供者
运行时直接访问实例变量
。 所有非瞬态
不是
的实例变量用Transient
注释是持久的。使用
基于字段的访问时,实体类的
对象/关系映射注释
注释
实例变量。
- 如果实体具有基于属性的访问权限,则持久性提供者
运行时将通过
属性访问器方法访问持久状态。没有用
Transient
注释注释的所有
属性都是持久的。
属性访问器方法必须是
public或protected。当使用
基于财产的访问时,实体类的
对象/关系映射注释
将注解
getter属性访问器。
- 映射注释不能应用于
为瞬态
或瞬变
- 如果映射注释应用于
持久字段和属性
,或者如果XML描述符指定使用不同的
,则行为未指定访问
类层次结构中的类型。
$ b $ p
$ / $> $
引用
- JPA 1.0规范
- 第2.1.1节持久字段
- Hibernate核心参考指南
相关问题
I want to avoid serialisation ( in JMS / AMF ) but still persist the field with JPA/Hibernate.
Is the
transient
modifier my friend ? Are@Transient
annotation and thetransient
modifier related or not a all ?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
And
java.io.Serialisable
seams to indicate that aout.writeObject
and in.readObject
are called for serialisation http://download.oracle.com/javase/1.4.2/docs/api/java/io/Serializable.htmlAny insight ?
Maybe should i just write a quick test, but i will be more confident with a piece of spec.
Thank !
解决方案Is the
transient
modifier my friend ? Are@Transient
annotation and thetransient
modifier related or not a all ?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 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 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.
...
References
- JPA 1.0 specification
- Section 2.1.1 Persistent Fields
- Hibernate Core Reference Guide
Related questions
这篇关于Hibernate / JPA考虑到transiant修饰符(不是注释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!