This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
The problem
Since Java already has a keyword transient, why should JPA have an @TRANSIENT annotation? Why not just use an existing keyword?
answer
Answer 1
The keyword TRANSIENT in Java is used to indicate that a field will not be serialized, while the @TRANSIENT annotation in JPA is used to indicate that a field will not be persisted to the database. Fundamentally, the two are completely different.
Answer 2
Because they don’t mean the same thing! The @TRANSIENT annotation is used to tell the JPA provider not to persist any attributes, while Transient is used to tell the sequence framework not to serialize attributes. When we use the @TRANSIENT annotation, we can serialize it with the keyword Transient.
Answer 3
As mentioned in the previous answer, @TRANSIENT is used to mark fields that should not be persisted:
public enum Gender { MALE, FEMALE, UNKNOWN }
@Entity
public Person {
private Gender g;
private long id;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public long getId(a) { return id; }
public void setId(long id) { this.id = id; }
public Gender getGender(a) { return g; }
public void setGender(Gender g) { this.g = g; }
@Transient
public boolean isMale(a) {
return Gender.MALE.equals(g);
}
@Transient
public boolean isFemale(a) {
returnGender.FEMALE.equals(g); }}Copy the code
When this class is provided to JPA, it persists the ID and gender attributes, but does not attempt to persist the two attributes that return Boolean. Without the @TRANSIENT annotation, An underlying warning is issued that the Person class is missing setMale and setFemale methods, so Person is not persisted.
Answer 4
They have different functions:
@TRANSIENT is mainly used to deal with persistence, while Transient is mainly used to deal with serialization. As programmers, we often mix these two concepts together, but this is not accurate. Persistence refers to the characteristics of a state that are more persistent than the process of creating it. Serialization, on the other hand, is the process of encoding/decoding the state of an object into a byte stream.
And transient is more powerful than @TRANSIENT in that:
If the field uses the TRANSIENT keyword, it will not be serialized when the object is converted to a byte stream. In addition, JPA will treat a field decorated with the TRANSIENT keyword as having the same functionality as the @TRANSIENT annotation, so it will not be persisted by JPA.
On the other hand, when an object is serialized, the @transient bullet will be converted to a byte stream, but it will not be persisted by JPA. So the TRANSIENT keyword has more power than the @TRANSIENT annotation!
Suppose you have a program running on a server that takes several hours to complete. It is very important for us to have the latest solutions. Thus, the client can be scheduled to the server and receive periodic updates during program execution using the ProgressReport object:
@Entity
public class ProgressReport implements Serializable{
private static final long serialVersionUID = 1L;
@Transient
long estimatedMinutesRemaining;
String statusMessage;
Solution currentBestSolution;
}
Copy the code
Solutions might be as follows:
@Entity
public class Solution implements Serializable{
private static final long serialVersionUID = 1L;
double[][] dataArray;
Properties properties;
}
Copy the code
The server persists each ProgressReport to the database. The server doesn’t have to care about the persistence time, but the client does. Therefore, when using the @ Transient annotation estimatedMinutesRemaining object, it will be directly by the JPA persistence, and will not use ProgressReport.
provenance
Stack Overflow: Why does JPA have a @transient annotation?