关于spring data 中CrudRepository 查询 怎么写@query中hql语句
spring吧
全部回复
仅看楼主
level 1
绯虹的雨 楼主
class Group{
int id;
@OneToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL,mappedBy="gr oup")
Set<Student> stuList;
}
class Student{
int id;
String name;
int mathscores;
@ManyToOne(fetch=FetchType.EAGER,optional=true)
@JoinColumn(name="group_id")
Group group;
}
public interface HeroRepository extends CrudRepository<Group,Integer> {
@Query("select g from Group g where id=?1" )
Group findById(int id);
}
这里查询的group对象包括了所有1个班的学生
我现在需要查询1个班中 mathscores>60的人 并且只需要1个group对象的返回
这个@Query里面的语句怎么写
2014年10月10日 06点10分 1
level 5
@Query("select s from Student s where s.mathScore > ?1 and group = ?2")
public find(int score, Group group) { ... }
2014年10月11日 02点10分 2
level 5
public List<Student> findByGroupAndMathscoresGreaterThan(Group group, int score);
擦,上面忘写返回值了
2014年10月11日 02点10分 3
level 1
绯虹的雨 楼主
感谢楼上的回复
有灵感了 我这样写的
@Query("from Group g join g.stuList ls where h.id=?1 and ls.mathscores>?2 order by ls.mathscores")
List<Object> findXXX(int id,int mathscores);
2014年10月11日 08点10分 4
1