JPA 瞬态注释和 JSON
这是对JPA瞬态注释的以下问题的后续,为什么JPA具有@Transient注释?
我有一个暂时变量,我不想保留它,它被标记为瞬态注释。但是,当我想从我的 rest 控制器生成 JSON 时,此瞬态变量在输出的 JSON 中不可用。
POJO PublicationVO是直截了当的,没有花哨的属性,只有一些私有属性(持久化),带有getter和setter以及1个瞬态变量。
@RequestMapping(value = { "{publicationId}"}, method = RequestMethod.GET, produces = "application/json")
@ResponseBody public PublicationVO getPublicationDetailsJSON(@PathVariable(value = "publicationId") Integer publicationId) {
LOG.info("Entered getPublicationDetailsJSON - publicationId: " + publicationId);
//Call method to get the publicationVO based on publicationId
PublicationVO publicationVO = publicationServices.getPublicationByIdForRestCalls(publicationId);
LOG.info("publicationVO:{}", publicationVO);
LOG.info("Exiting getPublicationDetailsJSON");
return publicationVO;
}
出版物VO如下
package com.trinity.domain.dao;
import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.fasterxml.jackson.annotation.JsonInclude;
@Entity
@Table(name = "publication")
public class PublicationVO {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@Column(name = "publicationName", unique = false, nullable = false, length = 200)
private String publicationName;
@Column(name = "publicationSource", unique = false, nullable = false, length = 45)
private String publicationSource;
@Column(name = "dateAdded", unique = false, nullable = false)
private Calendar dateAdded;
@Transient
private float percentageProcessed;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPublicationName() {
return publicationName;
}
public void setPublicationName(String publicationName) {
this.publicationName = publicationName;
}
public String getPublicationSource() {
return publicationSource;
}
public void setPublicationSource(String publicationSource) {
this.publicationSource = publicationSource;
}
public Calendar getDateAdded() {
return dateAdded;
}
public void setDateAdded(Calendar dateAdded) {
this.dateAdded = dateAdded;
}
public float getPercentageProcessed() {
return percentageProcessed;
}
public void setPercentageProcessed(float percentageProcessed) {
this.percentageProcessed = percentageProcessed;
}
@Override
public String toString() {
return "PublicationVO [id=" + id + ", publicationName=" + publicationName + ", publicationSource=" + publicationSource + ", dateAdded=" + dateAdded
+ ", percentageProcessed=" + percentageProcessed + "]";
}
}
当我在日志中看到 publishVO 的调试语句时,瞬态变量包含在输出中,但在我的客户端代码中,瞬态变量不包含在 json 响应中。
任何帮助都非常感谢。
谢谢你,达米安