gpt4 book ai didi

java - 加入时有额外条件的疯狂 hibernate 标准?

转载 作者:行者123 更新时间:2023-11-29 08:52:25 26 4
gpt4 key购买 nike

我有 2 个相关的实体:

@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=? )

虽然在第一次加入时有不好的额外条件。有解决这个标准的想法吗?

最佳答案

因为你在这里做了:

.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。这是一个具有单一限制的静态查询。

关于java - 加入时有额外条件的疯狂 hibernate 标准?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22112448/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com