对双向关联中的子实体进行hql查询

时间:2012-03-13 13:46:12

标签: java oracle hibernate

面对问题 -

////////////////// GbCapacityEntity class //////////////////
@Entity
@Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.NONE)
@Table(name = "MARKSIST.GB_CAPACITY")
public class GbCapacityEntity {
    @Id
    @Column(name = "ORG_ID")
    private Integer orgId;
...
    @OneToMany(cascade = CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="gbCapacityEntity")
    private List<GbLoadForecast> gbLoadForecast;
    /**
     * 
     * @return
     */
    public List<GbLoadForecast> getGbLoadForecast() {
        return gbLoadForecast;
    }
    /**
     * 
     * @param gbLoadForecast
     */
    public void setGbLoadForecast(List<GbLoadForecast> gbLoadForecast) {
        this.gbLoadForecast = gbLoadForecast;
    }



////////////////// GbLoadForecast class ////////////////// 
@Entity
@Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.NONE)
@Table(name = "MARKSIST.GB_LOAD_FORECAST")
public class GbLoadForecast {
    @Id 
    @Column(name = "ORG_ID")
    private Integer orgId;
...
    @ManyToOne(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY)
    private GbCapacityEntity gbCapacityEntity;
    /**
     * @return
     */
    public GbCapacityEntity getGbCapacityEntity() {
        return gbCapacityEntity;
    }
    /**
     * @param gbLoadForecast
     */
    public void setGbCapacityEntity(GbCapacityEntity gbCapacityEntity) {
        this.gbCapacityEntity = gbCapacityEntity;
    }
...

////////////////// Some query //////////////////  

        String hql = "FROM com.intellex.marksist.hbn.model.GbLoadForecast E " +
                     "WHERE E.orgId = :id1 AND E.cargoGroup = :id2";

        Session session = HibernateUtil.getMarksistSessionFactory().openSession();
        session.beginTransaction();

        Query query = session.createQuery(hql);
        query.setParameter("id1", orgId);
        query.setParameter("id2", gcId);
        List results = query.list();
        session.close();
...

在说明 query.list(); 抛出异常 - [java] 4085 [“http-apr-8080”-exec-10] ERROR org.hibernate.util.JDBCExceptionReporter - ORA-00904:“GBLOADFORE0 _”。“GBCAPACITYENTITY_ORG_ID”:????????? ??? ?????????????

有人知道为什么吗?我会很感激! : - )

=========================================

在子类中添加了@ JoinColumn注释,现在看起来像这样 -

////////////////// GbLoadForecast class ////////////////// 
@Entity
@Cache(usage = org.hibernate.annotations.CacheConcurrencyStrategy.NONE)
@Table(name = "MARKSIST.GB_LOAD_FORECAST")
public class GbLoadForecast {
    @Id 
    @Column(name = "ORG_ID")
    private Integer orgId;
...
    @ManyToOne(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY)
    @JoinColumn(name = "ORG_ID")
    private GbCapacityEntity gbCapacityEntity;
    /**
     * @return
     */
    public GbCapacityEntity getGbCapacityEntity() {
        return gbCapacityEntity;
    }
    /**
     * @param gbLoadForecast
     */
    public void setGbCapacityEntity(GbCapacityEntity gbCapacityEntity) {
        this.gbCapacityEntity = gbCapacityEntity;
    }
...

但是现在又出现了另一个异常 - 由:org.hibernate.MappingException:实体映射中的重复列:com.intellex.marksist.hbn.model.GbLoadForecast column:ORG_ID(应该用insert =“false”update = “假”)

问题是,我怀疑有一堆同名的字段名ORG_ID列如表MARKSIST.GB_LOAD_FORECAST,以及表MARKSIST.GB_CAPACITY。或者不是吗?

1 个答案:

答案 0 :(得分:2)

让我们来看看这个映射:

@Id 
@Column(name = "ORG_ID")
private Integer orgId;
...
@ManyToOne(cascade = {CascadeType.REFRESH}, fetch = FetchType.LAZY)
@JoinColumn(name = "ORG_ID")
private GbCapacityEntity gbCapacityEntity;

这意味着ID将映射到列ORG_ID,并且您还有一个列,它是GB_CAPACITY.ORG_ID列的外键,也称为ORG_ID

同一个表中不能有两个具有相同名称的列。为您的加入列选择其他名称:

@JoinColumn(name = "ORG_ID_OF_CAPACITY")