Description
According to JPA v2 specification page 50, the joining table name should be composed from entity names (e.g. class names) by default. The table should have two columns.
Table name:
- owner class name
- underscore "_"
- inverse class name.
Name of the column with inverses ids:
- the name of the relationship property or field of the owner
- underscore "_"
- the name of the primary key column in table inverse.
If the annotated property maps data into the map, OpenJPA assumes different database structure.
OpenJPA table name:
- owner class name <- OK
- underscore "_" <- OK
- the name of the relationship property or field of the owner <- WRONG
OpenJPA column with inverses id name:
- "element_id" <- WRONG
Name of the column with owners ids is correct.
Extract from the specification:
"Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
There is a join table that is named A_B (owner name first). This join table has two foreign key
columns. One foreign key column refers to table A and has the same type as the primary key of
table A. The name of this foreign key column is formed as the concatenation of the following:
the name of entity A; "_"; the name of the primary key column in table A. The other foreign
key column refers to table B and has the same type as the primary key of table B and there is a
unique key constraint on it. The name of this foreign key column is formed as the concatenation
of the following: the name of the relationship property or field of entity A; "_"; the name
of the primary key column in table B."
Example entities:
@Entity
public class MapOneToManyOwner {
@Id
private long id;
@OneToMany
@MapKey(name="mapKey")
private Map<String, MapOneToManyInverse> inverses;
}
@Entity
public class MapOneToManyInverse {
@Id
private long id;
private String mapKey;
}
Expected table name: MapOneToManyOwner_MapOneToManyInverse
OpenJPA table name: MapOneToManyOwner_inverses
Expected column name: inverses_id
OpenJPA column name: element_id
DB structure that works with JPA:
CREATE TABLE MapOneToManyOwner_inverses (
MapOneToManyOwner_id INT NOT NULL,
element_id INT NOT NULL,
UNIQUE (element_id)
);
Note: this happens only if the annotated property is a map. If it is a collection, join table structure is different.