无法找到逻辑名称为@embeddedid的列 [英] unable to find column with logical name of @embeddedid

查看:146
本文介绍了无法找到逻辑名称为@embeddedid的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用hibernate和JPA的Spring MVC应用程序中,我试图为一个实体建立映射,该实体的基础数据表具有一个两列的主键. 如何更改下面的代码以使其正常工作?

In a Spring MVC app using hibernate and JPA, I am trying to set up a mapping for an entity whose underlying data table has a two-column primary key. How can I alter my code below to get it to work?

我创建了一个名为conceptPK的EmbeddedId,但是出现以下错误消息:

I created an EmbeddedId called conceptPK, but I am getting the following error message:

Caused by: org.hibernate.MappingException: 
Unable to find column with logical name: conceptPK 
in org.hibernate.mapping.Table(sct2_concept) and its related supertables and secondary tables

在实体类中,我使用以下代码设置主键:

In the entity class, I set up the primary key with the following code:

@EmbeddedId
@AttributeOverrides({
    @AttributeOverride(name="id", column=@Column(name="id")),
    @AttributeOverride(name="effectiveTime", column=@Column(name="effectiveTime"))
})
private ConceptPK conceptPK;

嵌入式的ConceptPK类如下:

The embedded ConceptPK class is as follows:

@Embeddable
class ConceptPK implements Serializable {
    @Column(name="id", nullable=false)
    protected BigInteger id;

    @Column(name="effectiveTime", nullable=false)
    @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    private DateTime effectiveTime;

    /** getters and setters **/
    public DateTime getEffectiveTime(){return effectiveTime;}
    public void setEffectiveTime(DateTime ad){effectiveTime=ad;}

    public void setId(BigInteger id) {this.id = id;}
    public BigInteger getId() {return id;}
}

为了便于阅读,我将完整的代码和完整的堆栈跟踪信息上传到了文件共享站点,而不是在此处创建过长的帖子.

For easier reading, I have uploaded the complete code and the complete stack trace to a file sharing site rather than create an excessively long posting here.

您可以在文件共享站点上单击此链接,以获取the class above的完整代码. .

You can read the complete code for the class above at the file sharing site by clicking on this link.

您可以在此链接上阅读引用第一类的a second class的代码 .

You can read the code for a second class that references the first class at this link.

您可以在此链接上阅读引用第一类 的代码 .

You can read the code for a third class that references the first class at this link.

通过点击此链接,您可以阅读SQL code创建基础数据表的SQL code .

You can read the SQL code that creates the underlying data tables by clicking on this link.

您可以在文件共享站点单击此链接来阅读完整的堆栈跟踪信息.

You can read the complete stack trace at the file sharing site by clicking on this link.

推荐答案

问题是SnomedDescription实体与SnomedConcept@ManyToOne关联.

首先,这不是ManyToOne,但实际上是OneToOne,因为它们的主键是相同的.

For a start, this is not a ManyToOne, but in fact a OneToOne, as their primary keys are identical.

但是主要的问题是,您在@JoinColumn注释中为此关联使用的列不作为数据库列存在,因此这将永远行不通.

But the main problem is that the columns you've used in the @JoinColumn annotation for this association do not exist as database columns, so this will never work.

相反,您应该具有如下的连接列:

Instead, you should have join columns as follows:

@ManyToOne
@JoinColumns({
    @JoinColumn(name="id", referencedColumnName="id"),
    @JoinColumn(name="effectiveTime", referencedColumnName="effectiveTime")
})
private SnomedConcept concept;

现在,您可以继续使用@ManyToOne进行这种关系,但是实际上您应该为两个(ConceptPK)使用相同的嵌入式PK,然后SnomedDescription看起来更像:

Now, you can carry on using the @ManyToOne for this relationship, but really you should be using the same embedded PK for both (ConceptPK), and then SnomedDescription will look more like:

@Entity
@Table(name = "accesslogs")
public class SnomedDescription {

    @EmbeddedId
    private ConceptPK descriptionPK;

    @Column(name="active")
    private boolean active;

    @Column(name="moduleId")
    private BigInteger moduleid;

    @OneToOne
    @PrimaryKeyJoinColumn
    private SnomedConcept concept;

    ..... etc.

是的,您可以在多个实体中使用可嵌入的相同PK.

Yes, you can use the same PK embeddable in multiple Entities.

如果 一对一的关系,则SnomedConceptSnomedDescription的关联也应该是一对一的,其中@PrimaryKeyJoinColumn.

If it is a one-to-one relationship, then the association in SnomedConcept to SnomedDescription should also be one-to-one, with @PrimaryKeyJoinColumn.

如果两者之间的关联是可选的,则始终存在的边"应将一对一定义为@OneToOne(optional=true).换句话说,如果始终有一个概念,但不总是有描述,则概念中的一对一应定义为@OneToOne(optional=true).

If the association between the two is optional, then the 'side' that is always there should define the one-to-one as @OneToOne(optional=true). In other words, if there is always a Concept, but not always a Description, then the one-to-one in Concept should be defined as @OneToOne(optional=true).

这篇关于无法找到逻辑名称为@embeddedid的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆