3

I may just be misunderstanding how @JoinColumns work, but I'm getting an error when I reuse the name attribute. But doesn't the name attribute map to a database column? Shouldn't I be able to reuse it?

There error I'm getting:

Repeated column in mapping for entity: data.model.DP column: division

The code:

@Column(name = "division", nullable = false)
private String division;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumns({
    @JoinColumn(name = "division_labeldefintion", referencedColumnName = "labeldefinition"),
    @JoinColumn(name = "division", referencedColumnName = "abbr")
})
private LabelFile divisionLabel;

What I'm trying to do is do a multi-column join from one table (which contains the division column, and join it with the LabelFile table.

Hanna
  • 10,315
  • 11
  • 56
  • 89

2 Answers2

14

If you want to reuse a column in a mapping you should mark one as insertable=false updatable=false, for Hibernate to know which Java reference value is the relevant one.

More information on when to use those attributes is available in this question.

Community
  • 1
  • 1
juanignaciosl
  • 3,435
  • 2
  • 28
  • 28
1

When using the JoinColumn annotation you must be aware that depending on the type of association between two tables/entities the attributes "name" and "referencedColumnName" are changing the place where they referencing to. Check the JPA API, or look here for more detailed description.

In your case the "name" attribute of JoinColumn on "divisonLabel" property is referencing to the source table/entity. And "referencedColumnName" is referencing to the columns from LabelFile table/entity. So, you are trying to have two "division" columns in the source table/entity.

To have the mapping work switch values for "name" and "referencedColumnName".

deyo.vuk
  • 180
  • 1
  • 3
  • 11
  • Won't switching them change which table I'm grabbing from? I want to have a "division" column AND use "division" in my join and use it to match up to "abbr" which is also in the LabelFile. – Hanna Sep 13 '12 at 22:13
  • It will, and that's the point. The way you wrote it now the JPA/Hibernate is trying to create two columns named "division"; one for plain text field that has nothing to do with any associations; the second to be used as part of composite key. Annotation above LabelFile property is creating new fields/columns inside the current entity/table. note: try not to think in form of columns&tables, JPA and ORM are there to hide the DB from you and make you work with objects instead. disclaimer: I'm typing this without writing any code, so it might be that I'm overlooking something. hopefully not. – deyo.vuk Sep 13 '12 at 22:43