博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hibernate单向关联N-N
阅读量:7287 次
发布时间:2019-06-30

本文共 2756 字,大约阅读时间需要 9 分钟。

单向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
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
getProducts() { return products; } public void setProducts(Set
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。

数据库表:

这里写图片描写叙述
这里写图片描写叙述
这里写图片描写叙述

你可能感兴趣的文章
HDU 4442 Physical Examination【水题】【思维题】
查看>>
NET 命令 常用方法!
查看>>
我的友情链接
查看>>
memcached
查看>>
谁搞死了你的软件?
查看>>
Promise 对象
查看>>
Windows快速添加IP地址
查看>>
AS3.0 事件流
查看>>
“将截断字符串或二进制数据。语句已终止……”问题的解决
查看>>
红苹果IP代理软件 v6.2
查看>>
Centos5.x & Centos6.x 使用mail命令发邮件以及如何伪造发件人
查看>>
JavaScript系列:ECMAScript原始类型
查看>>
centos反编译APK包
查看>>
CSS系列:CSS中盒子的浮动与定位
查看>>
windows 用户用户组迁移
查看>>
Linux系统扩充2
查看>>
linux新手的心得
查看>>
我的友情链接
查看>>
zabbix表字段类型和value type问题
查看>>
shoususaiBti
查看>>