Home Hibernate @Transient annotation in Jpa or Hibernate – ignore persistent fields

@Transient annotation in Jpa or Hibernate – ignore persistent fields

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 in javax.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.

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).

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 and final 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  static
final String transient2 = “Satish”;  // not persistent – ignore because of  final
transient String transient3;  // not persistent – ignore because of  transient  keyword
@Transient String transient4;  // not persistent – ignore because of  @Transient

@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.

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

User Table jpa hibernate

  • 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 to String.
@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 dateofBirth.toString();
    }
  • 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 @TransientdateOfBirthString field should be either static or final or transient, 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)

Download Application – JPA-Transient.zip (11 KB)

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles