@Transient
符合您的需求。
要忽略某个字段,请使用 @Transient
对其进行注释,这样它就不会被 hibernate 映射。
但是杰克逊在转换为 JSON 时不会序列化该字段。
如果您需要将 JPA 与 JSON 混合使用(JPA 省略但仍包含在 Jackson 中)使用 @JsonInclude
:
@JsonInclude()
@Transient
private String token;
小费:
您还可以在反序列化期间使用 JsonInclude.Include.NON_NULL 并在 token == null
时隐藏 JSON 中的字段:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Transient
private String token;
@JsonInclude
不是必需的:@Transient
字段仍包含在 JSON 中。 (你仍然得到我的投票:该技术本身在其他情况下可能非常有用)。
@Transient
字段仅在 jackson-datatype-hibernate 模块位于类路径上并使用 ObjectMapper 注册时才被排除,这可能是带有 Jackson 和 Hibernate 的 Spring Boot 应用程序的默认设置,以及为什么设置 @JsonInclude
也是可选的为他人。
这个答案来得有点晚,但它完成了响应。
为了避免实体中的字段被持久化在数据库中,可以使用以下两种机制之一:
@Transient - 将字段标记为不可持久的 JPA 注释
transient 关键字在 java 中。当心 - 使用这个关键字,将阻止该字段与来自 java 的任何序列化机制一起使用。因此,如果必须对字段进行序列化,最好只使用 @Transient 注释。
根据实体属性类型,有多种解决方案。
基本属性
假设您有以下 account
表:
https://i.stack.imgur.com/5oC40.png
account
表映射到 Account
实体,如下所示:
@Entity(name = "Account")
public class Account {
@Id
private Long id;
@ManyToOne
private User owner;
private String iban;
private long cents;
private double interestRate;
private Timestamp createdOn;
@Transient
private double dollars;
@Transient
private long interestCents;
@Transient
private double interestDollars;
@PostLoad
private void postLoad() {
this.dollars = cents / 100D;
long months = createdOn.toLocalDateTime()
.until(LocalDateTime.now(), ChronoUnit.MONTHS);
double interestUnrounded = ( ( interestRate / 100D ) * cents * months ) / 12;
this.interestCents = BigDecimal.valueOf(interestUnrounded)
.setScale(0, BigDecimal.ROUND_HALF_EVEN).longValue();
this.interestDollars = interestCents / 100D;
}
//Getters and setters omitted for brevity
}
基本实体属性映射到表列,因此 id
、iban
、cents
等属性是基本属性。
但是 dollars
、interestCents
和 interestDollars
是计算属性,因此您使用 @Transient
注释它们以将它们从 SELECT、INSERT、UPDATE 和 DELETE SQL 语句中排除。
因此,对于基本属性,您需要使用 @Transient 来排除给定属性的持久化。
协会
假设您有以下 post
和 post_comment
表:
https://i.stack.imgur.com/5aNhN.png
您希望将 Post
实体中的 latestComment
关联映射到最新添加的 PostComment
实体。
为此,您可以使用 @JoinFormula
注释:
@Entity(name = "Post")
@Table(name = "post")
public class Post {
@Id
private Long id;
private String title;
@ManyToOne(fetch = FetchType.LAZY)
@JoinFormula("(" +
"SELECT pc.id " +
"FROM post_comment pc " +
"WHERE pc.post_id = id " +
"ORDER BY pc.created_on DESC " +
"LIMIT 1" +
")")
private PostComment latestComment;
//Getters and setters omitted for brevity
}
获取 Post
实体时,您可以看到 latestComment
已获取,但如果您想修改它,则更改将被忽略。
因此,对于关联,您可以使用 @JoinFormula 忽略写入操作,同时仍允许读取关联。
@MapsId
另一种忽略已由实体标识符映射的关联的方法是使用 @MapsId
。
例如,考虑以下一对一的表关系:
https://i.stack.imgur.com/uELvQ.png
PostDetails
实体的映射如下:
@Entity(name = "PostDetails")
@Table(name = "post_details")
public class PostDetails {
@Id
private Long id;
@Column(name = "created_on")
private Date createdOn;
@Column(name = "created_by")
private String createdBy;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private Post post;
public PostDetails() {}
public PostDetails(String createdBy) {
createdOn = new Date();
this.createdBy = createdBy;
}
//Getters and setters omitted for brevity
}
请注意,id
属性和 post
关联都映射同一个数据库列,即 post_details
主键列。
要排除 id
属性,@MapsId
注释将告诉 Hibernate post
关联处理表主键列值。
因此,当实体标识符和关联共享同一列时,您可以使用@MapsId 忽略实体标识符属性并改用关联。
使用可插入 = 假,可更新 = 假
另一种选择是将 insertable = false, updatable = false
用于您希望被 Hibernate 忽略的关联。
例如,我们可以像这样映射之前的一对一关联:
@Entity(name = "PostDetails")
@Table(name = "post_details")
public class PostDetails {
@Id
@Column(name = "post_id")
private Long id;
@Column(name = "created_on")
private Date createdOn;
@Column(name = "created_by")
private String createdBy;
@OneToOne
@JoinColumn(name = "post_id", insertable = false, updatable = false)
private Post post;
//Getters and setters omitted for brevity
public void setPost(Post post) {
this.post = post;
if (post != null) {
this.id = post.getId();
}
}
}
@JoinColumn
注释的 insertable
和 updatable
属性将指示 Hibernate 忽略 post
关联,因为实体标识符负责 post_id
Primary Key 列。
post
和 post_details
表的上下文中这是否正确?
有时你想:
序列化列 忽略列被持久化:
使用@Column(name = "columnName", insertable = false, updatable = false)
一个好的场景是使用其他列值自动计算某个列
使用 @Transient 使 JPA 忽略该字段。
但! Jackson 也不会序列化该字段。解决只需添加@JsonProperty
一个例子
@Transient
@JsonProperty
private boolean locked;
为了完成上述答案,我使用了一个 XML 映射文件,其中 @Transient
和 transient
都不起作用......我必须将瞬态信息放在 xml 文件中:
<attributes>
(...)
<transient name="field" />
</attributes>
以上答案均不适用于我使用 Hibernate 5.2.10、Jersey 2.25.1 和 Jackson 2.8.9。我终于找到了答案(有点,他们引用了 hibernate4module 但它也适用于 5)here。所有 Json 注释都不能与 @Transient
一起使用。显然 Jackson2 足够“聪明”,可以忽略标有 @Transient
的内容,除非您明确告诉它不要这样做。关键是添加 hibernate5 模块(我用来处理其他 Hibernate 注释)并在我的 Jersey 应用程序中禁用 USE_TRANSIENT_ANNOTATION
功能:
ObjectMapper jacksonObjectMapper = new ObjectMapper();
Hibernate5Module jacksonHibernateModule = new Hibernate5Module();
jacksonHibernateModule.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION);
jacksonObjectMapper.registerModule(jacksonHibernateModule);
这是 Hibernate5Module 的依赖项:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
<version>2.8.9</version>
</dependency>
@JsonProperty
@JsonInclude
@JsonSerialize + @JsonDeserialize
mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, false));
之后,这个解决方案终于奏效了。谢谢!
显然,使用 Hibernate5Module,如果使用 ObjectMapper,@Transient 将不会被序列化。删除将使其工作。
import javax.persistence.Transient;
import org.junit.Test;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TransientFieldTest {
@Test
public void Print_Json() throws JsonProcessingException {
ObjectMapper objectEntityMapper = new ObjectMapper();
//objectEntityMapper.registerModule(new Hibernate5Module());
objectEntityMapper.setSerializationInclusion(Include.NON_NULL);
log.info("object: {}", objectEntityMapper.writeValueAsString( //
SampleTransient.builder()
.id("id")
.transientField("transientField")
.build()));
}
@Getter
@Setter
@Builder
private static class SampleTransient {
private String id;
@Transient
private String transientField;
private String nullField;
}
}