JPA / Hibernate:“缺少列”当加入专栏时 [英] JPA/Hibernate: "Missing Column" when joining columns

查看:195
本文介绍了JPA / Hibernate:“缺少列”当加入专栏时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JPA或Hibernate不是很熟悉。我正在接管别人的代码,我只是试图让它现在正常工作。鉴于我的理解有限,我会尽量具体,不要淹没在不必要的细节中。



我有一个运行时异常org.hibernate.HibernateException :缺少列:public.sessions中的名称。这是应用程序这部分的基本组织:

  @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小组小组;


}

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

以下是相关表格。模式比这更复杂(也是不可协商的 - 我不能改变我们现有的基线),但是这些是相关的表/列。

 会话
--------
物品名称
群组名称
...



--------
article_name
名称
...

如您所见,组名列的名称在两个表中是不同的。尽管我对JPA不太了解,但我认为使用referencedColumnName属性可以在连接两个不同名称的列时考虑到这种差异。但是,使用此代码运行会产生缺少列:public.sessions中的名称异常。如果我切换第二个@JoinColumn注解(导致name =name,referencedColumnName =group_name)的name和referencedColumnName属性的值,我会得到异常org.hibernate.MappingException:Unable to找到逻辑名为group_name的组。



我在网上发现了很多类似的例子,但是无论出于何种原因,我无法得到这个工作。值得一提的是,这段代码用于工作正常,但正如我们发现的那样,这是因为Hibernate被配置为根据需要更新表(因此,名称列被添加到会话表中)。一旦我们将hbm2ddl.auto属性更改为验证,我们开始出现此错误。因此,我认为问题一直存在,我们没有注意到,因为列已被添加。



非常感谢您的帮助。 referencedColumnName 中的一列。

解决方案 group 在外键中引用的表。并且,因为您具有 group 表中用于作为外键的列的实际名称,所以它是 name



以下是有道理的:

  @JoinColumn(name =group_name,
referencedColumnName =name,

因此,外键列将是 group_name group 表中引用(或用作外键)是 name 。但是当你改变它时,它不起作用,因为那时有 no 列名为 group 表中。


I am not very familiar with JPA or 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." 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. However, running with this code yields the "Missing column: name in public.sessions" exception. 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."

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). Once we changed the "hbm2ddl.auto" property to "validate," we began getting this error. 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. And as you have the actual name of the column in group table that is that is used as foreign key is "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. But when you change it around, it does not work because then there is no column named group_name in your group table.

这篇关于JPA / Hibernate:“缺少列”当加入专栏时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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