我有这两个实体:
@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方法在查询中创建一个join语句,比如_
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:Hibernate-core-3.6.0.Final
我想我在这里遗漏了一些东西。
答案 0 :(得分:0)
在这种情况下,您应该使用方法链接,这可能是您遇到此问题的原因:
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();
}