Hibernate关联映射-many to one 外键单向关联

多个人可以对应一个地址,也就是所谓的多对一关系,many to one, 这次还是以Person 和 Address为例 ,写一个简单的 多对一单向外键关联。

 

请看Pojo实体,

 

Person_FK_Many_to_One:

 

/**
 * 多对一,单向关联
 * @author Administrator
 *
 */
public class Person_FK_Many_to_One implements Serializable{
	
	private static final long serialVersionUID = -6313867775683964530L;
	private Integer id;
	private String name;
	private Integer age;
	private Address_FK_Many_to_One addressFKManyToOne;

//getter 和 settter方法省略.
}	

 

Address_FK_Many_to_One:

/**
 * 
 * 多对一;
 * @author Administrator
 *
 */
public class Address_FK_Many_to_One implements Serializable{
	
	private static final long serialVersionUID = 3635140598485086087L;
	private Integer addressID;
	private String addressDetail;
	

//getter 和setter方法省略
}

 

 

接下来配置,Hibernate.hbm.xml文件。

 

新建一个,Person_FK_Many_to_One.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>
	<class name="pack.java.model.Person_FK_Many_to_One" table="ZHT_PERSON_FK_MANY_TO_ONE">
		<id column="ID" name="id" type="java.lang.Integer">
			<generator class="identity"></generator>
		</id>
		<property name="name" column="NAME"></property>
		<property name="age" column="AGE"></property>
		<!-- 
			多对一,name为person中对应的address对象,column指定person表中的外键字段,
			这里的cascade,设为save-update,persist时,指定级联操作是,只能正对save和update进行级联。
		 -->
		<many-to-one name="addressFKManyToOne" column="addressID" cascade="save-update,persist"></many-to-one>
	</class>
</hibernate-mapping>

 

然后,在新建一个Address_FK_Many_to_One.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>
	<class name="pack.java.model.Address_FK_Many_to_One" table="ZHT_ADDRESS_MANY_TO_ONE">
		<id column="ADDRESSID" name="addressID">
			<generator class="identity"></generator>
		</id>
		<property name="addressDetail" column="ADDRESSDETAIL"></property>
	</class>
</hibernate-mapping>

 

配置文件,配置完之后,在hibernate.cfg.xml中,

加入resource mapping.

<mapping resource="pack/java/model/Address_FK_Many_to_One.hbm.xml"/>
<mapping resource="pack/java/model/Person_FK_Many_to_One.hbm.xml"/>

 

最后,新建一个Test测试类,进行测试.

package pack.java.test;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pack.java.hibernate.HibernateSessionFactory;
import pack.java.model.Address_FK_Many_to_One;
import pack.java.model.Person_FK_Many_to_One;
/**
 * 测试多对一单向外键关联;
 * @author Administrator
 *
 */
public class HibernateDemoManyToOne {
	public static void main(String[] args) {
		HibernateDemoManyToOne demoManyToOne = new HibernateDemoManyToOne();
		Session session = demoManyToOne.getSession();
		//保存;
		demoManyToOne.savePerson(session);
		
		//查询;
		demoManyToOne.queryPersonByPersonID(session);
	}
	
	/**
	 * 查询出所有的person对象;
	 * @param session
	 */
	@SuppressWarnings("unchecked")
	private void queryPersonByPersonID(Session session){
		String hql = "select p from Person_FK_Many_to_One p";
		Query query = session.createQuery(hql);
		
		List<Person_FK_Many_to_One> list = query.list();
		for (Person_FK_Many_to_One personFKManyToOne : list) {
			System.out.println(personFKManyToOne.getId()+","+personFKManyToOne.getName()+","+personFKManyToOne.getAge()+","+personFKManyToOne.getAddressFKManyToOne().getAddressDetail());
		}
	}
	
	/**
	 * 获取 session;
	 * @return
	 */
	private Session getSession(){
		Session session = HibernateSessionFactory.getSession();
		return session;
	}
	
	/**
	 * 保存Person;
	 */
	private void savePerson(Session session){
		Person_FK_Many_to_One personFKManyToOne1 = new Person_FK_Many_to_One();
		personFKManyToOne1.setAge(23);
		personFKManyToOne1.setName("张三");
		
		Person_FK_Many_to_One personFKManyToOne2 = new Person_FK_Many_to_One();
		personFKManyToOne2.setAge(43);
		personFKManyToOne2.setName("李四");
		
		Address_FK_Many_to_One addressFKManyToOne = new Address_FK_Many_to_One();
		addressFKManyToOne.setAddressDetail("广东省深圳市");
		
		//给person设置地址;
		personFKManyToOne1.setAddressFKManyToOne(addressFKManyToOne);
		personFKManyToOne2.setAddressFKManyToOne(addressFKManyToOne);
		
		session.beginTransaction().begin();
		//保存Person;
		session.save(personFKManyToOne1);
		session.save(personFKManyToOne2);
		session.beginTransaction().commit();
		
	}
}

 

 测试结果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.

1,张三,23,广东省深圳市
2,李四,43,广东省深圳市

你可能感兴趣的:(Hibernate)