Hibernate单向关联1-N

基于外键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<Card> 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<Card> getCards() {
        return cards;
    }
    public void setCards(Set<Card> 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<Card> 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<Card> getCards() {
        return cards;
    }
    public void setCards(Set<Card> 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。

数据库表:

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

时间: 2024-10-31 09:27:26

Hibernate单向关联1-N的相关文章

Hibernate单向关联N-1

无连接表的N-1关联 多个Employee关联一个Department. Employee实体(N端): package com.ydoing.hibernate2; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.G

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;

Hibernate单向关联1-1

基于外键的单向1-1关联 一个House关联一个Owner. House类: package com.ydoing.hibernate3; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Generation

一口一口吃掉Hibernate(五)——一对多单向关联映射

版权声明:本文为博主原创文章,未经博主允许不得转载.如需转载请声明:[转自 http://blog.csdn.net/xiaoxian8023 ] 在上一篇博客<一口一口吃掉Hibernate(四)--多对一单向关联映射>中,介绍了多对一的关联映射,今天就反过来说一下一对多的单向关联映射. 可能有人会对这2篇博客的题目有点混淆不清,跟日常说的关系有点不同.我们日常说的比如父子关系,夫妻关系都是说的双向关系,而现在讨论的则是单向关系,所以也就有了多对一和一对多的说法. 二者的关系其实很简单,只是

Hibernate 一对多单向关联Demo

以Classes[班级]和Student[学生]为例的Demo Classes .java public class Classes implements Serializable { private long Cid; private String Cname; private String Cdesc; private Set<Student> students; //get和set } Student .java public class Student implements Seria

【Hibernate步步为营】--单向关联一对一映射(一)

上篇文章对多对一的关联映射做了详细的分析,它在实现上可以有两种方式,并且这两种方式实现也很简单,关键是标签<many-to-one>的使用,它分别指明了多端和一端的映射关系,这种映射关系既是对象模型中的聚合关系.接下来继续讨论关联映射. 一.唯一外键 唯一外键说的是数据库表中的每一行的外键唯一对应着另一张表中的主键,也就是说一个表的主键作为另一张表的外键,并且它们之间的关系是唯一的,这种反应到关系模型中如下图所示: 上图的两个实体表,分别为人和身份证,很明显的一个人对应着一个身份证.身份证作为

Hibernate之关于多对多单向关联映射

[Hibernate]之关于多对多单向关联映射 老师和学生,最典型的多对多关联, Teacher和Student.所谓单向意思就是说.老师知道自己的教的是哪些学生而学生不知道是哪些老师教. 也能够这么说,在查询的时候,通过老师能够级联查询出学生,可是通过学生不能够级联查询出老师. 而多对多最麻烦的是怎么自己定义我们中间表的,表名和列名,这个是重要的! Annotations配置 @Entity @Table(name="t_teacher") publicclass Teacher {

hibernate之多对一单向关联

一个工作组(Group)里可以有多个用户(User),一个User只属于一个Group,这是典型的多对一的关系.在多对一的关系中正确的数据库设计是在多的这方(在这里是User这方)加一个Group的外键.如果数据库设计的与之相反就会产生冗余,请看下面这个例子: 友情提示:这是错误的设计方法: GroupId GroupName UserId 1 Group_1 1 1 Group_1 2 UserId UserName 1 moluo 2 xingzhe 这样在一的这方(也就是Group这方)设

Hibernate ORM框架——续第二章:Hibernate映射关系:单向关联

一:课堂笔记 **********单表映射*************** 1.只有一个实体类映射一个表 2.有一个实体类+ 一个或者多个值类型,合在一起 映射为一个表 **********多表映射*************** 定义:类有多个映射为多个表 **********数据库层面,如ORM无关************** 2个表之间要建立关系?该如何做?有多少种方法 假定有2个表,分别为A,B 3种关系 a)主键关联:A表的主键对应B表的主键,(一对一的关系) 在数据库种如何建立这种关系?