Hibernate 、多表关联映射-多对一关系(many-to-one)


Hibernate.cfg.xml:

    <session-factory name="sessionFactory">
    	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    	<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8</property>
    	<property name="hibernate.connection.username">root</property>
    	<property name="hibernate.connection.password"></property>
    	<property name="hibernate.show_sql">true</property>
    	<property name="hibernate.format_sql">true</property>
    	<property name="hibernate.hbm2ddl.auto">update</property>

    	<mapping resource="cn/hbm/Person.hbm.xml"/>
    	<mapping resource="cn/hbm/Department.hbm.xml"/>
    </session-factory>


Person:

public class Person {

	private Integer id;
	private String name;
	private Department dept;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Department getDept() {
		return dept;
	}
	public void setDept(Department dept) {
		this.dept = dept;
	}
	
	
}


Department:

public class Department {

	private Integer id;
	private String name;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
}


department.hbm.xml:

<hibernate-mapping package="cn.model">
	<class name="Department" table="DEPARTMENT" lazy="true">
		<id name="id" column="ID">
			<generator class="native"></generator>
		</id>
		<property name="name" column="NAME" type="java.lang.String" />
		<property name="address" column="ADDRESS" type="java.lang.String" />
	</class>
</hibernate-mapping>


Person.hbm.xml:


<hibernate-mapping package="cn.model">
	<class name="Person" table="PERSON" lazy="true">
		<id name="id" column="ID">
			<generator class="native"></generator>
		</id>
		<property name="name" column="NAME" type="java.lang.String" />
		<many-to-one name="dept"  column="DEPT_ID" />
	</class>
</hibernate-mapping>


使用many-to-one 将Person与Department的实体应射关联起来 DEPT_ID 是Department在Person表中的一个外键,Hibernate会根据Person实体中的dept然后找到对应的实体Department 然后再取得Department 的映射关系;

public void save(){
		Session session=null;
		Transaction tran=null;
		try{
			Department dept=new Department();
			dept.setName("IT开发部");
			Person person=new Person();
			person.setName("汤姆");
			person.setDept(dept);
			session=HibernateSessionFactory.getSession();
			tran=session.beginTransaction();
			session.save(dept);
			session.save(person);
			tran.commit();
		}catch(Exception e){
			if(session!=null){
				session.close();
			}
		}
	}
	
	public List<Person> getByName(String name){
		Session session=null;
		try{
			session=HibernateSessionFactory.getSession();
			String hql=" from Person where name=:name";
			Query query=session.createQuery(hql);
			query.setString("name", name);
			return query.list();
		}catch(Exception e){
			if(session!=null){
				session.close();
			}
		}
		return null;
	}
	


结果,将取得Person的时候,访问Person的部门属性,会将部门信息关联查询出来:



posted @ 2015-10-22 17:17  Bodi  阅读(536)  评论(0编辑  收藏  举报