使用jointable和复合键进行多对多映射

时间:2012-09-05 08:38:47

标签: hibernate hibernate-mapping

我在书和作者之间有多对多的映射

@Enity
public class Book{

    private Long id;
    private String name;
    ...

    @Id
    @Column(name="book_id")
    public getId(){
    ...
    }
    @ManyToMany(
    @JoinTable(name="book_author",
        joinColumns = @JoinColumn(name="book_id"),
        inverseJoinColumns = @JoinColumn(name="????")
    )
    public List<Author> getAuthors(){
    ...
    }
}

@Enity
public class Author{
    @EmbeddedId;
    private AuthorPk authorPk;
    ...

}

如何在inverseJoinColumn上进行多对多映射,因为它是一个复合键?

1 个答案:

答案 0 :(得分:5)

inverseJoinColumns = {@JoinColumn(name = "theFirstColumnInTheJoinTable", 
                                  referencedColumnName = "theFirstColumnInTheTargetTable"),
                      @JoinColumn(name = "theSecondColumnInTheJoinTable", 
                                  referencedColumnName = "theSecondColumnInTheTargetTable")}

但我肯定会避免使用复合PK:它们会让生活变得更加复杂,性能会更糟。