LousyG
LousyG

Reputation: 177

JPA/Hibernate: "Missing Column" when joining columns

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.

Upvotes: 3

Views: 4245

Answers (1)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

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.

Upvotes: 2

Related Questions