2 回答

TA貢獻1900條經驗 獲得超5個贊
首先,@Transient來自 JPA ,與 Spring 無關。
其次,為了能夠讓Spring注入bean Employee,Employee還需要注冊為spring bean。但實際上,您可以認為 Employee 是由 JPA 實現在幕后使用“new”創建的。這就是為什么 spring 不能自動將其他 bean 連接到它。
如果您確實需要這樣做,您可以AspectJ按照文檔中的描述進行操作。
我個人沒有嘗試這種方法,因為你可以簡單地讓你SalaryService接受Employee作為其參數之一來計算他的工資,這比該方法更簡單且易于理解AspectJ。
public interface SalaryService {
public double computeSalary(Employee employee , List<Benefit> benefits, List<Deduction> deductions);
}
客戶端代碼如下所示:
@Service
public class EmployeeService {
@Autowired
private SalaryService salaryService;
@Transactional
public void computeEmployeeSalary(Integer employeeId){
Employee employee = entityManager.find(Employee.class , employeeId);
salaryService.computeSalary(employee, .... ,.....);
}
}

TA貢獻1818條經驗 獲得超3個贊
實體對象是由 JPA 實現(如 Hibernate)創建的,而不是由 spring 管理的。
它們既不是單例也不是原型,所以一般來說,您不能在實體 bean 的屬性上使用自動裝配(因為自動裝配只能在 spring bean 上完成)。
添加回答
舉報