加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Hibernate / JPA是否考虑了transiant修饰符(而不是注释)

发布时间:2020-12-14 05:58:59 所属栏目:Java 来源:网络整理
导读:我想避免序列化(在JMS / AMF中),但仍然使用JPA / Hibernate保持字段. 瞬态修饰符是我的朋友吗? @Transient注释和瞬态修饰符是否相关? java规范精确地指出,瞬态字段不会被系统服务保存到持久存储中.但是hibernate是一个系统服务吗? (我不这么认为) http://
我想避免序列化(在JMS / AMF中),但仍然使用JPA / Hibernate保持字段.

瞬态修饰符是我的朋友吗? @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 the transient 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 the Transient

    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 or Transient
    .
  • 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

相关问题

> Why does JPA have a @Transient annotation?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读