面对问题-

////////////////// 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]错误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”更新=“ false”进行映射)

问题是,我怀疑字段名ORG_ID列的名称与表MARKSIST.GB_LOAD_FORECAST和表MARKSIST.GB_CAPACITY中的名称相同。还是不是?

最佳答案

让我们检查一下此映射:

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

08-27 14:30