cn.scxh.hibernate.model包
java Department类:
package cn.scxh.hibernate.model;
public class Department {
private int id;//主键 部门编号
private String name;//部门名称
public Department() {
}
public Department(int id, String name) {
this.id = id;
this.name = 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;
}
@Override
public String toString() {
return "Department [id=" + id + ", name=" + name + "]";
}
}
cn.scxh.hibernate.model包
Department.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">
<!-- Generated 2017-1-6 16:34:38 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="cn.scxh.hibernate.model.Department" table="department">
<id name="id" type="int">
<column name="id" />
<!-- 自动生成主键 increment identity sequence; native根据底层数据自动选择相应生成策略 -->
<!-- assigned 根据java代码生成 -->
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
</class>
</hibernate-mapping>
Employee java类
package cn.scxh.hibernate.model;
public class Employee {
private int id;//主键
private String name;//员工姓名
private int age;
private int salary;//员工薪水
private Department department;
public Employee() {
}
public Employee(int id, String name, int age, int salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age
+ ", salary=" + salary + ", department=" + department + "]";
}
}
cn.scxh.hibernate.model包
Employee.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">
<!-- Generated 2017-1-6 16:34:38 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="cn.scxh.hibernate.model.Employee" table="employee">
<id name="id" type="int">
<column name="id" />
<!-- 自动生成主键 increment identity sequence; native根据底层数据自动选择相应生成策略 -->
<!-- assigned 根据java代码生成 -->
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="name" />
</property>
<property name="age" type="int">
<column name="age" />
</property>
<property name="salary" type="int">
<column name="salary" />
</property>
<!--多对一关联 -->
<many-to-one name="department" column="department_id"
class="cn.scxh.hibernate.model.Department"></many-to-one>
</class>
</hibernate-mapping>
java 类
package cn.scxh.hibernate.utils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtils {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static void main(String args[]){
HibernateUtils.getSessionFactory();
}
}
工程目录src下
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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 设置数据库URL -->
<property name="hibernate.connection.url">jdbc:mysql://192.168.5.7:3306/hibernate_manytoone</property>
<!-- 数据库用户名 -->
<property name="hibernate.connection.username">test</property>
<!-- 数据库密码 -->
<property name="hibernate.connection.password">123321</property>
<!-- 指定对应数据库的方言,hibernate为了更好适配各种关系数据库,针对每种数据库都指定了一个方言dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 指定ddl的生成方式 第一次用create创建表,第二次用update修改 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 输出sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化输出sql语句 -->
<property name="hibernate.format_sql">true</property>
<!-- 配置CurrentSessionContext 使用getCurrentSession()获取Session实例时 -->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 映射文件 -->
<mapping resource="cn/scxh/hibernate/model/Department.hbm.xml" />
<mapping resource="cn/scxh/hibernate/model/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
test文件 cn.scxh.hibernate.model包
java DepartmentTest类
package cn.scxh.hibernate.model;
import static org.junit.Assert.fail;
import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import cn.scxh.hibernate.utils.HibernateUtils;
public class DepartmentTest {
Session session;
@Before
public void setUp() throws Exception {
session = HibernateUtils.getSessionFactory().getCurrentSession();
session.beginTransaction();//开启事物
}
@After
public void tearDown() throws Exception {
session.getTransaction().commit();//提交事物
}
@Test
public void test() {
Department develop = new Department(1,"开发部");
Department product = new Department(2,"产品部");
Employee zhangsan = new Employee(1,"张三",19,4500);
Employee lisi = new Employee(2,"李四",19,4500);
session.save(zhangsan);
session.save(lisi);
zhangsan.setDepartment(develop);
lisi.setDepartment(product);
session.save(develop);
session.save(product);
}
}