您的位置:首页 > 数据库

Hibernate关联映射之多对一单向关联映射

2012-05-16 22:38 381 查看
此实例为多对一(Employee——Depart)单向关联映射

方法一:使用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)运行方法一的测试类,得到相同的测试结果。

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息