[英]How can I use Spring Data Projection with a Transient property?
[英]How do I can bind an entity object to a local @Transient property?
@Entity
@Table(name = "employeeinfo")
public class EmployeeInfo{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "employeeId")
private String employeeId;
@Column(name = "firstName")
private String firstName;
@Column(name = "middleName")
private String middleName;
@Column(name = "lastName")
private String lastName;
......
}
@Entity
@Table(name = "projecttaskcomments")
public class ProjectTaskComments{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Long id;
@Basic(optional = false)
@Column(name = "comments")
private String comments;
@Basic(optional = false)
@Column(name = "commentTime")
@Temporal(TemporalType.TIMESTAMP)
private Date commentTime;
@Column(name = "fkCommentedBy")
private Long fkCommentedBy;
@Transient
@JsonIgnoreProperties
private EmployeeInfo commentedEmployee;
@Transient
@Autowired
EmployeeInfoService employeeInfoService;
public EmployeeInfo getCommentedEmployee() {
EmployeeInfo employeeInfo;
employeeInfo = employeeInfoService.getSingleEmployeeInfoByFkUserId(this.fkCommentedBy);
if(employeeInfo != null) {
this.commentedEmployee.setEmployeeId(employeeInfo.getEmployeeId());
this.commentedEmployee.setFirstName(employeeInfo.getFirstName());
this.commentedEmployee.setMiddleName(employeeInfo.getMiddleName());
this.commentedEmployee.setLastName(employeeInfo.getLastName());
this.commentedEmployee.setPhoto(employeeInfo.getPhoto());
return commentedEmployee;
} else {
return null;
}
}
}
我試圖通過fkCommentedBy屬性在getCommentedEmployee()方法中找到一個EmployeeInfo對象,並設置為@Transient屬性commentedEmployee。
我發現以下錯誤:
2018-10-11 13:07:56.834 WARN 16756 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[1]->com.activeboss.model.pm.ProjectTasks["projecttaskcommentsCollection"]->org.hibernate.collection.internal.PersistentBag[0]->com.activeboss.model.pm.ProjectTaskComments["commentedEmployee"])
2018-10-11 13:07:56.853 WARN 16756 --- [nio-8081-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[1]->com.activeboss.model.pm.ProjectTasks["projecttaskcommentsCollection"]->org.hibernate.collection.internal.PersistentBag[0]->com.activeboss.model.pm.ProjectTaskComments["commentedEmployee"])
我該如何解決?
@Transient的目的是為非持久屬性建模,因此我不清楚為什么要在通過“ fkCommentedBy”屬性持久化commentedByEmployee屬性時對@transient進行建模。 IMO,@ ManyToOne在這種情況下更合適。
@Entity
@Table(name = "projecttaskcomments")
public class ProjectTaskComments {
// .... other declarations
@ManyToOne
@JoinColumn(name="fkCommentedBy")
private EmployeeInfo commentedEmployee;
// ..... other code
}
現在,如果仍然要使用@Transient,則需要在getter方法中確保對EmployeeInfoService對象具有有效的引用。 @Autowired在這里不起作用,因為ProjectTaskComments不是spring托管的bean。
同意@KishoreKirdat,需要檢查null並進行一些初始化:
public EmployeeInfo getCommentedEmployee() {
// check here
if (employeeInfoService == null) return null;
EmployeeInfo employeeInfo = employeeInfoService.getSingle...;
if (employeeInfo != null) {
// init here
commentedEmployee = new EmployeeInfo();
commentedEmployee.set...;
return commentedEmployee;
} else {
return null;
}
}
private void setCommentedEmployee(EmployeeInfo employeeInfo) {
// do nothing
}
是的,終於可以解決了。 我剛剛做了以下工作:
在ProjectTaskComments類中添加了@Component:
@Entity @Component @Table(name = "projecttaskcomments") public class ProjectTaskComments{ ........
將EmployeeInfoService聲明為靜態,並為該服務添加了一個seter方法,並對其進行@Autowired。
@Transient private static EmployeeInfoService employeeInfoService; @Autowired public void setEmployeeInfoService(EmployeeInfoService employeeInfoService) { this.employeeInfoService = employeeInfoService; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.