JPA查询集合映射连接表

时间:2016-11-11 07:11:39

标签: java spring hibernate collections spring-data-jpa

我有两个实体类USER和TRAININGROLE,如下所示

USER(id,csl,fullname,email) - > id - long,其他字符串

TRAININGROLE(id,role) - > role是String,id是Long

我有Session.java实体类,如下所示


    @Id
    @GeneratedValue
    private long id;


    @JoinTable(name="session_user",
            joinColumns = @JoinColumn(name="session_id"),
            inverseJoinColumns = @JoinColumn(name="trainingRole_id"))
    @MapKeyJoinColumn(name="user_id")   
    @ManyToMany
    private Map<User, TrainingRole> users = new HashMap<User, TrainingRole>();

    private Date start;
    private Date end;

我必须查询Session类以查找对特定会话具有特定培训角色的用户。所以我将会话ID和trainingrole对象传递给查询方法。错误的查询在下面..


    public final static String FIND_TRAINER_FOR_SESSION = "SELECT DISTINCT users " +    
                "FROM Session s join s.users users where( :trainingRole in elements(s.users) and s.id = :sid )";
    @Query(FIND_TRAINER_FOR_SESSION)
        List<User> findTrainersForSessions(@Param("trainingRole") TrainingRole trainingRole,@Param("sid") Long sid);

请帮我纠正上述问题,以获得具有特定会话特定培训角色的用户。

还有一件事......我正在使用JPAReposotiry进行查询..

@Repository
public interface SessionRepository extends JpaRepository<Session, Long>{
}

0 个答案:

没有答案