5
很多時候我在我的實體中使用@Formula。但總是它是一個簡單的查詢或帶有參數的存儲過程,我可以從表格中提取參數。現在我需要從相關對象中使用一些屬性。但是當我嘗試從數據庫中獲取對象時,我會看到異常。請看下面的例子如何在Hibernate中使用實體字段@Formula
@Entity
@Table(name = "MINISTRY")
public class Ministry {
@Id
@Column(name = "ID")
private Long id;
@Column(name = "NAME")
private String name;
// unnecessary code
}
@Entity
@Table(name = "DEPARTMENT")
public class Department {
@Id
@Column(name = "ID")
private Long id;
@Column(name = "DEP_NAME")
private String departmentName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "MINISTRY_ID")
private Ministry ministry;
// unnecessary code
}
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@Column(name = "ID")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DEPARTMENT_ID")
private Department department;
@Formula("test_package.calc_something(department.ministry.id)")
private BigDecimal someMetric;
// unnecessary code
}
我應該如何在@Formula中使用實體prop。 我不希望如果你讀的Formula
的JavaDoc寫類似
select d.ministry.id from Department d ...
感謝您的回答。但我寫了一個壞榜樣。任務是採取相關對象的參數。我更新了例子。提前致謝。 – Dmitry