将具有条件的列连接到JPA实体

时间:2017-10-06 10:20:42

标签: java hibernate jpa

我有两张桌子:

person:
- person_id
- name

meeting:
- meeting_id
- person_id
- date

一个人可以举行多次会议。我的JPA实体看起来像:

@Entity
@Table(name = "person")
@Getter
public class Person {

  @Id
  private Integer personId;

  private String name;

  @Formula("(SELECT m.date FROM meeting m WHERE ROWNUM <= 1 AND m.person_id = person_id ORDER BY m.date DESC)")
  private Date lastMeetingDate;

问题是:如何将上次会议日期纳入Person对象。上面的代码生成ORA-00936错误(我使用的是Oracle数据库)。

findPersonById()生成的SQL:

select person0_.person_id as person_id1_10, person0_.name as name2_10, (SELECT m.date FROM meeting m WHERE ROWNUM <= 1 AND m.person_id = person0_.person_id ORDER BY m.date DESC) as formula0_ form person person0_ where person0_.person_id=?

1 个答案:

答案 0 :(得分:2)

你应该尝试:

SELECT max(m.date) FROM meeting m WHERE m.person_id = person_id