我已经使用Spring数据创建简单的测验,我已经映射问题和类别中的许多使用注释的一个关系。我queering归入指定类别获得随机的问题,但它给上面的错误,我尝试一些替代品。但不工作似乎。
category.Java
@Entity
@Table(name = "categories")
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long catId;
@Column(nullable = false , unique = true)
private String description;
question.Java
@Entity
@Table(name = "question")
public class Question {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long questionId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "catId", nullable = false, updatable = false)
@JsonBackReference
private Category category;
@Lob
@Column (nullable = false)
private String question;
@Column (nullable = false)
private String medium;
question repository.Java
@Repository
@Transactional
public interface QuestionRepository extends JpaRepository<Question, Long> {
@Query("select question from Question question where question.catId = :catId AND question.medium = :medium order by function('RAND')")
List<Question> getQuestionsForAttempt(@Param("catId") Long catId , @Param("medium") String medium);
@Query("select question from Question question where question.catId = : catId")
List<Question> getQuestionByCategory(@Param("catId") Long catId);
}
这是JPQL不是SQL所以你必须去想过在您的查询的相关性:
select question from Question question where question.category.catId
要么
select question from Question question inner join question.category c where c.catId