论坛首页 入门技术论坛

Hibernate一对一外键映射

浏览 1962 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-11-12   最后修改:2009-11-12
person作为IdCard的一个外键
Person.java
package cn.itcast.hibernate.domain;

public class Person {
	private int id;
	private String name;
	private IdCard idcard;
	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 IdCard getIdcard() {
		return idcard;
	}
	public void setIdcard(IdCard idcard) {
		this.idcard = idcard;
	}
}


IdCard
package cn.itcast.hibernate.domain;

import java.util.Date;

public class IdCard {
	private int id;
	private Date usefullife;
	private Person person;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public Date getUsefullife() {
		return usefullife;
	}
	public void setUsefullife(Date usefullife) {
		this.usefullife = usefullife;
	}
	
}


JavaBean映射文件1(Person.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="cn.itcast.hibernate.domain">
	<class name="Person" table="person">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="name" unique="true"/>
		<one-to-one name="idcard" property-ref="person"/>
	</class>

</hibernate-mapping>


JavaBean映射文件2(IdCard.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="cn.itcast.hibernate.domain">
	<class name="IdCard" table="id_card">
		<id name="id">
			<generator class="native" />
		</id>
		<property name="usefullife" column="useful_life"/>
		<!-- 为了防止多个IdCard对应同一个人,所以设置 unique="true"-->
		<many-to-one name="person" column="person_id" unique="true"/>
	</class>
</hibernate-mapping>


Hibernate配置文件(hibernate.cfg.xml)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

	<session-factory>

		<!-- Database connection settings -->
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="connection.url">
			jdbc:mysql://localhost:3306/test
		</property>
		<property name="connection.username">root</property>
		<property name="connection.password">123456</property>

		<!-- JDBC connection pool (use the built-in) -->
		<property name="connection.pool_size">1</property>

		<!-- SQL dialect -->
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>

		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">true</property>

		<!-- Drop and re-create the database schema on startup如果没有表则自动创建 -->
		<property name="hbm2ddl.auto">create</property>

		
		<mapping resource="cn/itcast/hibernate/domain/Person.hbm.xml" />
		<mapping resource="cn/itcast/hibernate/domain/IdCard.hbm.xml" />
	</session-factory>

</hibernate-configuration>



One2One.java用来使用映射
package cn.itcast.hibernate;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.itcast.hibernate.domain.Department;
import cn.itcast.hibernate.domain.Employee;
import cn.itcast.hibernate.domain.IdCard;
import cn.itcast.hibernate.domain.Person;

public class One2One {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		add();
		queryPerson(1);
//		queryIdCard(1);
	}

	static Person queryPerson(int id){
		Session s= null;
		Transaction tx = null;
		try {
			s=HibernateUtil.getSession();
			tx= s.beginTransaction();
			Person person=(Person)s.get(Person.class, id);
			System.out.println(person.getIdcard().getUsefullife());
			tx.commit();
			return person;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}
	
	static IdCard queryIdCard(int id){
		Session s= null;
		Transaction tx = null;
		try {
			s=HibernateUtil.getSession();
			tx= s.beginTransaction();
			IdCard idCard=(IdCard)s.get(IdCard.class, id);
			System.out.println(idCard.getPerson().getName());
			tx.commit();
			return idCard;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}
	
	static Person add(){
		Session s= null;
		Transaction tx = null;
		try {
			s=HibernateUtil.getSession();
			tx= s.beginTransaction();
			
			IdCard idcard = new IdCard();
			idcard.setUsefullife(new Date());

			Person person = new Person();
			person.setName("personname1");
			person.setIdcard(idcard);
			
			idcard.setPerson(person);
			s.save(person);
			s.save(idcard);
			tx.commit();
			return person;
		}finally{
			if(s!=null){
				s.close();
			}
		}
	}
}

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics