简体   繁体   English

JPA中的瞬态字段和来自Query的设置

[英]Transient Field in JPA and setting from Query

How we can load transient fields in JPA from select queries. 我们如何从选择查询中加载JPA中的瞬态字段。

For example I have this query: 例如,我有这个查询:

SELECT table1.*, (SELECT SUM(field) from table2 WHERE theField=table1.flag) as total FROM table1;

So here I need a transient field called "total" in my bean. 所以我在这里需要一个名为“total”的瞬态字段。

But it seems it is not possible in JPA 但似乎在JPA中是不可能的

You can make use of a constructor in JPQL 您可以在JPQL使用constructor

Query: 查询:

SELECT NEW com.foo.entities.Table1(table1.*, (SELECT SUM(field) from table2 WHERE theField=table1.flag) as total) FROM table1;

Entity: 实体:

@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

}


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM