简体   繁体   中英

Hibernate Criteria setMaxResults with Oracle 11G giving org.hibernate.exception.SQLGrammarException: ORA-00918: column ambiguously defined

We are using Hibernate 4.0 with JBoss 7 against an Oracle 11G database. The java pojo class has these two properties (in addition to others). Please note that the SalesPerson class has a SalesPersonId, a composite key that persists to the SalesOrder table.

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;
    }
}

The hibernate criteria code is as follows:

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"));

The sql generated by hibernate is this:

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

The error that is returning is due to the duplicate column names generated by hibernate. Is there anything that can be done about this error? I know if I remove SalesPersonId from the pojo, I don't get the error, however, I have been told that the class needs both the SalesPerson and the SalesPersonId. I also know if I remove the setMaxResults I don't get the error, however, that is not an option. Thank you for any help.

I did figure this out. This issue helped me: What does @AttributeOverride mean?

For some reason, only one of the attributes of the composite key for the embedded id was overridden. So I added a customer attribute override to the location override and that took care of this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM