我们正在将Hibernate 4.0与JBoss 7一起用于Oracle 11G数据库。 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;
}
}
hibernate标准代码如下:
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 <= ?
返回的错误是由hibernate生成的重复列名引起的。对于这个错误有什么可以做的吗?我知道如果我从pojo中删除SalesPersonId,我没有得到错误,但是,我被告知该类需要SalesPerson和SalesPersonId。我也知道如果我删除setMaxResults我没有得到错误,但是,这不是一个选项。谢谢你的帮助。
答案 0 :(得分:0)
我确实想到了这一点。这个问题帮助了我: What does @AttributeOverride mean?
由于某种原因,只覆盖了嵌入式ID的复合键的一个属性。所以我在地址覆盖中添加了一个客户属性覆盖,并解决了这个问题。