2012-06-01 24 views
0

我有一個數據庫表和一個函數,它接受表的id並返回一些數字,例如(我使用Oracle語法,但我相信RDMS doesn 「噸真的在這種情況下重要),如何向數據對象添加作爲函數執行結果的屬性

CREATE TABLE test1(test1_id INT NOT NULL PRIMARY KEY, 
val VARCHAR(50)); 
CREATE FUNCTION getNum (id IN test1.test1_id%type) RETURN NUMBER IS .... 

在我的休眠數據對象類我想增加持有函數執行結果的屬性。使用註釋做正確的方法是什麼?

@javax.persistence.Table(name = "test1", schema = "test", catalog = "") 
@Entity 
public class Test1Entity 
{ 
     private int test1Id; 
     @Column(name = "test1_id", nullable = false, insertable = true, 
     updatable = true, length = 22, precision = 0) 
     @Id 
     //get/set 
     ... 

     // is it possible to use annotations, 
     // so it will call a function to populate this field ? 
     private BigDecimal test_num; 
} 

我在想創建命名本機查詢,像SELECT a.test1_id, a.val, getNum(a.test1_id) as test_num FROM test1 a WHERE a.test1_id = :param_test1_id,但它意味着我不能從數據庫讀取對象使用Session.get

謝謝。

回答

1

使用此項:

@Formula("getNum(test1_id)") 
private BigDecimal test_num; 
相關問題