gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:00:32 25 4
gpt4 key购买 nike

我想避免序列化(在 JMS/AMF 中)但仍然使用 JPA/Hibernate 保留该字段。

transient 修饰符是我的 friend 吗? @Transient 注释和 transient 修饰符是相关的还是不是全部?

Java 规范明确指出系统服务不会将 transient 字段保存到持久存储中。但是 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 ?

它们不是真正相关的,但恐怕它们无论如何都不会成为您的 friend ,transient 属性不会被 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.

...

引用资料

相关问题

关于java - Hibernate/JPA 是否考虑了 transient 修饰符(而不是注释),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3976469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com