Kam*_*icz 102
要忽略一个字段,请用@Transient它来注释它,这样它就不会被hibernate映射.
但是当转换为JSON时,jackson不会序列化该字段.
如果您需要将JPA与JSON混合(由JPA省略但仍包含在Jackson中),请使用@JsonInclude:
@JsonInclude()
@Transient
private String token;
Run Code Online (Sandbox Code Playgroud)
小费:
您还可以使用JsonInclude.Include.NON_NULL并在反序列化期间隐藏JSON中的字段token == null:
@JsonInclude(JsonInclude.Include.NON_NULL)
@Transient
private String token;
Run Code Online (Sandbox Code Playgroud)
Oli*_*POP 16
这个答案有点晚了,但它完成了回复.
为了避免实体中的字段持久存储在DB中,可以使用以下两种机制之一:
@Transient - 将字段标记为不可持久的JPA注释
java中的 transient关键字.注意 - 使用此关键字,将阻止该字段与java中的任何序列化机制一起使用.因此,如果必须对字段进行序列化,则最好只使用 @Transient注释.
Vla*_*cea 12
根据实体属性类型,有多种解决方案。
考虑您有下account表:
该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
}
Run Code Online (Sandbox Code Playgroud)
基本实体属性映射到表列,因此属性喜欢id,iban,cents是基本属性。
但是dollars,interestCents和interestDollars计算性能,让您与注释他们@Transient从SELECT,INSERT,UPDATE和DELETE SQL语句中排除。
因此,对于基本属性,您需要使用
@Transient以从持久化中排除给定的属性。
假设您有以下post和post_comment表格:
您希望latestComment将Post实体中的关联映射到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
}
Run Code Online (Sandbox Code Playgroud)
在获取Post实体时,可以看到latestComment已获取,但如果要修改它,更改将被忽略。
因此,对于关联,您可以使用
@JoinFormula忽略写操作,同时仍然允许读取关联。
忽略已由实体标识符映射的关联的另一种方法是使用@MapsId.
例如,考虑以下一对一的表关系:
在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
}
Run Code Online (Sandbox Code Playgroud)
请注意,id属性和post关联都映射相同的数据库列,即post_details主键列。
为了排除该id属性,@MapsId注解会告诉 Hibernatepost关联负责处理表的主键列值。
因此,当实体标识符和关联共享同一列时,您可以使用
@MapsId忽略实体标识符属性并使用关联来代替。
insertable = false, updatable = false另一种选择是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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
注释的insertable和updatable属性@JoinColumn将指示 Hibernate 忽略post关联,因为实体标识符负责post_id主键列。
小智 9
有时你想:
用 @Column(name = "columnName", insertable = false, updatable = false)
一个很好的场景是当某个列使用其他列值自动计算时
使用 @Transient 使 JPA 忽略该字段。
但!杰克逊也不会连载该领域。要解决只需添加@JsonProperty
一个例子
@Transient
@JsonProperty
private boolean locked;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
155342 次 |
| 最近记录: |