最近在看hibernate,看到了实体关联关系这一部分内容,说实话,这部分内容我花了很多时间才理解(当然也可能保证完全理解)。这篇博客主要讲解多对一单向关联关系的理解与实践,我们开始吧!
hibernate中多对一单向关联关系主要是对应数据库中的多对一的关系,其实这是一种非常常见的关系,这里以生产商(factory)和产品(product)为例进行讲解。
【以下是配置hibernate多对一单向关联关系的详细步骤与代码,本人是初学者,这可能对初学者来说比较容易入手,也希望能让更多像我一样的初学者更快理解和入手。】
使用开发工具:
IDE:eclipse
数据库:MySql
【以下使用的源码下载地址:http://download.youkuaiyun.com/detail/liu_005/9162581】
1、建表:(方法一)
这里我们用sql语句建表(下面有另外的方法,可以不使用sql语句建表),当然建立数据库的语句就不在这里写了,你自己随便建个数据库就行(记得等下在hibernate.cfg.xml中修改成对应数据库就行),sql语句如下:
生厂商表
DROP TABLE IF EXISTS `tab_factory`;
CREATE TABLE `tab_factory` (
`factoryId` int(11) NOT NULL AUTO_INCREMENT,
`factoryName` varchar(45) DEFAULT NULL,
PRIMARY KEY (`factoryId`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=latin1;
产品表
DROP TABLE IF EXISTS `tab_product`;
CREATE TABLE `tab_product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`price` double NOT NULL,
`factory_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=latin1;
2、数据表建完后,我们开始编辑配置文件:
(1)编写hibernate全局配置文件
<?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.cache.use_second_level_cache">true</property>
<!-- 指定缓存产品提供商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 数据库驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库连接的URL -->
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<!-- 数据库连接用户名 -->
<property name="connection.username">root</property>
<!-- 数据库连接密码 -->
<property name="connection.password">123</property>
<!-- Hibernate方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印SQL语句 -->
<property name="show_sql">true</property>
<!-- 映射文件 -->
<mapping resource="com/mr/product/Product.hbm.xml"/>
<mapping resource="com/mr/factory/Factory.hbm.xml"/>
</session-factory>
</hibernate-configuration>
【提醒】请修改相应的数据库驱动、URL、用户名以及密码
(2)定义HibernateInitialize类
这个类主要是初始化sessionFactory和定义获取session的方法,主要是为了后面使用方便。
package com.mr.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate初始化类
*
*/
public class HibernateInitialize {
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();// ThreadLocal对象
private static SessionFactory sessionFactory = null;// SessionFactory对象
// 静态块
static {
try {
// 加载Hibernate配置文件
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
/**
* 获取Session
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);
}
return session;
}
/**
* 重建会话工厂
*/
public static void rebuildSessionFactory() {
try {
// 加载Hibernate配置文件
Configuration cfg = new Configuration().configure();
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err.println("创建会话工厂失败");
e.printStackTrace();
}
}
/**
* 获取SessionFactory对象
*
* @return SessionFactory对象
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* 关闭Session
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);//
if (session != null) {
session.close();// 关闭Session
}
}
}
(3) 定义【生产商】类
package com.mr.factory;
public class Factory {
private Integer factoryId;// 生产商的id
private String factoryName;// 生产商名称
public Integer getFactoryId() {
return factoryId;
}
public void setFactoryId(Integer factoryId) {
this.factoryId = factoryId;
}
public String getFactoryName() {
return factoryName;
}
public void setFactoryName(String factoryName) {
this.factoryName = factoryName;
}
}
(4)编写【生产商】配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 产品信息字段配置信息 -->
<hibernate-mapping>
<class name="com.mr.factory.Factory" table="tab_factory">
<!-- id值 -->
<id name="factoryId" column="factoryid" type="int">
<generator class="native"/>
</id>
<!-- 生产商名称 -->
<property name="factoryName" type="string" length="45">
<column name="factoryname"/>
</property>
</class>
</hibernate-mapping>
【提醒】class中的table字段是对应数据库中生厂商表名(如果你是按以上提供的建表语句生成的数据表的话,这里就不用修改了,否则请做相应修改)
(5)定义【产品】类
package com.mr.product;
import com.mr.factory.Factory;
/**
* 商品信息的持久化类
*/
public class Product {
private Integer id;// 唯一性标识
private String name;// 产品名称
private Double price;// 产品价格
private Factory factory;// 关联的生产商
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 Factory getFactory() {
return factory;
}
public void setFactory(Factory factory) {
this.factory = factory;
}
}
(6)编写【产品】配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 产品信息字段配置信息 -->
<hibernate-mapping>
<class name="com.mr.product.Product" table="tab_product">
<!-- id值 -->
<id name="id" column="id" type="int">
<generator class="native"/>
</id>
<!-- 产品名称 -->
<property name="name" type="string" length="45">
<column name="name"/>
</property>
<!-- 产品价格 -->
<property name="price" type="double">
<column name="price"/>
</property>
<!-- 多对一关联映射 -->
<many-to-one name="factory" class="com.mr.factory.Factory" cascade="all">
<!-- 映射的字段 -->
<column name="factory_id"/>
</many-to-one>
</class>
</hibernate-mapping>
【提醒】class中的table字段是对应数据库中产品表名(如果你是按以上提供的建表语句生成的数据表的话,这里就不用修改了,否则请做相应修改)
如果你已经使用了第一步中的sql语句建表的话,这里就不用建表了。当然你也可以删除以前建的表尝试一下这种方式。
3、建表:(方法二)
定义以下类并运行就可以根据以上的配置文件进行建表
package com.mr.main;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class createTable
{
public static void main(String[] args)
{
ServiceRegistry serviceRegistry = (ServiceRegistry) new StandardServiceRegistryBuilder()
.configure().build();
MetadataImplementor metadataImplementor = (MetadataImplementor) new MetadataSources(
serviceRegistry).buildMetadata();
SchemaExport export = new SchemaExport(serviceRegistry,
metadataImplementor);
export.create(true, true);//第一个参数是是否输出到控制台,第二个参数是是否输出到数据库建表
}
}
4、测试:
如果还没用数据的话,先将插入部分代码去除注释运行。然后可以将插入部分注释,然后将查询部分注释去除进行测试。
package com.mr.main;
import org.hibernate.Session;
import com.mr.hibernate.HibernateInitialize;
public class SelectProduct {
public static void main(String[] args) {
Session session = null; // 声明第一个Session对象
try {
// Hibernate的持久化操作
session = HibernateInitialize.getSession();// 获取Session
session.beginTransaction();// 事务开启
/******* 插入 ********/
// // 实例化factory类
// Factory factory = new Factory();
// factory.setFactoryName("factoryName1");
//
// // 实例化product类
// Product product = new Product();
// product.setName("productName1");
// product.setPrice(12.3);
// product.setFactory(factory);
// session.save(product);
/******* 查询 *******/
// Product product = (Product) session.get(Product.class,
// new Integer("1")); // 装载对象
// System.out.println("产品名称:" + product.getName());
// System.out.println("产品价格:" + product.getPrice() + "元");
//
// System.out.println("生产商:" +
// product.getFactory().getFactoryName());
session.getTransaction().commit();// 事务提交
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();// 事务回滚
} finally{
HibernateInitialize.closeSession();// 关闭Session
}
}
}
以上内容如有错误之处,欢迎各位指正。如有问题,可以与我讨论。