JPA如何在用户表是连接的答案表中找到前500名,我们正在寻找其中userid等于某事的答案

时间:2017-06-14 12:58:20

标签: java hibernate spring-data-jpa jpql

这个问题将JPA语法视为findTop500 ...其余我无法弄清楚。我试过跟随错误并查看堆栈,没有关于自动生成的JPQL的具体答案。自定义查询不足以执行此任务:

List<Answer> findTop500ByOrderByIdanswerDescWhereUserIduser(@Param("iduser")  long iduser);

&安培;

List<Answer> findTop500ByIdanswerWhereUserIduserOrderByIdanswerDesc(@Param("iduser")  long iduser);

答案表:

@Entity
@Table(name="answer")
public class Answer {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name="Idanswer")
    private long idanswer;


 @ManyToOne
    @JoinColumn(name = "Useriduser", referencedColumnName = "Iduser")
    private User user;
}

用户表:

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="Iduser")
    private long iduser;

}

1 个答案:

答案 0 :(得分:3)

你必须像这样创建它:

SELECT M.[TIMESTAMP], M.[NAME], 
CASE WHEN S.TYPE = 'TYPE_1' THEN S.[VAL] END AS [VAL_TYPE_1], 
CASE WHEN S.TYPE = 'TYPE_2' THEN S.[VAL] END AS [VAL_TYPE_2]
FROM MAIN M
LEFT JOIN SEC S ON M.TIMESTAMP = S.TIMESTAMP
WHERE S.TYPE IN('TYPE_1','TYPE_2');

您必须以实体映射为基础 - &gt; BY user.iduser