[英]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;
}
}
}
I tried to find an EmployeeInfo object in getCommentedEmployee() method by fkCommentedBy property and set to @Transient property commentedEmployee. 我试图通过fkCommentedBy属性在getCommentedEmployee()方法中找到一个EmployeeInfo对象,并设置为@Transient属性commentedEmployee。
I found the following errors: 我发现以下错误:
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"])
How do I solve it? 我该如何解决?
The purpose of @Transient is to model a non persistent attribute, so it is unclear to me why do you want to @Transient on commentedByEmployee attribute when you have it persisted via "fkCommentedBy" attribute. @Transient的目的是为非持久属性建模,因此我不清楚为什么要在通过“ fkCommentedBy”属性持久化commentedByEmployee属性时对@transient进行建模。 IMO, @ManyToOne is more appropriate in this case. IMO,@ ManyToOne在这种情况下更合适。
@Entity
@Table(name = "projecttaskcomments")
public class ProjectTaskComments {
// .... other declarations
@ManyToOne
@JoinColumn(name="fkCommentedBy")
private EmployeeInfo commentedEmployee;
// ..... other code
}
Now if you still want to use @Transient, then in the getter method you need to make sure that you have a valid referent to EmployeeInfoService object. 现在,如果仍然要使用@Transient,则需要在getter方法中确保对EmployeeInfoService对象具有有效的引用。 @Autowired will not work here as the ProjectTaskComments is not a spring managed bean. @Autowired在这里不起作用,因为ProjectTaskComments不是spring托管的bean。
Agree @KishoreKirdat, neet to check null and do some initialization: 同意@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
}
Yes, finally I could solve it. 是的,终于可以解决了。 I just did the following works: 我刚刚做了以下工作:
Added @Component to ProjectTaskComments class: 在ProjectTaskComments类中添加了@Component:
@Entity @Component @Table(name = "projecttaskcomments") public class ProjectTaskComments{ ........
Declared EmployeeInfoService as static and added a seter method for the service and @Autowired it. 将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.