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
.