Hibernate Spring数据。冬眠当一对多关系发生时,限制联接表中的结果
我有两张桌子。 我想查找用户所做的最后一个操作,还可以添加将此操作作为参数max date发送的可能性Hibernate Spring数据。冬眠当一对多关系发生时,限制联接表中的结果,hibernate,spring-data,spring-data-jpa,Hibernate,Spring Data,Spring Data Jpa,我有两张桌子。 我想查找用户所做的最后一个操作,还可以添加将此操作作为参数max date发送的可能性 create table user( id int, name char(5) ); create table action( id int, user_id int, action char(5), rep_date date ); 在sql中是这样的 select t1.*, t2* from user t1 left join action t2 on (t1.id =
create table user(
id int,
name char(5)
);
create table action(
id int,
user_id int,
action char(5),
rep_date date
);
在sql中是这样的
select t1.*, t2* from user t1
left join action t2 on (t1.id = t2.user_id)
join (select t3.user_id, max(t3.rep_date) as max_date
from action t3
group by t3.user_id) t4 on (t2.user_id=t4.user_id and t2.rep_date=t4.max_date)
我在用户中创建实体类是have
@名单上的一个女人
存储库中
@Repository
public interface StudyMatherialRepository extends CrudRepository<User, Long> {
@Query(value="select distinct u from User u left join fetch sm.action a1 join (select a2.user_id, max(a2.rep_date) max_date from sm.history a2
group by a2.user_id) a3 on (a1.user_id = a2.user_id and a1.rep_date= a2.max_date)
")
public List<StudyMaterial> findAllwithNative();
}
我得到
org.hibernate.hql.internal.ast.QuerySyntaxException:意外令牌
我不知道如何修复它。我相信通过来自UserAction实体来实现这一点会更容易,它可能与用户有@manytone关系,允许我们加入获取它:
SELECT a
FROM UserAction a
JOIN FETCH a.user u
WHERE a.id IN (
SELECT a_.id
FROM UserAction a_
GROUP BY a_.user
HAVING MAX( a_.repDate )
)