Hibernate关系映射(一) 基于外键的单向一对一
2016-04-30 00:31
711 查看
模拟用户和地址的映射关系,一个用户只有一个地址,用户知道地址,但是地址不知道用户。用户对地址的单向一对一映射。
一、建立实体类
Account.cs类
Address.cs类 地址类
二、映射文件
Account.hbm.xml
Address.hbm.xml
三、HibernateUtil工具类
四、hibernate.cfg.xml配置文件
五、Jutil测试类
总结:单向一对一必须通过实体类生成数据库表的方式,会自动生成相关的约束,直接创建表无效。
一、建立实体类
Account.cs类
package com.lxit.entity; import java.io.Serializable; public class Account implements Serializable{ public Account(){ } private int id; private String name; private String password; //需要添加被控端的引用 private Address address; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
Address.cs类 地址类
package com.lxit.entity; import java.io.Serializable; public class Address implements Serializable{ public Address(){ } private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
二、映射文件
Account.hbm.xml
<?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.lxit.entity.Account" table="Account"> <id column="id" name="id"> <generator class="native"/> </id> <property column="name" generated="never" lazy="false" name="name"/> <property column="password" generated="never" lazy="false" name="password"/> <!-- 通过many-to-one 标签添加唯一属性约束,建立一对一关联关系 --> <many-to-one column="address_id" name="address" unique="true"/> <!-- 必须通过映射关系生成的表才会生成唯一约束,否则不会生成 --> </class> </hibernate-mapping>
Address.hbm.xml
<?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.lxit.entity.Address" table="Address"> <id column="id" name="id"> <generator class="native"/> </id> <property column="name" generated="never" lazy="false" name="name"/> </class> </hibernate-mapping>
三、HibernateUtil工具类
package com.lxit.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; /** * Hibernate3.3工具类 * @author Administrator * */ public class HibernateUtil { private static SessionFactory factory; static{ Configuration cfg = new Configuration().configure(); factory = cfg.buildSessionFactory(); } public static SessionFactory getFactory(){ return factory; } public static Session getSession(){ return factory.openSession(); } public static void CloseSession(Session session){ if(session != null){ session.close(); } } }
四、hibernate.cfg.xml配置文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory name="foo"> <!-- 配置数据库连接 --> <property name="hibernate.dialect"> org.hibernate.dialect.MySQL5Dialect </property> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> jdbc:mysql://127.0.0.1:3306/test </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/lxit/entity/Account.hbm.xml" /> <mapping resource="com/lxit/entity/Address.hbm.xml" /> </session-factory> </hibernate-configuration>
五、Jutil测试类
package com.lxit.demo2.test; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import com.lxit.entity.Account; import com.lxit.entity.Address; import com.lxit.util.HibernateUtil; public class AccountTest { @Test public void Add(){ Transaction tx = null; Session session = HibernateUtil.getSession(); tx = session.beginTransaction(); Address address= new Address(); address.setName("深圳宝安"); session.save(address); Account account = new Account(); account.setName("zhangsan"); account.setPassword("123"); account.setAddress(address); try { session.save(account); tx.commit(); } catch (Exception e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateUtil.CloseSession(session); } } @Test public void Add2(){ Transaction tx = null; Session session = HibernateUtil.getSession(); tx = session.beginTransaction(); Address address= new Address(); address.setName("深圳福田"); session.save(address); Account account1 = new Account(); account1.setName("lisi"); account1.setPassword("123"); account1.setAddress(address); Account account2 = new Account(); account2.setName("wangwu"); account2.setPassword("123"); account2.setAddress(address); try { //重复添加第二个用户,地址相同,则会报错 session.save(account1); session.save(account2); tx.commit(); } catch (Exception e) { e.printStackTrace(); tx.rollback(); }finally{ HibernateUtil.CloseSession(session); } } }
总结:单向一对一必须通过实体类生成数据库表的方式,会自动生成相关的约束,直接创建表无效。
相关文章推荐
- 深入理解javascript闭包
- 链表的倒序打印
- 链表的倒序打印
- 数组和字符串
- JavaWeb之Servlet day02
- HDU 2844 二进制优化的多重背包
- PendingTransition实现炫酷的Activity切换动画
- 【LeetCode】Move Zeroes 解题报告
- C++接口(抽象类)
- 解决 Mac OS X 下 Nginx 编译报错 symbol(s) not found for architecture x86_64
- html中调用本地exe程序 打开文件或文件夹
- Codeforces #324 Div2 D.Dima and Lisa(三素数构造、哥德巴赫猜想)
- poj 1251 最小生成树 krusral算法
- lightoj1207Posters_For_Election&&POJ2528
- POJ 2983 差分约束系统
- 复星昆仲杨光:VR行业四大痛点
- Codeforces #324 Div2 C Marina and Vasya(构造)
- tools.js
- 《DATA+++》保护个人隐私的安全套装
- Python str内部功能介绍