数据库:两张表,两者通过publisher_id构成关联关系。
Book(图书表)
CREATE TABLE `t_book` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `price` double DEFAULT NULL, `publisher_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FKkp03xwrur02d7x2nxv1hup6x0` (`publisher_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8
Publisher(出版社表)
CREATE TABLE `t_publisher` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8
数据:
/* SQLyog Ultimate v12.09 (64 bit) MySQL - 5.5.53 : Database - query ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`query` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `query`; /*Table structure for table `t_book` */ DROP TABLE IF EXISTS `t_book`; CREATE TABLE `t_book` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `price` double DEFAULT NULL, `publisher_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FKkp03xwrur02d7x2nxv1hup6x0` (`publisher_id`) ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; /*Data for the table `t_book` */ insert into `t_book`(`id`,`name`,`price`,`publisher_id`) values (1,'云计算技术及性能优化',70,1),(2,'C语言程序设计',31,1),(3,'中国冰雪梦',54,1),(4,'Photoshop图形图像处理',31.84,2),(5,'VisualBasic2015实践教程 ',36,2),(6,'生产微服务 ',44,3),(7,'架构探险:轻量级微服务架构(下册)',63.2,3); /*Table structure for table `t_publisher` */ DROP TABLE IF EXISTS `t_publisher`; CREATE TABLE `t_publisher` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; /*Data for the table `t_publisher` */ insert into `t_publisher`(`id`,`name`) values (1,'电子工业出版社'),(2,'北京大学出版社'),(3,'人民邮电出版社'); /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
项目结构以及所使用的包:
实体类,以及映射文件:
Book.java
package com.gordon.domain; public class Book { private Integer id; private String name; private Double price; private Publisher publisher; 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 Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Publisher getPublisher() { return publisher; } public void setPublisher(Publisher publisher) { this.publisher = publisher; } }
Book.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.gordon.domain.Book" table="t_book"> <id name="id" column="id"> <generator class="native" /> </id> <property name="name" column="name" /> <property name="price" column="price" /> <many-to-one name="publisher" column="publisher_id" class="com.gordon.domain.Publisher"/> </class> </hibernate-mapping>
Publisher.java
package com.gordon.domain; import java.util.HashSet; import java.util.Set; public class Publisher { private Integer id; private String name; private Set<Book> books = new HashSet<Book>(); 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<Book> getBooks() { return books; } public void setBooks(Set<Book> books) { this.books = books; } }
Publisher.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.gordon.domain.Publisher" table="t_publisher"> <id name="id" column="id"> <generator class="native" /> </id> <property name="name" column="name" /> <set name="books"> <key column="publisher_id" /> <one-to-many class="com.gordon.domain.Book" /> </set> </class> </hibernate-mapping>
hibernate核心配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/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/query</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <mapping resource="com/gordon/domain/Book.hbm.xml"/> <mapping resource="com/gordon/domain/Publisher.hbm.xml"/> </session-factory> </hibernate-configuration>
HibernateUtil.java工具类:
package com.gordon.utils; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final Configuration CONFIGURATION; private static final SessionFactory SESSIONFACTORY; static { CONFIGURATION = new Configuration().configure(); SESSIONFACTORY = CONFIGURATION.buildSessionFactory(); }; public static Session getSession() { return SESSIONFACTORY.openSession(); } public static Session getCurrentSession() { return SESSIONFACTORY.getCurrentSession(); } }
开始测试:使用Junit包进行测试。
package com.gordon.test; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import com.gordon.utils.HibernateUtil; public class TestDemo1 { @Test public void run1() { Session session = HibernateUtil.getCurrentSession(); Transaction transaction = session.beginTransaction(); transaction.commit(); } }