尝试使用相同的嵌入两次时,实例异常映射中的重复列

时间:2015-04-09 06:55:56

标签: eclipse hibernate

我有一个实体SchoolGalleryPic,它有两个BlobField类型的嵌入属性。在执行时,我收到错误说"重复列映射"。我正在使用eclipse和hibernate 4.3.8。

实体:

@Entity
public class SchoolGalleryPic implements java.io.Serializable {

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
private Long schoolGalleryPicID;

@Embedded
@Column(nullable=false)
private BlobField pic;

@Embedded
@Column(nullable=false)
private BlobField thumbnail;

@ManyToOne
@JoinColumn(name="schoolGalleryAlbumID", referencedColumnName="schoolGalleryAlbumID",nullable=false)
private SchoolGalleryAlbum schoolGalleryAlbum;

@Column(nullable=true)
private Integer sortOrder;

@Embedded
@Column(nullable=false)
private AuditTrail auditTrail;

private static final long serialVersionUID = 1L;

public SchoolGalleryPic() {
    super();
}

public Long getSchoolGalleryPicID() {
    return schoolGalleryPicID;
}

public void setSchoolGalleryPicID(Long schoolGalleryPicID) {
    this.schoolGalleryPicID = schoolGalleryPicID;
}

public BlobField getPic() {
    return pic;
}

public void setPic(BlobField pic) {
    this.pic = pic;
}

public BlobField getThumbnail() {
    return thumbnail;
}

public void setThumbnail(BlobField thumbnail) {
    this.thumbnail = thumbnail;
}

public SchoolGalleryAlbum getSchoolGalleryAlbum() {
    return schoolGalleryAlbum;
}

public void setSchoolGalleryAlbum(SchoolGalleryAlbum schoolGalleryAlbum) {
    this.schoolGalleryAlbum = schoolGalleryAlbum;
}

public Integer getSortOrder() {
    return sortOrder;
}

public void setSortOrder(Integer sortOrder) {
    this.sortOrder = sortOrder;
}

public AuditTrail getAuditTrail() {
    return auditTrail;
}

public void setAuditTrail(AuditTrail auditTrail) {
    this.auditTrail = auditTrail;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result
            + ((auditTrail == null) ? 0 : auditTrail.hashCode());
    result = prime * result + ((pic == null) ? 0 : pic.hashCode());
    result = prime
            * result
            + ((schoolGalleryAlbum == null) ? 0 : schoolGalleryAlbum
                    .hashCode());
    result = prime
            * result
            + ((schoolGalleryPicID == null) ? 0 : schoolGalleryPicID
                    .hashCode());
    result = prime * result
            + ((sortOrder == null) ? 0 : sortOrder.hashCode());
    result = prime * result
            + ((thumbnail == null) ? 0 : thumbnail.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    SchoolGalleryPic other = (SchoolGalleryPic) obj;
    if (auditTrail == null) {
        if (other.auditTrail != null)
            return false;
    } else if (!auditTrail.equals(other.auditTrail))
        return false;
    if (pic == null) {
        if (other.pic != null)
            return false;
    } else if (!pic.equals(other.pic))
        return false;
    if (schoolGalleryAlbum == null) {
        if (other.schoolGalleryAlbum != null)
            return false;
    } else if (!schoolGalleryAlbum.equals(other.schoolGalleryAlbum))
        return false;
    if (schoolGalleryPicID == null) {
        if (other.schoolGalleryPicID != null)
            return false;
    } else if (!schoolGalleryPicID.equals(other.schoolGalleryPicID))
        return false;
    if (sortOrder == null) {
        if (other.sortOrder != null)
            return false;
    } else if (!sortOrder.equals(other.sortOrder))
        return false;
    if (thumbnail == null) {
        if (other.thumbnail != null)
            return false;
    } else if (!thumbnail.equals(other.thumbnail))
        return false;
    return true;
}
}

嵌入式:

@Embeddable
public class BlobField implements java.io.Serializable {

private static final long serialVersionUID = -5111311566516067191L;

@Column(nullable=false, length=100000)
@Lob
@Basic(fetch=FetchType.LAZY)
private byte[] blobField;

public BlobField() {
}

public byte[] getBlobField() {
    return blobField;
}

public void setBlobField(byte[] blobField) {
    this.blobField = blobField;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + Arrays.hashCode(blobField);
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    BlobField other = (BlobField) obj;
    if (!Arrays.equals(blobField, other.blobField))
        return false;
    return true;
}
}

错误:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: MyClassRegister] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:1239)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.access$600(EntityManagerFactoryBuilderImpl.java:120)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:855)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at com.myclassregister.api.login.GetSchoolListActivity.getSchoolList(GetSchoolListActivity.java:30)
at com.myclassregister.model.test.TestGetSchoolListActivity.main(TestGetSchoolListActivity.java:12)
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: com.myclassregister.model.base.event.SchoolGalleryPic column: blobField (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:709)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:731)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:727)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:753)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:506)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1360)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
... 9 more

1 个答案:

答案 0 :(得分:0)

由于您没有明确指定它们当前映射到同一名称blobField的两个blob列的名称。您必须明确指定图片和缩略图的基础数据库列。

我还建议将所有blobField注释移到具体字段。例如,您可能想要懒惰加载一列而另一列急切地加载。

@Column(nullable = false, length = 100000)
@Lob
@Basic(fetch=FetchType.LAZY)
@Embedded
@AttributeOverrides({ @AttributeOverride(name = "blobField", column = @Column(name = "<PICTURE column in DB>")) })
private BlobField pic;

@Column(nullable = false, length = 100000)
@Lob
@Basic(fetch=FetchType.LAZY)
@Embedded
@AttributeOverrides({ @AttributeOverride(name = "blobField", column = @Column(name = "<THUMBNAIL column in DB")) })
private BlobField thumbnail;

在这种情况下,BlobField看起来像这样:

@Embeddable
public class BlobField implements java.io.Serializable {

private static final long serialVersionUID = -5111311566516067191L;

private byte[] blobField;