3 回答

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個贊
目的是為一個非持久屬性建模,所以我不清楚為什么當(dāng)你通過“fkCommentedBy”屬性持久化它時,你為什么要在 commentedByEmployee 屬性上使用 @Transient。IMO,@ManyToOne 在這種情況下更合適。
@Entity
@Table(name = "projecttaskcomments")
public class ProjectTaskComments {
// .... other declarations
@ManyToOne
@JoinColumn(name="fkCommentedBy")
private EmployeeInfo commentedEmployee;
// ..... other code
}
現(xiàn)在,如果您仍然想使用@Transient,那么在 getter 方法中,您需要確保您擁有對 EmployeeInfoService 對象的有效引用。@Autowired 在這里不起作用,因?yàn)?ProjectTaskComments 不是 spring 管理的 bean。

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個贊
需要檢查 null 并進(jìn)行一些初始化:
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
}

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超8個贊
是的,我終于可以解決它了。我只是做了以下工作:
將 @Component 添加到 ProjectTaskComments 類:
@Entity
@Component
@Table(name = "projecttaskcomments")
public class ProjectTaskComments{
........
將 EmployeeInfoService 聲明為靜態(tài)并為該服務(wù)添加了一個 seter 方法并@Autowired 它。
@Transient
private static EmployeeInfoService employeeInfoService;
@Autowired
public void setEmployeeInfoService(EmployeeInfoService employeeInfoService) {
this.employeeInfoService = employeeInfoService;
}
添加回答
舉報