1. Introduction
1.绪论
When persisting Java objects into database records using an Object-Relational Mapping (ORM) framework, we often want to ignore certain fields. If the framework is compliant with the Java Persistence API (JPA), we can add the @Transient annotation to these fields.
在使用对象关系映射(ORM)框架将Java对象持久化为数据库记录时,我们经常希望忽略某些字段。如果该框架符合Java Persistence API(JPA),我们可以为这些字段添加@Transient注解。
In this tutorial, we’ll demonstrate proper usage of the @Transient annotation. We’ll also look at its relationship with Java’s built-in transient keyword.
在本教程中,我们将演示@Transient注解的正确用法。我们还将看看它与Java的内置transient关键字的关系。
2. @Transient Annotation vs. transient Keyword
2.@Transient 注释与transient关键字的对比
There is generally some confusion over the relationship between the @Transient annotation and Java’s built-in transient keyword. The transient keyword is primarily meant for ignoring fields during Java object serialization, but it also prevents these fields from being persisted when using a JPA framework.
对于@Transient注解和Java内置的transient关键字之间的关系,通常存在一些混淆。transient关键字主要是为了在Java 对象序列化期间忽略字段,但它也可以防止这些字段在使用JPA框架时被持久化。
In other words, the transient keyword has the same effect as the @Transient annotation when saving to a database. However, the @Transient annotation does not affect Java object serialization.
换句话说,在保存到数据库时,transient关键字与@Transient注解具有相同的效果。然而,@Transient注解并不影响Java对象的序列化。
3. JPA @Transient Example
3.JPA @Transient 示例
Let’s say we have a User class, which is a JPA entity that maps to a Users table in our database. When a user logs in, we retrieve their record from the Users table, and then we set some additional fields on the User entity afterward. These extra fields don’t correspond to any columns in the Users table because we don’t want to save these values.
假设我们有一个User类,它是一个JPA实体,映射到我们数据库中的Users表。当一个用户登录时,我们从Users表中检索他们的记录,然后我们在User实体上设置一些额外的字段。这些额外的字段并不对应于用户表中的任何列,因为我们不希望保存这些值。
For example, we’ll set a timestamp on the User entity that represents when the user logged in to their current session:
例如,我们将在User实体上设置一个时间戳,代表用户何时登录到他们的当前会话。
@Entity
@Table(name = "Users")
public class User {
@Id
private Integer id;
private String email;
private String password;
@Transient
private Date loginTime;
// getters and setters
}
When we save this User object to the database using a JPA provider like Hibernate, the provider ignores the loginTime field because of the @Transient annotation.
当我们使用Hibernate等JPA提供者将这个User对象保存到数据库时,由于@Transient注解的存在,该提供者忽略了loginTime字段。
If we serialize this User object and pass it to another service in our system, the loginTime field will be included in the serialization. If we didn’t want to include this field, we could replace the @Transient annotation with the transient keyword instead:
如果我们将这个User对象序列化并将其传递给我们系统中的另一个服务,loginTime字段将被包含在序列化中。如果我们不想包括这个字段,我们可以将@Transient注解替换为transient关键字。
@Entity
@Table(name = "Users")
public class User implements Serializable {
@Id
private Integer id;
private String email;
private String password;
private transient Date loginTime;
//getters and setters
}
Now, the loginTime field is ignored during database persistence and object serialization.
现在,loginTime字段在数据库持久化和对象序列化期间被忽略。
4. Conclusion
4.总结
In this article, we have investigated how to properly use the JPA @Transient annotation in a typical use case. Be sure to check out other articles on JPA to learn more about persistence.
在这篇文章中,我们研究了如何在一个典型的使用案例中正确使用JPA @Transient 注解。请务必查看有关JPA的其他文章以了解有关持久性的更多信息。
As always, the full source code of the article is available over on GitHub.
一如既往,文章的完整源代码可在GitHub上获得。