In this tutorial we are going to learn about @Transient annotation using in JPA with Hibernate application to ignore fields.
1. What is the use of @Transient annotation in JPA and Hibernate
@Transient annotation in JPA or Hibernate is used to indicate that a field is not to be persisted or ignore fields to save in the database.
@Transient
exist injavax.persistence
package. It is used to annotate a property or field of an entity class, mapped superclass, or embeddable class.
For example- Let’s say you have created two variables in an entity class in one you want to save in database and second is derived from the first one. So we need not save the second field in the database and can be map with @Transient annotation, the second field will be ignored to save in data base.
1.1. What are persistent fields ?
- Every non-static, non-final entity field is persistent by default in Hibernate or JPA. (If we are not annotated any fields).
1.2. Non persistent or Transient fields
- Transient entity fields are fields that do not participate in persistence or ignore their values are never stored in the database (similar to transient fields in Java that do not participate in serialization).
static
andfinal
entity fields are always considered to be transient.- Other fields can be declared explicitly as transient using either the Java transient modifier or the JPA @Transient annotation (which only affects persistence):
static String transient1;
// not persistent – ignore because of staticfinal String transient2 = “Satish”;
// not persistent – ignore because of finaltransient String transient3;
// not persistent – ignore because of transient keyword@Transient String transient4;
// not persistent – ignore because of @Transient
1.3. @Transient annotation vs transient java keyword
@Transient
annotation is used to ignore a field to not persist in database in JPA, where as transient
key word used to ignore a field from serialization. The field annotated with @Transient
still can be serialized, but the field declared with transient
keyword not to be persisted and not to be serialized. And also @Transient
can be used for property access as well, where as transient
keyword allowed to use only for fields.
To ignore fields to not persist in DB, @Transient
annotation recommended to use, because it is specific to persistence. In some cases you may need to save the object state even though the fields are ignoring to not persist, which is not possible for the transient fields (fields are declared with transient
keyword).
1.4. Example to ignore fields in JPA or Hibernate
Technologies Used in following example :
- JPA 2.1
- Hibernate 5.2.6
- MySql 8.0
- Maven 3
- Spring Tool Suite (STS) 3.9.8
- Java 1.8
- Sometimes it is required that define non persistent fields for representational purpose on view pages or to do some data manipulation purpose.
- For the above table User entity is created. User entity defined one non persistent field to convert
java.sql.date
toString
.
@Entity(name="USER") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") private Long id; @Column(name="USER_NAME") private String userName; @Column(name="PASSWORD") private String password; @Temporal(value=TemporalType.TIMESTAMP) @Column(name="CREATED_TIME") private Date creationTime; @Temporal(value=TemporalType.TIMESTAMP) @Column(name="UPDATED_TIME") private Date updatedTime; @Temporal(value=TemporalType.DATE) @Column(name="DOB") private Date dateofBirth; @Enumerated(value=EnumType.STRING) @Column(name="USER_TYPE") private UserType userType; @Transient private transient String dateOfBirthString; // to display on view public String getDateOfBirthString() { return dateOfBirthString; }
- In user entity dateOfBirthString field is non persistent variable.
- If we want to make dateOfBirthString field non persisatable and If we do not annotate with
@Transient
, dateOfBirthString field should be eitherstatic
orfinal
ortransient
, otherwise JPA identifies it as Unknown column, will get following exception.
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181) ... 1 more Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'dateOfBirthString' in 'field list' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
Conclusion
We have covered how to use @Transient annotation JPA with Hibernate applications in detailed. To use @Transient
in Spring/Spring Boot applications you can refer @Transient in Spring Boot.
Download Application – JPA-Transient.zip (11 KB)
Greate post. Keep writing such kind of info on your page.
Im really impressed by it.
Hey there, You’ve done an excellent job. I’ll certainly digg it and
in my opinion recommend to my friends. I’m sure they will be benefited from this website.