将复合键添加到“1”侧后,看到“referencedColumnNames(ID)...未映射到单个属性”错误,具有1-M关系

时间:2013-01-21 08:31:58

标签: jpa key composite

我有一个现有的JPA实体(“Reference”),其ID列是它的主键,它继承自基类“BaseEntity”(使用超类上的@MappedSuperclass注释)。

我在Reference和另一个名为Violation的实体之间也有1-M关系。先前使用外键“REFERENCE_ID”定义了违规,并将其引用到参考实体的“ID”列。

最近,我尝试将一个不相关的复合键添加到Reference实体。这不应该影响参考和违规之间的1-M关系。但是,当我在tomcat服务器中运行代码时,我看到以下堆栈跟踪:

产生的原因:org.hibernate.AnnotationException:referencedColumnNames(ID)的org.qcri.copydetection.sdk.metastore.entity.Violation.reference引用org.qcri.copydetection.sdk.metastore.entity.Reference未映射到单一财产         在org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:205)〜[hibernate-annotations-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:110)〜[hibernate-annotations-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:541)〜[hibernate-annotations-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:523)〜[hibernate-annotations-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:380)〜[hibernate-annotations-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1206)〜[hibernate-core-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1459)〜[hibernate-entitymanager-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193)〜[hibernate-entitymanager-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1086)〜[hibernate-entitymanager-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:685)〜[hibernate-entitymanager-3.5.6-Final.jar:3.5.6-Final]         在org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)〜[hibernate-entitymanager-3.5.6-Final.jar:3.5.6-Final]         在org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:268)〜[弹簧ORM-3.1.2.RELEASE.jar:3.1.2.RELEASE]         在org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)〜[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)〜[弹簧豆-3.1.2.RELEASE.jar:3.1.2.RELEASE]         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)〜[弹簧豆-3.1.2.RELEASE.jar:3.1.2.RELEASE]         ...省略了39个常见帧

以下是涉及的3个类的代码:

@Entity
@Table(name = "REFERENCE")
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
@IdClass(Reference.ContextualName.class)
public class Reference extends BaseEntity {

@Column(name= "LOCATION", unique=true)
@XmlElement
private String location;

@Id
@AttributeOverrides({
    @AttributeOverride(name = "name", column = @Column(name = "NAME")),
    @AttributeOverride(name = "account", column = @Column(name = "ACCOUNT_ID"))
})

@Column(name = "NAME")
@XmlElement
private String name;

@ManyToOne(optional=false)
@XmlTransient
@JoinColumn(name = "ACCOUNT_ID", referencedColumnName = "ID")
private Account account;


public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public Reference() {}

public Reference(String name) {
    setName(name);
}

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

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

public Account getAccount() {
    return this.account;
}

public void setAccount(Account account) {
    this.account = account;
}

@Embeddable
private class ContextualName implements Serializable {
    private static final long serialVersionUID = -3687389984589209378L;

    @Basic(optional = false)
    @Column(name = "NAME")
    @XmlElement
    private String name;

    @ManyToOne(optional=false)
    @XmlTransient
    @JoinColumn(name = "ACCOUNT_ID", referencedColumnName = "ID")
    private Account account;

    ContextualName() {}
}
}

@MappedSuperclass
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
@XmlElement
private Long id;

@Basic(optional = true)
@Column(name = "CREATED", insertable = false, updatable = false, columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@Temporal(TemporalType.TIMESTAMP)
@XmlElement
private Date creationDate;

protected BaseEntity() {}

public Long getId() {
    return id;
}

public void setId(Long id) {
    if(this.id==null) {
        this.id = id;
    } else if (this.id!=id) {
        throw new IllegalArgumentException("Cannot change the id after it has been set, as it is a generated field.");
    }
}

public Date getCreationDate() {
    return creationDate;
}

public void setCreationDate(Date creationDate) {
    if(this.creationDate==null) {
        this.creationDate = creationDate;
    } else if (this.creationDate!=creationDate) {
        throw new IllegalArgumentException("Cannot change the creation-date after it has been set, as it is a generated field.");
    }
}
}

@Entity
@Table(name = "VIOLATION")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Violation extends BaseEntity {

@ManyToOne (optional=false, fetch= FetchType.EAGER)
@JoinColumn(name = "REFERENCE_ID", referencedColumnName = "ID")
private Reference reference;

@ManyToOne (optional=false, fetch= FetchType.EAGER)
@JoinColumn(name = "SUSPECT_ID", referencedColumnName = "ID")
private Suspect suspect;

@ManyToOne (optional=false, fetch= FetchType.EAGER)
@XmlTransient
@JoinColumn(name = "SEARCH_ID", referencedColumnName = "ID")
private Search search;

@Basic(optional = false)
@Column(name = "SCORE")
@XmlElement
private double score;

public Violation() {}

public Violation(Search search, Reference ref, Suspect sus, double score) {
    this.search = search;
    this.reference = ref;
    this.suspect = sus;
    this.score = score;
}

public double getScore() {
    return score;
}

public void setScore(double score) {
    this.score = score;
}

public Reference getReference() {
    return reference;
}

public void setReference(Reference reference) {
    this.reference = reference;
}

public Suspect getSuspect() {
    return suspect;
}

public void setSuspect(Suspect suspect) {
    this.suspect = suspect;
}

public Search getSearch() {
    return search;
}

public void setSearch(Search search) {
    if(this.search!=null && this.search!=search) {
        this.search.removeViolation(this);
    }
    this.search = search;
    if(search!=null) {
        if(!search.getViolations().contains(this)) {
            search.addViolation(this);
        }
    }
}
}

简而言之,我完全不知道如何将复合键添加到已有ID列的现有(旧版)实体。我无法删除ID列,也无法更改引用和违规之间的1-M关系。我无法理解错误消息,因为Violation实体的“REFERENCE_ID”外键列被映射到Reference实体的单个“ID”列。

非常感谢提前!

0 个答案:

没有答案