Java 在jpql查询和get as列表中创建并填充POJO

Java 在jpql查询和get as列表中创建并填充POJO,java,mysql,hibernate,jpa,Java,Mysql,Hibernate,Jpa,我有以下实体和pojo: @Entity public class TableA { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) long id; string name; } @Entity public class TableB { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) long id; double price; @ManyToOne @JoinC

我有以下实体和pojo:

@Entity
public class TableA {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id;
string name;
}

@Entity
public class TableB {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id;
double price;
@ManyToOne
@JoinColumn(name = "tableAId")
TableA tableA;

//setters & getters
}
统计数字 我想做一个jpql查询,以获取统计对象的resultlist,它填充了tableA对象的引用id以及TableB表中价格列和行数的总和

我尝试了以下代码,但没有成功:

Query query =  em.createQuery("SELECT NEW se.exampel.Statistics"
+ "(b.tableAId, sum(price) ,count(b)) from TableB b ");

List<Statistics> statistics = query.getResultList();
Query Query=em.createQuery(“选择新se.exampel.Statistics”
+“(b.表b中的表A、金额(价格)、计数(b));
List statistics=query.getResultList();
例外情况


java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:tableAId of:se.exampel.TableB[选择新的se.exampel.Statistics(b.tableAId、计数(b)、总和(价格)) 来自se.exampel.TableB]

我做错了什么

现在它被修复了:
“从tabelB p JOIN p.tabelA s GROUP BY s中选择新的se.examplel.Statistic(s.id、sum(p.price)、count(p))”

查询应包含
b.tableA
,它是属性名,而不是列名
tablead

更新:关于@Chris query的评论应


从TableB中选择新的se.examplel.Statistics(b.tableA.id、sum(b.price)、count(b))
您正在将SQL概念混合到JPQL中。查询需要在实体TableB上进行,因此只能在tablebjava类中使用映射属性。因此,您需要使用以下内容:

"SELECT NEW se.exampel.Statistics(b.tableA.id, sum(b.price) ,count(b)) from TableB b "
请注意,Hibernate可能会从tableB到tableA进行内部联接以获取A的ID。如果您希望能够以JPA中立的方式直接访问tableB中的外键字段,则可能需要在tableB类中为其添加只读字段。差不多

@Column(name="tableA_ID", insertable=false, updatable=false);
long tableAId;
这样,您就可以在查询中访问b.tableAId:

"SELECT NEW se.exampel.Statistics(b.tableAId, sum(b.price) ,count(b)) from TableB b "

并且应该避免表联接。

您遇到的异常情况是什么请发布它@与我一起看更新的post plz。类中没有合适的构造函数:se.exampel.Statistics-构造函数(long tableAId,double price,long count):(try
(b.tableAId,Cast(sum(price)as double),count(b))
问题是tableA是一个实体,而不是一个long。sum(price)应该返回一个double而不进行强制转换。
"SELECT NEW se.exampel.Statistics(b.tableAId, sum(b.price) ,count(b)) from TableB b "