@Transient annotation vs transient keyword in Java



For a Java beginner it can be a bit confusing to see that the Java language has both the keyword transient, and that JPA (Java Persistence API) has an annotation named @Transient, and that they seem to mean different things (which they infact do).

The transient keyword tells the JVM to not serialize a specific attribute in an object. To serialize an object means for example to write it to a file, send it over the network etc.

The @Transient annotation in JPA tells the JVM to not persist the attribute in the database when the object is saved.

So why not use only the keyword for both cases? You could have an object where you don’t want to save the attribute to the database, but you want to serialize it when you write it to file for example. Or vise versa, you want to save the attribute to the database but not serialize it when you write it to a file.







Search for more Java info on this site here:
Custom Search


Please type any questions here.