Reputation: 65
The entity Construction
has following map:
@Column(name = "paper_FK")
@ManyToMany
@JoinTable(inverseForeignKey = @ForeignKey(name = "construction_FK"), joinColumns = @JoinColumn(name = "construction_FK", referencedColumnName = "construction_FK"), name = "ConstructionPaperTracks", inverseJoinColumns = @JoinColumn(name = "paper_FK"))
private HashMap<Integer, Paper> tracks_field = new HashMap<Integer, Paper>();
Due to my database design guidelines the table ConstructionPaperTracks should have the columns construction_FK, position and paper_FK.
JPA works with construction_id, position and paper_id.
How can i specify the column names?
best regards
Heiko
Upvotes: 1
Views: 3631
Reputation: 23246
I am not sure I understand "JPA works with construction_id, position and paper_id."
Anyway, I believe the mapping will be as below:
@Entity
public class Construction{
@Id
@Column(name = "construction_id")
//specify a generation strategy
private Long id;
@ManyToMany
@JoinTable(name = "ConstructionPaperTracks",
joinColumns = @JoinColumn(name = "construction_FK"),
inverseJoinColumns = @JoinColumn(name = "paper_FK"))
@MapKeyColumn(name = "position")
private HashMap<Integer, Paper> paper;
}
You need to specify the @MapKeyColumn
, the documentation for which states that:
If the map key is for a ManyToMany entity relationship or for a OneToMany entity relationship using a join table, the map key column is in a join table
For the joinColumn and inverseJoinColumn the referencedColumn names will default to the primary key column of the referenced tables (construction_id, paper_id) so you don't have to specify these.
Upvotes: 4