Criteria API:我可以将 Criteria 与单向 ManyToMany 关联一起使用吗?
Criteria API: can I use Criteria with unidirectional ManyToMany association?
我有 User
个实体:
@Entity
@Table(name = "users")
public class User implements Serializable{
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
和 Project
实体,与 User
具有单向 ManyToMany
关联:
@Entity
@Table(name = "projects")
public class Project {
@Id
@GeneratedValue
private Long id;
@Column
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable
private List<User> users;
然后我想让所有用户分配给指定的项目。最简单的方法是通过 ID 询问 GroupDAO
项目,然后获取该项目中的用户。但我认为这不是他的责任,UserDAO
应该照顾User
。是否有机会通过 Criteria
接口将 Project
实体与 User
结合起来以实现此目的?我尝试了以下方法:
@Override
public List<User> getByProjectId(Long projectId) {
Criteria criteria = createCriteria(User.class);
criteria.setFetchMode("project", FetchMode.JOIN);
criteria.add(Restrictions.eq("project.id", projectId));
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List<User> users = (List<User>) criteria.list();
return users;
}
但它仍然 "need" Project
关联在 User
实体中。如果有人决定帮助我,我会很高兴 - 提前感谢您的宝贵时间。
更新: 我可能找到了可行但不是很好的解决方案,但我仍然向您寻求帮助。 解决方案:
我还尝试了以下方法:
public List<User> getByProjectId(Long projectId) {
Query query = getCurrentSession().createQuery("from User as user right
join Project project where project.id=:projectId");
query.setLong("projectId", projectId);
List users = query.list();
return users;
}
但我得到了 org.hibernate.QueryException: outer or full join must be followed by path expression [from entities.User as user right join Project project where project.id=:projectId]
。然后我放弃了,决定向ProjectDAO
询问项目中的用户:
@Override
public List<User> getUsersInProject(Long projectId) {
Criteria criteria = createCriteria(Project.class);
criteria.createAlias("users", "u");
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
criteria.add(Restrictions.eq("id", projectId));
List resultList = criteria.list();
List<User> users = new ArrayList<User>();
for (Object result: resultList){
Map resultMap = (Map) result;
users.add((User) resultMap.get("u"));
}
return users;
}
看起来没问题,但如果你们中的一些人能看到我的问题并以更优雅的方式解决它,我仍然会很高兴 - 在此先感谢所有观众。
我还尝试了以下方法:
public List<User> getByProjectId(Long projectId) {
Query query = getCurrentSession().createQuery("from User as user right
join Project project where project.id=:projectId");
query.setLong("projectId", projectId);
List users = query.list();
return users;
}
但我得到了 org.hibernate.QueryException: outer or full join must be followed by path expression [from entities.User as user right join Project project where project.id=:projectId]
。然后我放弃了,决定向ProjectDAO
询问项目中的用户:
@Override
public List<User> getUsersInProject(Long projectId) {
Criteria criteria = createCriteria(Project.class);
criteria.createAlias("users", "u");
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
criteria.add(Restrictions.eq("id", projectId));
List resultList = criteria.list();
List<User> users = new ArrayList<User>();
for (Object result: resultList){
Map resultMap = (Map) result;
users.add((User) resultMap.get("u"));
}
return users;
}
我有 User
个实体:
@Entity
@Table(name = "users")
public class User implements Serializable{
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
和 Project
实体,与 User
具有单向 ManyToMany
关联:
@Entity
@Table(name = "projects")
public class Project {
@Id
@GeneratedValue
private Long id;
@Column
private String name;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable
private List<User> users;
然后我想让所有用户分配给指定的项目。最简单的方法是通过 ID 询问 GroupDAO
项目,然后获取该项目中的用户。但我认为这不是他的责任,UserDAO
应该照顾User
。是否有机会通过 Criteria
接口将 Project
实体与 User
结合起来以实现此目的?我尝试了以下方法:
@Override
public List<User> getByProjectId(Long projectId) {
Criteria criteria = createCriteria(User.class);
criteria.setFetchMode("project", FetchMode.JOIN);
criteria.add(Restrictions.eq("project.id", projectId));
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
List<User> users = (List<User>) criteria.list();
return users;
}
但它仍然 "need" Project
关联在 User
实体中。如果有人决定帮助我,我会很高兴 - 提前感谢您的宝贵时间。
更新: 我可能找到了可行但不是很好的解决方案,但我仍然向您寻求帮助。 解决方案:
我还尝试了以下方法:
public List<User> getByProjectId(Long projectId) {
Query query = getCurrentSession().createQuery("from User as user right
join Project project where project.id=:projectId");
query.setLong("projectId", projectId);
List users = query.list();
return users;
}
但我得到了 org.hibernate.QueryException: outer or full join must be followed by path expression [from entities.User as user right join Project project where project.id=:projectId]
。然后我放弃了,决定向ProjectDAO
询问项目中的用户:
@Override
public List<User> getUsersInProject(Long projectId) {
Criteria criteria = createCriteria(Project.class);
criteria.createAlias("users", "u");
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
criteria.add(Restrictions.eq("id", projectId));
List resultList = criteria.list();
List<User> users = new ArrayList<User>();
for (Object result: resultList){
Map resultMap = (Map) result;
users.add((User) resultMap.get("u"));
}
return users;
}
看起来没问题,但如果你们中的一些人能看到我的问题并以更优雅的方式解决它,我仍然会很高兴 - 在此先感谢所有观众。
我还尝试了以下方法:
public List<User> getByProjectId(Long projectId) {
Query query = getCurrentSession().createQuery("from User as user right
join Project project where project.id=:projectId");
query.setLong("projectId", projectId);
List users = query.list();
return users;
}
但我得到了 org.hibernate.QueryException: outer or full join must be followed by path expression [from entities.User as user right join Project project where project.id=:projectId]
。然后我放弃了,决定向ProjectDAO
询问项目中的用户:
@Override
public List<User> getUsersInProject(Long projectId) {
Criteria criteria = createCriteria(Project.class);
criteria.createAlias("users", "u");
criteria.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
criteria.add(Restrictions.eq("id", projectId));
List resultList = criteria.list();
List<User> users = new ArrayList<User>();
for (Object result: resultList){
Map resultMap = (Map) result;
users.add((User) resultMap.get("u"));
}
return users;
}