0

Are all instance variables in an entity class persistent in the database? Or is it possible for some variables in the entity to be non-persistent?

Sam
  • 47
  • 3
  • 1
    Of course, use transient annotation keyword . – drowny Sep 15 '18 at 08:39
  • 1
    By default they are persistent. If you add `@Transient` annotation on a variable, it is non-persistent. – Phenomenal One Sep 15 '18 at 08:39
  • There are many JPA API docs on the internet and they will ALL explain this. Perhaps read them? For example http://www.datanucleus.org:15080/products/accessplatform_5_2/jpa/mapping.html#member_transient –  Sep 15 '18 at 10:19

1 Answers1

1

Of course, you can with using of @Transient annotation. For example :

class MyClass{

    private int id;
    private String name;

    @Transient
    private int identity;
}

Here, identity field is transient and not persistent in the database.

This means if a variable is marked @Transient then it won't be added in the database. it has no persistence representation of this variable in that hibernate session. After the session is closed these transient objects will be destroyed by garbage collection.

Phenomenal One
  • 2,501
  • 4
  • 19
  • 29
drowny
  • 2,067
  • 11
  • 19