【问题标题】:Hibernate Criteria.createAlias not joining tablesHibernate Criteria.createAlias 未加入表
【发布时间】:2017-06-17 23:26:27
【问题描述】:

我有这两个实体:

@Entity
@Table(name = "BA_USER", schema = "MYSCHEMA")
@XmlType(namespace = BusinessOperations.NAMESPACE)
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserWeb implements Serializable, UserDetails, SecurityUserInfo, Auditable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "userGenerator")
    @SequenceGenerator(name="userGenerator", sequenceName="SEQ_USER")
    @Column(name = "USERID", unique = true, nullable = false, precision = 22, scale = 0)
    private long userid;

    @ManyToOne(targetEntity = Country.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "COUNTRYID")
    private Country country;

    @ManyToOne(targetEntity = Menu.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "MENUID")
    private Menu menu;

    //Getters and Setters
}

和

@Entity
@Table(name = "BA_PREMIUMUSER", schema = "MYSCHEMA")
public class PremiumUser implements java.io.Serializable {

    @Id
    @OneToOne(targetEntity = UserWeb.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "USERID")
    private UserWeb userWeb;

    @ManyToOne(targetEntity = Customer.class, fetch = FetchType.LAZY)
    private Customer customer;

    //Getters and Setters
}

然后,在我的 DAO 中,我有以下方法:

public List<PremiumUser> findAllUsers(Long userid, Long customerId, Menu menu) {

    Criteria criteria = getSession().createCriteria(PremiumUser.class);

    criteria.add(Restrictions.eq("clienteProsegur", customerId));
    criteria.createAlias("userWeb", "userweb", CriteriaSpecification.INNER_JOIN);
    criteria.add(Restrictions.eq("userweb.menu", menu));

    return criteria.list();
}

Hibernate 正在创建以下查询:

select this_.USERID as USERID52_0_, this_.CUSTOMER as CUSTOMER2_52_0_ 
from MYSCHEMA.BA_PREMIUMUSER this_ 
where userweb1_.MENUID=2 and this_.CUSTOMERID=3

我希望 createAlias 方法在查询中创建一个连接语句,像这样_

select this_.USERID as USERID52_0_, this_.CUSTOMER as CUSTOMER2_52_0_ 
from MYSCHEMA.BA_PREMIUMUSER this_ 
inner join MYSCHEMA.BA_USER userweb1_ on this_.USERID = userweb1.USERID
where userweb1_.MENUID=2 and this_.CUSTOMERID=3

我正在使用这个版本的休眠:Hibernate-core-3.6.0.Final

我想我在这里遗漏了一些东西。

【问题讨论】:

    标签: java hibernate hibernate-criteria


    【解决方案1】:

    在这种情况下,您应该使用方法链接,这可能是您遇到此问题的原因:

    public List<PremiumUser> findAllUsers(Long userid, Long customerId, Menu menu)
      return getSession().createCriteria( PremiumUser.class )
             .add( Restrictions.eq( "clienteProsegur", customerId ) )
             .createAlias( "userWeb", "userweb", CriteriaSpecification.INNER_JOIN )
             .add( Restrictions.eq( "userweb.menu", menu ) )
             .list();
    }
    

    【讨论】:

    • 试过了,得到的查询和以前一样
    • 你用的是什么Hibernate版本?
    • Hibernate-core-3.6.0.Final,我将其添加到问题中
    猜你喜欢
    • 2018-08-21
    • 1970-01-01
    • 2016-07-07
    • 2017-06-18
    • 2016-12-20
    • 1970-01-01
    • 2012-08-22
    • 2012-01-11
    • 2023-03-27
    相关资源
    最近更新 更多