简体   繁体   中英

How to control JPA Column names in Map

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM