简体   繁体   English

JPA / Hibernate:加入列时“缺少列”

[英]JPA/Hibernate: “Missing Column” when joining columns

I am not very familiar with JPA or Hibernate. 我对JPA或Hibernate不太熟悉。 I'm taking over someone else's code and I'm merely trying to get it to work right now. 我正在接管其他人的代码而我现在只是想让它工作。 Given my limited understanding, I'll try to be as specific as possible without drowning in unnecessary detail. 鉴于我的理解有限,我会尝试尽可能具体,不要淹没不必要的细节。

I've got a runtime exception of "org.hibernate.HibernateException: Missing column: name in public.sessions." 我有一个运行时异常“org.hibernate.HibernateException:Missing column:name in public.sessions。” Here's the basic organization of this part of the application: 以下是该部分应用程序的基本组织:

@Entity
@Table(name = "sessions")
public class Session {
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name = "article_name",
            referencedColumnName = "article_name",
            insertable = false, updatable = false),
        @JoinColumn(name = "group_name",
            referencedColumnName = "name",
            insertable = false, updatable = false) })
    private Group group;

    ...
}

@Entity
@Table(name = "groups")
public class Group {
    ...
}

And here are the relevant tables. 以下是相关表格。 The schema is a lot more complex than this (also non-negotiable - I can't change our existing baseline), but these are the tables/columns that are relevant. 模式比这复杂得多(也是不可协商的 - 我不能改变现有的基线),但这些是相关的表/列。

sessions
--------
article_name
group_name
...


groups
--------
article_name
name
...

As you can see, the name of the group name column is different in the two tables. 如您所见,两个表中的组名列的名称不同。 While I don't know much about JPA, I thought using the "referencedColumnName" property allowed you to account for this difference when joining two differently-named columns. 虽然我对JPA知之甚少,但我认为使用“referencedColumnName”属性允许您在连接两个不同命名的列时考虑这种差异。 However, running with this code yields the "Missing column: name in public.sessions" exception. 但是,使用此代码运行会产生“缺少列:public.sessions中的名称”异常。 If I switch the values of the "name" and "referencedColumnName" properties for the second @JoinColumn annotation (resulting in name = "name", referencedColumnName = "group_name"), I get the exception "org.hibernate.MappingException: Unable to find column with logical name: group_name in groups." 如果我为第二个@JoinColumn注释切换“name”和“referencedColumnName”属性的值(导致name =“name”,referencedColumnName =“group_name”),我得到异常“org.hibernate.MappingException:Unable to查找具有逻辑名称的列:group_name in groups。“

I've found quite a few examples across the web that appear to be doing something similar, but for whatever reason, I can't get this working. 我在网上发现了很多似乎做类似事情的例子,但无论出于何种原因,我都无法做到这一点。 For what it's worth, this code used to work OK, but as we found out, it's because Hibernate was configured to update the table as needed (so, the "name" column was being added to the sessions table). 对于它的价值,这段代码以前的工作正常,但正如我们所知,这是因为Hibernate被配置为根据需要更新表(因此,“name”列被添加到sessions表中)。 Once we changed the "hbm2ddl.auto" property to "validate," we began getting this error. 一旦我们将“hbm2ddl.auto”属性更改为“validate”,我们就开始收到此错误。 Because of this, I think the problem always existed, we just didn't notice it because the column was being added. 因此,我认为问题始终存在,我们只是没有注意到它,因为列正在添加。

Thanks in advance for any and all help. 在此先感谢您的帮助。

The referencedColumnName is the column in the group table that is referenced in the foreign key. referencedColumnName是外键中引用的group表中的列。 And as you have the actual name of the column in group table that is that is used as foreign key is " name ". 并且因为您在group表中具有用作外键的列的实际名称是“ name ”。

The following makes sense: 以下是有道理的:

@JoinColumn(name = "group_name",
            referencedColumnName = "name",

because, then the name of the foreign key column will be group_name and the name of the column in the group table that is referenced (or used as foreign key) is name . 因为,那么外键列的名称将是group_name并且group表中引用(或用作外键)的列的namename But when you change it around, it does not work because then there is no column named group_name in your group table. 但是当你改变它时,它不起作用,因为在你的group表中没有名为group_name列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM