Hibernate关联映射之多对一单向关联映射
2012-05-16 22:38
381 查看
此实例为多对一(Employee——Depart)单向关联映射
方法一:使用Annotation实现
(1)建立Depart类和Employee类
(2)设置Hibernate配置文件hibernate.cfg.xml
(3)建立测试类,此处使用Junit4进行测试,仅仅测试一对一映射如何建表即可。
(4)运行测试类,查看后台输出的建表SQL语句。
方法二:使用映射文件hbm.xml实现:
(1)建立Depart类和Employee类,在方法一的基础上去掉@注解即可。
(2)建立映射文件Depart.hbm.xml和Employee.hbm.xml
(3)设置Hibernate配置文件hibernate.cfg.cml的映射文件
(4)运行方法一的测试类,得到相同的测试结果。
方法一:使用Annotation实现
(1)建立Depart类和Employee类
package com.model; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Depart { private int id; private String name; @Id 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; } }
package com.model; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @Entity public class Employee { private int id; private String name; private Depart depart; @Id 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; } @ManyToOne public Depart getDepart() { return depart; } public void setDepart(Depart depart) { this.depart = depart; } }
(2)设置Hibernate配置文件hibernate.cfg.xml
<?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="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 连接的数据库的url --> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <!-- 连接的数据库的用户名--> <property name="connection.username">root</property> <!-- 连接的数据库的密码 --> <property name="connection.password"></property> <!-- 配置Hibernate数据库方言 --> <property name="Dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 输出执行的SQL语句 --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- 启动时撤销并重新创建数据库的模式--> <property name="hbm2ddl.auto">create</property> <property name="current_session_context_class">thread</property> <mapping class="com.model.Depart"/> <mapping class="com.model.Employee"/> </session-factory> </hibernate-configuration>
(3)建立测试类,此处使用Junit4进行测试,仅仅测试一对一映射如何建表即可。
package com.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.Test; public class ORMappingTest { @Test public void test() { Configuration cfg = new Configuration(); cfg.configure(); ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); SessionFactory sf = cfg.buildSessionFactory(sr); Session s = sf.getCurrentSession(); Transaction tx = s.beginTransaction(); tx.commit(); sf.close(); } }
(4)运行测试类,查看后台输出的建表SQL语句。
Hibernate: create table Depart ( id integer not null, name varchar(255), primary key (id) ) Hibernate: create table Employee ( id integer not null, name varchar(255), depart_id integer, primary key (id) ) Hibernate: alter table Employee add index FK4AFD4ACE7F7C470C (depart_id), add constraint FK4AFD4ACE7F7C470C foreign key (depart_id) references Depart (id)
方法二:使用映射文件hbm.xml实现:
(1)建立Depart类和Employee类,在方法一的基础上去掉@注解即可。
(2)建立映射文件Depart.hbm.xml和Employee.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 package="com.model"> <class name="Depart"> <id name="id"> <generator class="native"></generator> </id> <property name="name" /> </class> </hibernate-mapping>
<?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 package="com.model"> <class name="Employee"> <id name="id"> <generator class="native"></generator> </id> <property name="name" /> <many-to-one name="depart" column="depart_id"></many-to-one> </class> </hibernate-mapping>
(3)设置Hibernate配置文件hibernate.cfg.cml的映射文件
<mapping resource="com/model/Depart.hbm.xml"/> <mapping resource="com/model/Employee.hbm.xml"/>
(4)运行方法一的测试类,得到相同的测试结果。
相关文章推荐
- Hibernate的关联映射(2)-单向1-1关联
- 【SSH进阶之路】Hibernate映射——多对一单向关联映射(四)
- 【Hibernate步步为营】--单向关联一对一映射
- Hibernate之对象关系映射01一对一单向关联
- Hibernate几种关系映射总结(单向关联)
- Hibernate教程04_关系映射之一对一单向外键关联
- hibernate关联映射-单向
- Hibernate映射—— 多对一单向关联映射
- Hibernate一对多单向关联和双向关联映射方法及其优缺点
- Hibernate的关联映射(4)-单向N-N关联
- Hibernate 映射关系 ---One2Many单向关联
- Hibernate映射详解(二)--多对一,一对多单向,一对多双向关联映射
- 【SSH进阶之路】Hibernate映射——多对一单向关联映射(四)
- 【SSH进阶之路】Hibernate映射——多对一单向关联映射(四)
- Hibernate之对象关系映射03一对一单向外键关联
- Hibernate一对一主键关联映射(单向)
- 【HIbernate框架学习】:Hibernate对象关系映射之单向一对多关联映射
- Hibernate基础 ( 4 )单向关联映射
- ORM框架Hibernate (四) 一对一单向、双向关联映射
- Hibernate一对多单向关联和双向关联映射方法及其优缺点