我是 Hibernate 和 SQL 的新手,所以我可能做错了什么并且看不到它——即使现在已经花了第二天的时间:
在我的 PostgreSQL 数据库中,我有两个表:
PieChartResult
+---------+---------+--------------+-------+----------+----------+----------------+
| id | answer | approvalrate | input | worktime | workerid | piecharthit_id |
+---------+---------+--------------+-------+----------+----------+----------------+
PieChartHit
+---------+-----------+
| id | truevalue |
+---------+-----------+
然后我在 pgAdmin 中编写以下 SQL 语句,效果很好:
SELECT DISTINCT input, answer FROM piecharthit INNER JOIN piechartresult ON piecharthit.id=piechartresult_piecharthit_id;
我得到一个结果表,我可以在其中看到每个输入的所有收集到的答案。
当我尝试在 java 中编写相同的语句时
Query q = em.createQuery(SELECT DISTINCT a FROM PieChartHit a INNER JOIN a.PiechartResult b WHERE a.id=b.piecharthit_id")
我只是得到一个例外:
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: PieChartResult of: PieChartHit [SELECT DISTINCT a FROM PieChartHit a INNER JOIN a.PieChartResult b WHERE a.id=b.piecharthit_id]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1364)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1300)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:294)
at Driver.main(Driver.java:37)
Caused by: org.hibernate.QueryException: could not resolve property: PieChartResult of: PieChartHit [SELECT DISTINCT a FROM PieChartHit a INNER JOIN a.PieChartResult b WHERE a.id=b.piecharthit_id]
这两个类看起来像这样:
@Entity
@Table(name="PieChartResult")
public class PieChartResult {
@Id
@GeneratedValue
private Long id;
private String workerID;
private String approvalRate;
private String input;
private double answer;
@ManyToOne
private PieChartHit piecharthit;
和
@Entity
@Table(name="PieChartHIT")
public class PieChartHit {
@Id
@GeneratedValue
private Long id;
private double trueValue;
有人可以指导这个迷失的灵魂吗?:(