我们如何从选择查询中加载JPA中的瞬态字段。
例如我有这个查询:
SELECT table1.*, (SELECT SUM(field) from table2 WHERE theField=table1.flag) as total FROM table1;
所以在这里我需要一个名为“total”的瞬态字段。
但似乎在JPA中不可能
答案 0 :(得分:2)
您可以在constructor
JPQL
<强>查询:强>
SELECT NEW com.foo.entities.Table1(table1.*, (SELECT SUM(field) from table2 WHERE theField=table1.flag) as total) FROM table1;
<强>实体:强>
@Entity
public class Table1{
// .. other columns
@Transient
int total;
// table1Field1,table1Field2 etc. map to your table1.* coulmns
public Table1(String table1Field1,int table1Field2,int total){
// ..other assignments here
this.total = total; // transient assignment here
}
}