2012-03-13 47 views
0

-HQL查詢到孩子實體與面臨問題的雙向關聯

////////////////// 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列:ORG_ID(應該被映射爲insert =「false」update =「false」)

事情是,我懷疑有一堆相同字段名稱ORG_ID列的名稱與表格MARKSIST.GB_LOAD_FORECAST中以及表格MARKSIST.GB_CAPACITY中的相同。或者不是嗎?

回答

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") 
+0

嗨,JB Nizet!是的,它幫助,謝謝! :-) – 2012-03-29 12:16:19