多对一(员工and部门)

 

ER:

 

employee
名称 主键 类型 描述
id pk int 员工编号
name   varcher(45) 员工名称
department_id fk int 部门编号

 

department
名称 主键 类型 描述
id pk int 部门编号
name   varchar(45) 部门名称

 

package com.zchen.user.domain;

public class Department {

	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;
	}
	
}

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  
<hibernate-mapping package="com.zchen.user.domain">
	<class name="Department" table="`department`">
	  	<id name="id" column="`id`">
		  <generator class="native">
		  </generator>
	  	</id>
	  	<property name="name" unique="true"/>
	</class>
	
</hibernate-mapping>

 

package com.zchen.user.domain;

public class Employee {

	private int id;
	private String name;
	private Department department;
	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 Department getDepartment() {
		return department;
	}
	public void setDepartment(Department department) {
		this.department = department;
	}
	
}

 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  
<hibernate-mapping package="com.zchen.user.domain">
	<class name="Employee" table="`employee`">
	  	<id name="id" column="`id`">
		  <generator class="native">
		  </generator>
	  	</id>
	  	<property name="name"/>
	  	<many-to-one name="department" column="depart_id"></many-to-one>
	</class>
	
</hibernate-mapping>

 

<!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="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql:///user</property>
		<property name="connection.username">root</property>
		<property name="connection.password">mysqladmin</property>
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">create</property>
		<mapping resource="com/zchen/user/domain/Department.hbm.xml"/>
		<mapping resource="com/zchen/user/domain/Employee.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

 测试:

package test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.zchen.user.dao.HibernateUtil;
import com.zchen.user.domain.Department;
import com.zchen.user.domain.Employee;
public class Mant2One {
	public static void main(String[] args) {
		add();
		query(1);
	}
	public static Department add(){
		Department depart = new Department();
		Employee emp = new Employee();
		Session session = null;
		Transaction tx = null;
		try {
			depart.setName("事业部");
			emp.setDepartment(depart);//对象模型 建立两个对象的关联
			emp.setName("里斯");
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			//session.save(depart);
			//session.save(emp);
			session.save(emp);
			emp.setName("wangwu");
			session.save(depart);
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session != null){
				session.close();
			}
		}
		return depart;
	}
	
	static Employee query(int empId){
		Session session = null;
		Transaction tx = null;
		Employee emp = null;
		try {
			session = HibernateUtil.getSession();
			tx = session.beginTransaction();
			emp = (Employee)session.get(Employee.class, empId);
			System.out.println("depart name"+emp.getDepartment().getName());
			tx.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(session != null){
				session.close();
			}
		}
		return emp;
	}
}

 

你可能感兴趣的:(Hibernate,mysql,.net,xml,jdbc)