gpt4 book ai didi

java - JPA 中的 transient 字段和查询中的设置

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:26:28 24 4
gpt4 key购买 nike

我们如何从选择查询中加载 JPA 中的 transient 字段。

例如我有这个查询:

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

所以在这里我的 bean 中需要一个名为“total”的 transient 字段。

但是在JPA里好像不行

最佳答案

您可以在 JPQL 中使用 constructor

查询:

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

}


}

关于java - JPA 中的 transient 字段和查询中的设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20696184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com