我有两个相关的实体:
@Entity
public class Base_Group implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
@Column(unique = true, nullable = false)
private String name;
@ManyToMany(mappedBy = "groups", fetch = FetchType.LAZY )
private Set<Base_Role> roles = new HashSet<Base_Role>(0);
...
@Entity
public class Base_Role implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String id;
@Column(nullable = false)
private String name;
@ManyToMany(fetch = FetchType.EAGER)
private Set<Base_Group> groups = new HashSet<Base_Group>(0);
...
我希望使用以下标准获取数据:
Criteria criteria = hem.getSession().createCriteria(Base_Group, "g");
criteria.setProjection( Projections.projectionList()
.add( Projections.property("g.id"), "id" )
.add( Projections.property("g.name"), "name" )
.add( Projections.property("roles.id"), "roleId" ) )
.createAlias("g.roles", "roles", JoinType.LEFT_OUTER_JOIN, Restrictions.eq("roles.id", roleId))
.setResultTransformer(Transformers.aliasToBean(RoleGroups.class));
但是标准做了如下错误查询:
select this_.id as y0_, this_.name as y1_, roles1_.id as y4_ from Base_Group this_
left outer join Base_Role_Base_Group roles3_ on this_.id=roles3_.groups_id and ( roles1_.id=? )
left outer join Base_Role roles1_ on roles3_.roles_id=roles1_.id and ( roles1_.id=? )
虽然在第一次加入时有额外的条件。有什么想法来解决这个标准?
答案 0 :(得分:0)
因为你在这里做到了:
.createAlias("g.roles", "roles", JoinType.LEFT_OUTER_JOIN, Restrictions.eq("roles.id", roleId))
使用:
...
.createAlias("g.roles", "roles", JoinType.LEFT_OUTER_JOIN)
.setResultTransformer(Transformers.aliasToBean(RoleGroups.class))
.add(Restrictions.eq("roles.id", roleId));
但是,我建议用HQL来做。这是一个带有单一限制的静态查询。