`
Ydoing
  • 浏览: 100949 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Hibernate单向关联N-N

 
阅读更多

单向N-N关联必须使用连接表。

Company实体:

package com.ydoing.hibernate5;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "company_inf")
public class Company {
    @Id
    @Column(name = "company_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    @ManyToMany(targetEntity = Product.class, cascade = CascadeType.ALL)
    @JoinTable(name = "company_product", joinColumns = @JoinColumn(name = "company_id", referencedColumnName = "company_id"), inverseJoinColumns = @JoinColumn(name = "product_id", referencedColumnName = "product_id"))
    private Set<Product> products = new HashSet<>();
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Set<Product> getProducts() {
        return products;
    }
    public void setProducts(Set<Product> products) {
        this.products = products;
    }
}

Product实体:

package com.ydoing.hibernate5;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "product_inf")
public class Product {
    @Id
    @Column(name = "product_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Console输出:

Hibernate: 
    insert 
    into
        company_inf
        (name) 
    values
        (?)
Hibernate: 
    select
        last_insert_id()
Hibernate: 
    insert 
    into
        product_inf
        (name) 
    values
        (?)
Hibernate: 
    select
        last_insert_id()
Hibernate: 
    insert 
    into
        product_inf
        (name) 
    values
        (?)
Hibernate: 
    select
        last_insert_id()
Hibernate: 
    insert 
    into
        company_product
        (company_id, product_id) 
    values
        (?, ?)
Hibernate: 
    insert 
    into
        company_product
        (company_id, product_id) 
    values
        (?, ?)

从输出不难看出,Hibernate创建了连接表company_product。

数据库表:
这里写图片描述
这里写图片描述
这里写图片描述

<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>

版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics