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

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

基于外键1-N关联(无连接表)

一个Customer关联多个Card

Customer实体(1端):

package com.ydoing.hibernate4;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.OneToMany;import javax.persistence.Table;@Entity@Table(name = "customer_inf")public class Customer {
@Id @Column(name = "customer_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; @OneToMany(targetEntity = Card.class, cascade = CascadeType.ALL) @JoinColumn(name = "customer_id", referencedColumnName = "customer_id") private Set
cards = 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
getCards() { return cards; } public void setCards(Set
cards) { this.cards = cards; }}

Card实体(N端):

package com.ydoing.hibernate4;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 = "card_inf")public class Card {
@Id @Column(name = "card_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; }}

测试:

package com.ydoing.hibernate4;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.boot.registry.StandardServiceRegistryBuilder;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.junit.BeforeClass;import org.junit.Test;public class Main {    private static Session session;    @BeforeClass    public static void init() {        Configuration conf = new Configuration();        conf.configure();        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties())                .build();        SessionFactory factory = conf.buildSessionFactory(serviceRegistry);        session = factory.openSession();    }    @Test    public void test() {        Transaction tx = session.getTransaction();        tx.begin();        Customer customer = new Customer();        customer.setName("Jack");        Card card1 = new Card();        card1.setName("ICBC");        Card card2 = new Card();        card2.setName("CCB");        customer.getCards().add(card1);        customer.getCards().add(card2);        session.save(customer);        tx.commit();        session.close();    }}

Console输出:

Hibernate:     insert     into        customer_inf        (name)     values        (?)Hibernate:     select        last_insert_id()Hibernate:     insert     into        card_inf        (name)     values        (?)Hibernate:     select        last_insert_id()Hibernate:     insert     into        card_inf        (name)     values        (?)Hibernate:     select        last_insert_id()Hibernate:     update        card_inf     set        customer_id=?     where        card_id=?Hibernate:     update        card_inf     set        customer_id=?     where        card_id=?

从输出不难看出Hibernate主要进行了5个操作。首先向customer_inf插入一条数据,向card_inf插入两条数据。然后两次更新card_inf表。

数据表:

customer_inf:
这里写图片描述

card_inf:

这里写图片描述

有连接表的单向1-N关联

只要改变前面Customer类就行了

package com.ydoing.hibernate4;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.OneToMany;import javax.persistence.Table;@Entity@Table(name = "customer_inf")public class Customer {
@Id @Column(name = "customer_id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; @OneToMany(targetEntity = Card.class, cascade = CascadeType.ALL) // @JoinColumn(name = "customer_id", referencedColumnName = "customer_id") @JoinTable(name = "customer_card_inf", joinColumns = @JoinColumn(name = "customer_id", referencedColumnName = "customer_id"), inverseJoinColumns = @JoinColumn(name = "card_id", referencedColumnName = "card_id", unique = true)) private Set
cards = 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
getCards() { return cards; } public void setCards(Set
cards) { this.cards = cards; }}

Console输出:

Hibernate:     insert     into        customer_inf        (name)     values        (?)Hibernate:     select        last_insert_id()Hibernate:     insert     into        card_inf        (name)     values        (?)Hibernate:     select        last_insert_id()Hibernate:     insert     into        card_inf        (name)     values        (?)Hibernate:     select        last_insert_id()Hibernate:     insert     into        customer_card_inf        (customer_id, card_id)     values        (?, ?)Hibernate:     insert     into        customer_card_inf        (customer_id, card_id)     values        (?, ?)

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

数据库表:

这里写图片描述

这里写图片描述

这里写图片描述

你可能感兴趣的文章
C++语言 填充选区
查看>>
1.Rabbitmq学习记录《本质介绍,协议AMQP分析》
查看>>
mysql安装与基本使用
查看>>
记一次Hbase数据迁移和遇到的问题
查看>>
Axure原型制作规范
查看>>
【UOJ#246】套路(动态规划)
查看>>
Repeater + 分页控件 AspNetPager 研究
查看>>
eclipse中的汉字极小的解决方案(转载)
查看>>
Finereport集群配置
查看>>
Introspector内省和反射的区别.
查看>>
php-fpm配置笔记
查看>>
[转]解读ASP.NET 5 & MVC6系列(7):依赖注入
查看>>
[转]Asp.Net大型项目实践(11)-基于MVC Action粒度的权限管理【续】【源码在这里】(在线demo,全部源码)...
查看>>
[转]VS2010中的单元测试
查看>>
前端面试每日 3+1(每日三问)
查看>>
HC-05底层驱动
查看>>
Ruby on Rails Tutorial 第六章 用户模型
查看>>
Flex与Javascript交互
查看>>
构建之法阅读笔记03
查看>>
IOS https抓包及10.3.3版本证书不生效问题解决
查看>>