我们针对 Oracle 11G 数据库使用带有 JBoss 7 的 Hibernate 4.0。java pojo 类具有这两个属性(除其他属性外)。请注意,SalesPerson 类有一个 SalesPersonId,它是一个持续存在于 SalesOrder 表中的复合键。
public class SalesOrder extends OurBaseClass<SalesOrder>{
private SalesPersonId salesPersonId;
private SalesPerson salesPerson;
@Embedded
@AttributeOverride(name = "location", column = @Column(name = "sale_person_loc"))
@XmlElement
public SalesPersonId getSalesPersonId() {
return salesPersonId;
}
public void setSalesPersonId(SalesPersonId salesPersonId) {
this.salesPersonId = salesPersonId;
}
//The below object contains a SalesPersonId as composite primary key
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns( {
@JoinColumn(name = "sales_person_no", insertable = false, updatable = false),
@JoinColumn(name = "sales_person_loc", insertable = false, updatable = false)
} )
@XmlElement
public SalesPerson getSalesPerson() {
return salesPerson;
}
public void setSalesPerson(SalesPerson salesPerson) {
this.salesPerson = salesPerson;
}
}
休眠条件代码如下:
Session session = (Session) manager.getDelegate();
Criteria criteria = session
.createCriteria(SalesOrder.class,"so")
.createAlias("salesPerson", "salesperson",JoinType.LEFT_OUTER_JOIN)
.setMaxResults(100)
.addOrder(Order.asc("id"));
hibernate生成的sql是这样的:
select * from ( select this_.sales_person_no as sales_person4_641_2_,
this_.sales_person_loc as sales_person5_641_2_,
this_.SALES_PERSON_NO as SALES_PERSON4_641_2_
from SALES_ORDER this_, sales_person sales_person2_
where this_.sales_person_no=sales_person2_.sales_person_no(+) and
this_.sales_person_loc=sales_person2_.loc(+)
order by this_.order_no asc ) where rownum <= ?
返回的错误是由于休眠生成的重复列名。有什么可以解决这个错误的吗?我知道如果我从 pojo 中删除 SalesPersonId,我不会收到错误,但是,我被告知该类需要 SalesPerson 和 SalesPersonId。我也知道如果我删除 setMaxResults 我没有收到错误,但是,这不是一个选项。感谢您的任何帮助。