我在我的Spring项目中使用hibernate在java类中映射我的表并使用FetchType.LAZY
我添加了@JsonIgnore
以避免在json映射期间发生异常。使用此配置,性能良好。
这是一个例子:
@Entity
@Table(name = "clientversion", catalog = "ats")
public class ClientVersion implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer idClientVersion;
private String name;
private Date releaseDate;
private String note;
private String filePath;
@JsonIgnore
private Set<User> users = new HashSet<User>(0);
public ClientVersion() {
}
/**
* @param idClientVersion
* @param name
* @param releaseDate
* @param users
*/
public ClientVersion(Integer idClientVersion, String name, Date releaseDate, String note, String filePath, Set<User> users) {
this.idClientVersion = idClientVersion;
this.name = name;
this.note = note;
this.releaseDate = releaseDate;
this.users = users;
this.setFilePath(filePath);
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_clientVersion", unique = true, nullable = false)
public Integer getIdClientVersion() {
return this.idClientVersion;
}
public void setIdClientVersion(Integer idClientVersion) {
this.idClientVersion = idClientVersion;
}
/**
* @return the name
*/
@Column(name = "name", nullable = false)
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the releaseDate
*/
@Column(name = "releaseDate", nullable = false)
public Date getReleaseDate() {
return releaseDate;
}
/**
* @param releaseDate the releaseDate to set
*/
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
/**
* @return the note
*/
@Column(name = "note")
public String getNote() {
return note;
}
/**
* @param note the note to set
*/
public void setNote(String note) {
this.note = note;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "clientVersion")
public Set<User> getUsers() {
return this.users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
/**
* @return the filePath
*/
@Column(name = "filePath")
public String getFilePath() {
return filePath;
}
/**
* @param filePath the filePath to set
*/
public void setFilePath(String filePath) {
this.filePath = filePath;
}
}
这里我没有任何注释,但我无法获得用户:
@Entity
@Table(name = "clientlicense", catalog = "ats")
public class ClientLicense implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private Integer idClientLicense;
private Date startDate;
private Date endDate;
private int counter;
private String macAddress;
private String cpuId;
private User user;
public ClientLicense() {
}
public ClientLicense(Date startDate, Date endDate, int counter, String macAddress, String cpuId, User user) {
super();
this.startDate = startDate;
this.endDate = endDate;
this.counter = counter;
this.setMacAddress(macAddress);
this.setCpuId(cpuId);
this.user = user;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id_clientLicense", unique = true, nullable = false)
public Integer getIdClientLicense() {
return this.idClientLicense;
}
public void setIdClientLicense(Integer idClientLicense) {
this.idClientLicense = idClientLicense;
}
@Column(name = "startDate", nullable = false)
@Temporal(TemporalType.DATE)//This annotation allows to use only YYYY/MM/DD
public Date getStartDate() {
return this.startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
@Column(name = "endDate", nullable = false)
@Temporal(TemporalType.DATE)
public Date getEndDate() {
return this.endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
@Column(name = "counter", nullable = false)
public int getCounter() {
return this.counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
@Column(name = "macAddress", nullable = false)
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
@Column(name = "cpuId", nullable = false)
public String getCpuId() {
return cpuId;
}
public void setCpuId(String cpuId) {
this.cpuId = cpuId;
}
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinColumn(name = "id_username")
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
}
这是返回的json:
{"data":[{"idClientLicense":30,"startDate":"2016-02-10","endDate":"2016-02-19","counter":350,"macAddress":"1223","cpuId":"123CPU","user":null}]}
有时我需要在我的界面中使用user.username
字段,但使用此代码我不会收到用户,因为我使用了@JsonIgnore
。
为了解决这个问题,我使用新的列表复制了返回的List,我在其中设置了字段,但这是一个不好的做法,因为我可能会复制许多元素。
解决此问题的最佳做法是什么?