单向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。

数据库表:





最新文章

  1. 微信小程序开发视频教程新鲜出炉
  2. 使用idea15搭建基于maven的springmvc-mybatis框架
  3. Bootstrap&lt;基础四&gt; 代码
  4. GOOGLE地图坐标拾取方法、GOOGLE地图获取坐标方法
  5. Process 执行shell 脚本
  6. Java Socket(2): 异常处理
  7. MapReduce在Map端的Combiner和在Reduce端的Partitioner
  8. 数据结构(树链剖分):BZOJ 4034: [HAOI2015]T2
  9. openssl AES加密算法API的使用示例
  10. 四个常用.NET的SqlHelper的方法
  11. URL压缩算法的短地址
  12. Windows Text Copyer 1.1绿色版
  13. 篇一:eclipse创建maven工程
  14. [UWP]附加属性2:实现一个Canvas
  15. Python 妙用heapq
  16. Floor报错原理分析
  17. PHP算法之冒泡排序
  18. jasperreports+IReport 5.56,集成到Spring MVC4.0案例
  19. Lua学习之加载其他lua文件
  20. datagrip离线安装驱动jar

热门文章

  1. apt-get常见错误
  2. 循环神经网络(RNN, Recurrent Neural Networks)——无非引入了环,解决时间序列问题
  3. AIX的系统备份
  4. 数据库表结构导出sql语句
  5. 关于HTTPS通信机制的笔记
  6. webpack入门 --初级压缩
  7. python课程设计笔记(五) ----Resuests+BeautifulSoup (爬虫入门)
  8. video相关简单的api
  9. C#中null、&quot;&quot;、string.empty区别
  10. springmvc 中配置aop