关联是类(类的实例)之间的关系,表示有意义和值得关注的连接。
本系列将介绍Hibernate中主要的几种关联映射
Hibernate一对一主键单向关联
Hibernate一对一主键双向关联
Hibernate一对一外键单向关联
Hibernate一对一外键双向关联
Hibernate多对一单向关联
Hibernate多对一双向关联
Hibernate多对多关联
代码都写有注释,主要包括(核心配置文件,实体映射文件,实体类,测试类,数据库)主要操作有增删改查。
本篇主要介绍Hibernate多对一单向关联:
hibernate.cfg.xml
<?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="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 配置数据库连接字符串 --> <property name="connection.url"><![CDATA[jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8]]></property> <!-- 配置数据库连接用户名 --> <property name="connection.username">root</property> <!-- 配置数据库连接密码 --> <property name="connection.password">123456</property> <!-- 配置数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 配置数据库表生成策略 --> <!--<property name="hibernate.hbm2ddl.auto">update</property> --> <!-- 配置是否打印显示SQL语句 --> <property name="show_sql">true</property> <!-- 配置是否格式化显示SQL语句 --> <property name="format_sql">true</property> <!-- 配置hibernate是否自动提交事务 --> <!--<property name="hibernate.connection.autocommit">true</property> --> <!-- 配置实体类对应的映射文件 --> <!-- 多对一单向关联配置 --> <mapping resource="com/great/entity1/Product.hbm.xml"></mapping> <mapping resource="com/great/entity1/Factory.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
Product.hbm.xml
<?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="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 配置数据库连接字符串 --> <property name="connection.url"><![CDATA[jdbc:mysql://localhost:3306/test111?useUnicode=true&characterEncoding=utf8]]></property> <!-- 配置数据库连接用户名 --> <property name="connection.username">root</property> <!-- 配置数据库连接密码 --> <property name="connection.password">123456</property> <!-- 配置数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 配置数据库表生成策略 --> <!--<property name="hibernate.hbm2ddl.auto">update</property> --> <!-- 配置是否打印显示SQL语句 --> <property name="show_sql">true</property> <!-- 配置是否格式化显示SQL语句 --> <property name="format_sql">true</property> <!-- 配置hibernate是否自动提交事务 --> <!--<property name="hibernate.connection.autocommit">true</property> --> <!-- 配置实体类对应的映射文件 --> <!-- 多对一单向关联配置 --> <mapping resource="com/great/entity1/Product.hbm.xml"></mapping> <mapping resource="com/great/entity1/Factory.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
Factory.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.great.entity1"> <class name="Factory" table="factory"> <!-- 配置主键映射关系 --> <id name="fId" column="fId" type="int"> <!-- 配置主键生成策略 --> <generator class="identity"> </generator> </id> <!-- 配置属性和表字段映射关系 --> <property name="fName" column="fName" type="string"></property> </class> </hibernate-mapping>
Product.java
package com.great.entity1; public class Product { // 产品类 // 产品id private int pId; // 产品名字 private String pName; // 厂商 private Factory factory; public int getpId() { return pId; } public void setpId(int pId) { this.pId = pId; } public String getpName() { return pName; } public void setpName(String pName) { this.pName = pName; } public Factory getFactory() { return factory; } public void setFactory(Factory factory) { this.factory = factory; } }
Factory.java
package com.great.entity1; public class Factory { // 厂商类 // 厂商id private int fId; // 厂商名字 private String fName; public int getfId() { return fId; } public void setfId(int fId) { this.fId = fId; } public String getfName() { return fName; } public void setfName(String fName) { this.fName = fName; } }
TestManyToOneUnidirectional.java(测试类)
package com.great.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.great.entity1.Factory; import com.great.entity1.Product; public class TestManyToOneUnidirectional { static Session session; // 多对一单向关联 @BeforeClass public static void setUpBeforeClass() throws Exception { // 加载hibernate主配置文件 Configuration cfg = new Configuration().configure(); // 构建session工厂 SessionFactory sf = cfg.buildSessionFactory(); // 打开session session = sf.openSession(); } @AfterClass public static void tearDownAfterClass() throws Exception { // 关闭session,释放资源 session.close(); } // select1,通过product查询数据 @Test public void testManyToOneSelect1() { Product p = (Product) session.get(Product.class, 1); System.out.println("产品名字为:" + p.getpName()); System.out.println("产品厂家为:" + p.getFactory().getfName()); } // insert1数据,只能通过保存product来插入数据 @Test public void testManyToOneSave1() { Transaction ts = session.beginTransaction(); Product p1 = new Product(); Product p2 = new Product(); Factory f1 = new Factory(); f1.setfName("健力宝集团"); p1.setpName("健力宝"); p2.setpName("神水"); p1.setFactory(f1); p2.setFactory(f1); session.save(p1); session.save(p2); ts.commit(); } // delete1数据,通过product删除了product. @Test public void testManyToOneDelete1() { Transaction ts = session.beginTransaction(); Product product = (Product) session.get(Product.class, 8); session.delete(product); ts.commit(); } // update1,通过product更新数据 @Test public void testManyToOneUpdate1() { Transaction ts = session.beginTransaction(); Product product = (Product) session.get(Product.class, 7); product.setpName("神水"); product.getFactory().setfName("健力宝集团2"); session.save(product); ts.commit(); } }
数据库:
product表
三个字段 pId pName fId(外键)
factory表
两个字段 fId fName
注意:在product表的fId上建立外键关系。