码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate单向关联1-N

时间:2015-08-06 13:16:48      阅读:127      评论:0      收藏:0      [点我收藏+]

标签: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。

数据库表:
技术分享

技术分享
技术分享

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

Hibernate单向关联1-N

标签:hibernate   关联   1-n   

原文地址:http://blog.csdn.net/csujiangyu/article/details/47315055

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!