我在使用 TypedQuery 接口、NamedQuery 和多对多关系创建查询时遇到问题。这是我的报告实体:
@Entity
@Table(name = "REPORT")
@NamedQueries({
@NamedQuery(name = Report.NAMED_QUERY.FIND_USERS, query = "SELECT r.users FROM Report r WHERE r = :report")})
public class Report {
public interface NAMED_QUERY {
String FIND_USERS = "Report.findUsers";
}
@ManyToMany
@JoinTable(name = "REPORT_USER", joinColumns = @JoinColumn(name = "REPORT_ID"), inverseJoinColumns = @JoinColumn(name = "USER_ID"))
private Set<User> users;
//another fields, getters and setters
}
和用户实体。在这里,我没有映射多对多关系的字段。
@Entity
@Table(name = "USER")
public class User {
//fields, getters and setters
}
我不知道如何使用这个命名查询。
public List<User> findUsersRelatedToReport(Report report) {
TypedQuery<User> query = entityManager.createNamedQuery(Report.NAMED_QUERY.FIND_USERS, User.class)
.setParameter("report", report);
return query.getResultList();
}
最后我有例外:
Type specified for TypedQuery [package_name.User] is incompatible with query return type [interface java.util.Set]
任何帮助,将不胜感激。