Hibernate / JPA respects transient modifier (not annotation)

I want to avoid serialization (in JMS / AMF), but still keep the field with JPA / Hibernate.

Is transient modifier my friend? Are @Transient annotations, and is the transient modifier bound or not all?

The java specification specifies that the transition field will not be persistent storage by the system service. But a sleeping system service? (I don’t think so) http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#78119

And java.io.Serialisable seams to indicate that a out.writeObject and in.readObject is called to serialize http://download.oracle.com/javase/1.4.2/docs/api/java/io/Serializable.html

Any insight?

Maybe I'll just write a quick test, but I will be more confident in the specification part.

Thanks!

+6
java serialization hibernate jpa persistence
source share
3 answers

Is transient modifier my friend? Are @Transient annotations, and is the transient modifier bound or not all?

They are not actually related to each other, but I am afraid that they will not be your friend, the transient properties are not saved by Hibernate / JPA. The JPA specification does the following:

2.1.1 Constant fields and properties

Constant state of the object access to the provider resource runtime through the JavaBeans style of the property owners or through instance variables. A single access type (field or property access) is applied to an entity hierarchy. When annotations are used, placing annotation mapping on persistent fields or persistent properties, the entity class indicates the type of access as either a field or a property β€” based on access, respectively.

  • If the object has field-based access, the runtime persistence provider accesses instance variables directly. All non transient instance variables that are not annotated using transient annotations are constant. when field access is used, an object / relational mapping annotation for an entity class annotates instance variables.
  • If the object has property-based access, the persistence provider accesses a persistent state through resource access methods. All properties not annotated Transient annotations are persistent. Resource access methods must be public or secure. when property-based access is used, an object / relational mapping annotation for an entity class annotates the getter.
  • Binding annotations cannot be applied to fields or properties that are transient or transient .
  • The behavior is unspecified if binding annotations apply to both constant fields and properties or if the XML descriptor indicates the use of different access types within the class hierarchy.

...

References

Related Questions

  • Why does JPA have @Transient annotation?
+6
source share

The part of the JPA specification published by Pascal Thivent seems rather confusing. In fact, Hibernate respects transient when accessing a field is used, but ignores it when accessing properties. Perhaps this is Hibernate-specific behavior.

For example, in this case bar not serialized, but stored in the database:

 @Entity @Access(AccessType.FIELD) // Default access type - field public class Foo { @Id @GeneratedValue private Long id; transient private String bar; ... @Access(AccessType.PROPERTY) // Override default access type for this property public String getBar() { return bar; } } 

EDIT:. Since it is unclear how this behavior complies with the JPA specification, it might be best to use different names for the transient field and the corresponding property.

+3
source share

Try providing an implementation of writeObject(ObjectOutputStream oos) that does not make a call to oos.defaultWriteObject() , but manually writes all the necessary properties.

But I'm not sure if this might work, google, do I need to strictly require defaultWriteObject first.

+1
source share

All Articles