hibernate注解方式实现一对多映射

本文介绍了一个使用Hibernate框架实现的读者与书籍之间的多对多关联关系案例。通过配置hibernate.cfg.xml文件引入映射类,并定义了Reader与Book两个实体类间的关联关系。最后通过一个简单的测试用例演示了如何保存数据。
摘要由CSDN通过智能技术生成

首先是在hibernate.cfg.xml中引入要映射的class

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=GBK</property>

        <property name="hibernate.connection.username">root</property>

        <property name="hibernate.connection.password">sa</property>

        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="show_sql">true</property>

        <property name="hibernate.hbm2ddl.auto">update</property>

 

        <mapping class="com.sszd.hibernate.Reader"/>

        <mapping class="com.sszd.hibernate.Book"/> 

    </session-factory>

 

 

</hibernate-configuration>

 

我们分别看看reader.java和book.java文件

 

Reader.java

package com.sszd.hibernate;

 

import java.util.HashSet;

import java.util.Set;

 

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.OneToMany;

import javax.persistence.Table;

@Entity

@Table(name="reader")

public class Reader {

 

 @Id

 @Column(name="ID")

 @GeneratedValue(strategy=GenerationType.AUTO)

 private int id;

 

 @Column(name="MENO")

 private String meno;

 

 @Column(name="NAME")  

 private String name;

 

 @OneToMany(mappedBy = "reader", cascade = CascadeType.ALL, fetch = FetchType.LAZY)

 private Set<Book> books = new HashSet<Book>();

 

 public Reader()

 {

 

 }

 

 public Set getBooks() {

  return books;

 }

 

 public void setBooks(Set books) {

  this.books = books;

 }

 

 public int getId() {

  return id;

 }

 

 public void setId(int id) {

  this.id = id;

 }

 

 public String getMeno() {

  return meno;

 }

 

 public void setMeno(String meno) {

  this.meno = meno;

 }

 

 public String getName() {

  return name;

 }

 

 public void setName(String name) {

  this.name = name;

 }

 

 

}

 

 

book.java

package com.sszd.hibernate;

 

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.ManyToOne;

import javax.persistence.Table;

 

@Entity

@Table(name="book")

public class Book {

 

 @Id

 @Column(name="ID")

 @GeneratedValue(strategy=GenerationType.AUTO) 

 private int id;

 

 @Column(name="TITLE")

 private String title;

 

 @ManyToOne(cascade = CascadeType.ALL, optional = false)

 @JoinColumn(name="rea_id", referencedColumnName="id")//外键为sut_id,与student中的id关联

 private Reader reader;

 

 

 

 public int getId() {

  return id;

 }

 

 public Reader getReader() {

  return reader;

 }

 

 public void setReader(Reader reader) {

  this.reader = reader;

 }

 

 public void setId(int id) {

  this.id = id;

 }

 

 public String getTitle() {

  return title;

 }

 

 public void setTitle(String title) {

  this.title = title;

 }

 

}

 

接下来是我们的本地测试用例

package com.sszd.main;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.AnnotationConfiguration;

import com.sszd.hibernate.Book;

import com.sszd.hibernate.Reader;

public class ReaderToBooks {

 

 private static  SessionFactory sessionFactory;

      public static void main(String[] args) {

   sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

 

Session session = sessionFactory.openSession();

Reader r = new Reader();

r.setName("Reader zhang");

Book b1 = new Book();

b1.setTitle("title1");

Book b2 = new Book();

b2.setTitle("title2");

b1.setReader(r);

b2.setReader(r);

r.getBooks().add(b1);

r.getBooks().add(b2);

Transaction trx = session.beginTransaction();

session.save(r);

trx.commit();

session.close();

 

}

 

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值