JPA 多对多 @ManyToMany

一本书可以有多个作者,一个作者可以有多本书

@Entity
public class Book {
    @Id
    String isbn;
    String title;
    @ManyToMany(mappedBy = "books")
    Collection<Author> authors;
}
@Entity
public class Author {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增
    Long id;
    String name;
    @ManyToMany
    Collection<Book> books;
}

结果

[Hibernate] 
    create table Author (
        id bigint not null auto_increment,
        name varchar(255),
        primary key (id)
    ) engine=InnoDB
[Hibernate] 
    create table Author_Book (
        authors_id bigint not null,
        books_isbn varchar(255) not null
    ) engine=InnoDB
[Hibernate] 
    create table Book (
        isbn varchar(255) not null,
        title varchar(255),
        primary key (isbn)
    ) engine=InnoDB
[Hibernate] 
    alter table Author_Book 
       add constraint FKs06974x2wwb8wjb7mpbrjucj5 
       foreign key (books_isbn) 
       references Book (isbn)
[Hibernate] 
    alter table Author_Book 
       add constraint FKsxycm9ny3ln3h2r2rflhutxss 
       foreign key (authors_id) 
       references Author (id)

使用 @JoinTable 手动设置关联信息

@Entity
public class Book {
    @Id
    String isbn;
    String title;
    @ManyToMany(mappedBy = "books")
    Collection<Author> authors;
}
@Entity
public class Author {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) // 自增
    Long id;
    String name;
    @ManyToMany
    @JoinTable(name = "Author_Book",
            joinColumns = {@JoinColumn(name = "authors_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "books_isbn", referencedColumnName = "isbn")}
    )
    Collection<Book> books;
}

结果

[Hibernate] 
    create table Author (
        id bigint not null auto_increment,
        name varchar(255),
        primary key (id)
    ) engine=InnoDB
[Hibernate] 
    create table Author_Book (
        authors_id bigint not null,
        books_isbn varchar(255) not null
    ) engine=InnoDB
[Hibernate] 
    create table Book (
        isbn varchar(255) not null,
        title varchar(255),
        primary key (isbn)
    ) engine=InnoDB
[Hibernate] 
    alter table Author_Book 
       add constraint FKs06974x2wwb8wjb7mpbrjucj5 
       foreign key (books_isbn) 
       references Book (isbn)
[Hibernate] 
    alter table Author_Book 
       add constraint FKsxycm9ny3ln3h2r2rflhutxss 
       foreign key (authors_id) 
       references Author (id)

 

测试

src/main/resources/META-INF/persistence.xml

<persistence xmlns="https://jakarta.ee/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
             version="3.0">
    <persistence-unit name="persistenceUnitName">
        <properties><!-- org.hibernate.cfg.JdbcSettings -->
            <property name="jakarta.persistence.jdbc.user" value="root"/>
            <property name="jakarta.persistence.jdbc.password" value="root"/>
            <property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://127.0.0.1:3306/jpa"/>
            <property name="jakarta.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>

            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.highlight_sql" value="true"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>
    </persistence-unit>
</persistence>

Main

Persistence.createEntityManagerFactory("persistenceUnitName");
// 对于关联的集合对象, 默认使用懒加载的策略
// 使用维护关联关系的一方获取, 还是使用不维护关联关系的一方获取, SQL 语句相同

 


https://docs.jboss.org/hibernate/orm/current/introduction/html_single/Hibernate_Introduction.html#many-to-many

https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#associations-many-to-many

posted @ 2019-02-12 18:56  江湖小小白  阅读(537)  评论(0编辑  收藏  举报