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。

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值