Hibernate问题 - 实体映射中的重复列

时间:2017-03-28 15:20:32

标签: java spring hibernate jpa

每次运行单元测试时,我都会遇到"Repeated column in mapping for entity: com.ts.user.entity.TutRptLocYearly column: locid (should be mapped with insert="false" update="false")"问题。 我正在寻求帮助和建议来解决我的问题,因为我努力解决这个问题,但是这个错误会继续发生吗?

以下是我的情况

我创建了2个表,TUT_LOCATION(主键:LocID)和TUT_RPT_LOC_YEARLY(主键:LocID + Year,外键:LocID)。通过使用eclipse JPA Tools,它为我生成了3个类,tutLocation.java,tutRptLocYearly和tutRptLocYearlyPK。

enter image description here

TutLocation.java

    @Entity
@Table(name="tut_location")
@NamedQuery(name="TutLocation.findAll", query="SELECT t FROM TutLocation t")
public class TutLocation extends InitEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Integer locid;

    private String name;

    //bi-directional many-to-one association to TutRptLocYearly
    @OneToMany(mappedBy="tutLocation", cascade={CascadeType.ALL})
    private List<TutRptLocYearly> tutRptLocYearlies;

    public TutLocation() {
    }

    public Integer getLocid() {
        return this.locid;
    }

    public void setLocid(Integer locid) {
        this.locid = locid;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<TutRptLocYearly> getTutRptLocYearlies() {
        return this.tutRptLocYearlies;
    }

    public void setTutRptLocYearlies(List<TutRptLocYearly> tutRptLocYearlies) {
        this.tutRptLocYearlies = tutRptLocYearlies;
    }

    public TutRptLocYearly addTutRptLocYearly(TutRptLocYearly tutRptLocYearly) {
        getTutRptLocYearlies().add(tutRptLocYearly);
        tutRptLocYearly.setTutLocation(this);

        return tutRptLocYearly;
    }

    public TutRptLocYearly removeTutRptLocYearly(TutRptLocYearly tutRptLocYearly) {
        getTutRptLocYearlies().remove(tutRptLocYearly);
        tutRptLocYearly.setTutLocation(null);

        return tutRptLocYearly;
    }
}

TutRptLocYearly.java

    @Entity
@Table(name="tut_rpt_loc_yearly")
@NamedQuery(name="TutRptLocYearly.findAll", query="SELECT t FROM TutRptLocYearly t")
public class TutRptLocYearly extends InitEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private TutRptLocYearlyPK id;

    private Timestamp lastupdate;

    private Integer notrans;

    //bi-directional many-to-one association to TutLocation
    @ManyToOne(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
    @JoinColumn(name="locid")
    private TutLocation tutLocation;

    public TutRptLocYearly() {
    }

    public TutRptLocYearlyPK getId() {
        return this.id;
    }

    public void setId(TutRptLocYearlyPK id) {
        this.id = id;
    }

    public Timestamp getLastupdate() {
        return this.lastupdate;
    }

    public void setLastupdate(Timestamp lastupdate) {
        this.lastupdate = lastupdate;
    }

    public Integer getNotrans() {
        return this.notrans;
    }

    public void setNotrans(Integer notrans) {
        this.notrans = notrans;
    }

    public TutLocation getTutLocation() {
        return this.tutLocation;
    }

    public void setTutLocation(TutLocation tutLocation) {
        this.tutLocation = tutLocation;
    }

}

TutRptLocYearlyPK.java

@Embeddable
public class TutRptLocYearlyPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(insertable=false, updatable=false)
    private Integer locid;

    private Integer year;

    public TutRptLocYearlyPK() {
    }
    public Integer getLocid() {
        return this.locid;
    }
    public void setLocid(Integer locid) {
        this.locid = locid;
    }
    public Integer getYear() {
        return this.year;
    }
    public void setYear(Integer year) {
        this.year = year;
    }

    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof TutRptLocYearlyPK)) {
            return false;
        }
        TutRptLocYearlyPK castOther = (TutRptLocYearlyPK)other;
        return 
            this.locid.equals(castOther.locid)
            && this.year.equals(castOther.year);
    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + this.locid.hashCode();
        hash = hash * prime + this.year.hashCode();

        return hash;
    }
}

错误讯息:

  

引起:org.hibernate.MappingException:映射中的重复列   for entity:com.ts.user.entity.TutRptLocYearly column:locid(should   用insert =&#34; false&#34;映射更新=&#34;假&#34;)at   org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:830)     在   org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:848)     在   org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:870)     在   org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:605)     在org.hibernate.mapping.RootClass.validate(RootClass.java:265)at   org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329)     在   org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443)     在   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879)     ...省略了46个常见帧

0 个答案:

没有答案