Java has the transient
keyword. Why does JPA have @Transient
instead of simply using the already existing java keyword?

- 89,107
- 111
- 320
- 448
8 Answers
Java's transient
keyword is used to denote that a field is not to be serialized, whereas JPA's @Transient
annotation is used to indicate that a field is not to be persisted in the database, i.e. their semantics are different.
-
3Yes, the semantics are different. But why was JPA designed this way? – Dilum Ranatunga Jan 28 '10 at 15:20
-
1Not sure I'm understanding you, but have a look at "Pascal Thivent"'s answer ;) – Jawher Jan 28 '10 at 15:46
-
45This is handy because you might not want to store the data in the database, but you do want to store it in the JPA Chaching system that uses serialization for store/restore of entities. – Kdeveloper Oct 12 '10 at 22:13
-
1What "JPA Caching system" that uses serialisation for store/restore of entities ? a JPA implementation can cache an object in any way they wish, and serialisation doesn't enter into it. – DataNucleus Oct 20 '10 at 13:56
-
@Jawher , here for transient not presistant means to not to presist any value or it will insert default value for that attribute. – Satish Sharma Jan 04 '13 at 11:31
-
@DilumRanatunga I tried to address your question in my (late) answer below. – Austin Feb 26 '16 at 02:00
-
The value of trasient after the entity is persisted continue in the cache? – Fernando Pie Apr 03 '17 at 21:09
Because they have different meanings. The @Transient
annotation tells the JPA provider to not persist any (non-transient
) attribute. The other tells the serialization framework to not serialize an attribute. You might want to have a @Transient
property and still serialize it.

- 562,542
- 136
- 1,062
- 1,124
-
3Thanks for the answer Pascal. As a note from your comment: "You might want to have a @Transient property and still serialize it." (That was what I was looking for) I also want to add that the contrary is not true. If one set a variable as transient, than you cannot persist it. – jfajunior Jan 23 '20 at 09:56
As others have said, @Transient
is used to mark fields which shouldn't be persisted. Consider this short example:
public enum Gender { MALE, FEMALE, UNKNOWN }
@Entity
public Person {
private Gender g;
private long id;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public Gender getGender() { return g; }
public void setGender(Gender g) { this.g = g; }
@Transient
public boolean isMale() {
return Gender.MALE.equals(g);
}
@Transient
public boolean isFemale() {
return Gender.FEMALE.equals(g);
}
}
When this class is fed to the JPA, it persists the gender
and id
but doesn't try to persist the helper boolean methods - without @Transient
the underlying system would complain that the Entity class Person
is missing setMale()
and setFemale()
methods and thus wouldn't persist Person
at all.

- 29,022
- 11
- 55
- 82
-
@psp can you explain more on why/how it could cause unspecified behavior? Thanks! – 40pro Aug 11 '15 at 07:51
-
-
8This should be IMHO the accepted answer as it is much more explaining that the current accepted one... – Honza Zidek Oct 16 '15 at 10:02
-
c'mon.. do you really use `equals()` to compare enums? It's like writing `new Integer(3).equals(new Integer(3))` to compare numbers – Jack Feb 22 '22 at 17:00
-
@Jack C'mon, you're replying to 12 year old answer. I haven't touched Java since 2016 even, I'm a full stack Clojurist these days. – Esko Mar 04 '22 at 14:42
-
1
Purpose is different:
The transient
keyword and @Transient
annotation have two different purposes: one deals with serialization and one deals with persistence. As programmers, we often marry these two concepts into one, but this is not accurate in general. Persistence refers to the characteristic of state that outlives the process that created it. Serialization in Java refers to the process of encoding/decoding an object's state as a byte stream.
The transient
keyword is a stronger condition than @Transient
:
If a field uses the transient
keyword, that field will not be serialized when the object is converted to a byte stream. Furthermore, since JPA treats fields marked with the transient
keyword as having the @Transient
annotation, the field will not be persisted by JPA either.
On the other hand, fields annotated @Transient
alone will be converted to a byte stream when the object is serialized, but it will not be persisted by JPA. Therefore, the transient
keyword is a stronger condition than the @Transient
annotation.
Example
This begs the question: Why would anyone want to serialize a field that is not persisted to the application's database? The reality is that serialization is used for more than just persistence. In an Enterprise Java application there needs to be a mechanism to exchange objects between distributed components; serialization provides a common communication protocol to handle this. Thus, a field may hold critical information for the purpose of inter-component communication; but that same field may have no value from a persistence perspective.
For example, suppose an optimization algorithm is run on a server, and suppose this algorithm takes several hours to complete. To a client, having the most up-to-date set of solutions is important. So, a client can subscribe to the server and receive periodic updates during the algorithm's execution phase. These updates are provided using the ProgressReport
object:
@Entity
public class ProgressReport implements Serializable{
private static final long serialVersionUID = 1L;
@Transient
long estimatedMinutesRemaining;
String statusMessage;
Solution currentBestSolution;
}
The Solution
class might look like this:
@Entity
public class Solution implements Serializable{
private static final long serialVersionUID = 1L;
double[][] dataArray;
Properties properties;
}
The server persists each ProgressReport
to its database. The server does not care to persist estimatedMinutesRemaining
, but the client certainly cares about this information. Therefore, the estimatedMinutesRemaining
is annotated using @Transient
. When the final Solution
is located by the algorithm, it is persisted by JPA directly without using a ProgressReport
.

- 8,018
- 2
- 31
- 37
-
2If they actually are different concerns, surely there exists a different word that captures the nuances. Why overload the term? As a starter suggestion, `@Unpersisted`. – Dilum Ranatunga Mar 12 '16 at 18:04
-
4I personally like `@Ephemeral`. According to Merriam Webster: When ephemeral was first printed in English in the 1600s, "it was a scientific term applied to short-term fevers, and later, to organisms (such as insects and flowers) with very short life spans. Soon after that, it acquired an extended sense referring to anything fleeting and short-lived (as in "ephemeral pleasures")." – Austin Apr 25 '17 at 02:30
-
1What I also like about this answer is that it mentions that JPA regards `transient` fields as implicitly having the `@Transient` annotation. So if you use the `transient` keyword to prevent serialization of a field then it will not end up in the database either. – neXus Oct 25 '18 at 12:13
-
1
If you just want a field won't get persisted, both transient and @Transient work. But the question is why @Transient since transient already exists.
Because @Transient field will still get serialized!
Suppose you create a entity, doing some CPU-consuming calculation to get a result and this result will not save in database. But you want to sent the entity to other Java applications to use by JMS, then you should use @Transient
, not the JavaSE keyword transient
. So the receivers running on other VMs can save their time to re-calculate again.
-
-
is there an annotation that jpa will treat as transient but jackson will not? – Kalpesh Soni Sep 26 '18 at 15:23
In laymen's terms, if you use the @Transient annotation on an attribute of an entity: this attribute will be singled out and will not be saved to the database. The rest of the attributes of the object within the entity will still be saved.
Im saving the Object to the database using the jpa repository built in save method as so:
userRoleJoinRepository.save(user2);

- 465
- 1
- 8
- 20
-
3This doesn't answer the question. The question is NOT "what does `@Transient` do?" but rather, "Why not use the keyword." – E-Riz Sep 13 '22 at 21:38
-
@E-Riz nah. by showing what it does it shows why not to use it as I elaborated in my explanation and screen shot, as 6 ppl have also agreed with. pls and thank you. – Llama Jan 31 '23 at 06:03
-
For Kotlin developers, remember the Java transient
keyword becomes the built-in Kotlin @Transient
annotation. Therefore, make sure you have the JPA import if you're using JPA @Transient
in your entity:
import javax.persistence.Transient

- 13,270
- 8
- 79
- 72
I will try to answer the question of "why". Imagine a situation where you have a huge database with a lot of columns in a table, and your project/system uses tools to generate entities from database. (Hibernate has those, etc...) Now, suppose that by your business logic you need a particular field NOT to be persisted. You have to "configure" your entity in a particular way. While Transient keyword works on an object - as it behaves within a java language, the @Transient only designed to answer the tasks that pertains only to persistence tasks.

- 995
- 2
- 14
- 25