Hibernate之one-to-one主键关联映射

one-to-one映射的例子为一个人拥有一个身份证,一个身份证属于一个人。

先创建po类

Person.java

package po;

public class Person {
	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;
	}
	
}

Card.java

package po;

public class Card {
	private int id;          //身份证ID
	private String number;   //身份证号码
	private Person person;   //一个身份证号对应一个人
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	
	
	
	
}

po类的映射文件

Person.hbm.xml

<hibernate-mapping>
	<class name="po.Person" table="person">
		<id name="id" type="integer">
			<generator class="native" />
		</id>
		<property name="name" />
	</class>

</hibernate-mapping>

Card.hbm.xml

<hibernate-mapping>
    <class name="po.Card" table="card">
        <id name="id" type="integer">
           <!-- 主键生成方式为foreign -->
           <generator class="foreign">
               <param name="property">person</param>
           </generator>
        </id>
        <property name="number"></property>
        <!-- !!!constrained=true表示生成外键约束 -->
        <one-to-one name="person" constrained="true"></one-to-one>
    </class>
</hibernate-mapping>

hibernate.cfg.xml

<hibernate-configuration>

    <session-factory>
       <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
		<property name="connection.username">root</property>
		<property name="connection.password">1</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="myeclipse.connection.profile">mysql</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
   	    <mapping resource="po/Person.hbm.xml"/>
   	    <mapping resource="po/Card.hbm.xml"/>
    </session-factory>
</hibernate-configuration

测试类Test.java

package po;

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

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;


public class Test {
	
	@Test
	public void testCreateDB(){
		//生成表结构
		Configuration config = new Configuration().configure();
		SchemaExport export = new SchemaExport(config);
		export.create(true, true);
	}
	@Test
	public void testSave(){
		//测试 添加数据 
		Configuration config = new Configuration().configure();
		SessionFactory factory = config.buildSessionFactory();
		Session session = factory.openSession();
		session.beginTransaction();
		
	   
		Person person = new Person();   //先有person然后才能有省份证
		person.setName("zhangsan");
		
		Card card = new Card();
		card.setNumber("4114031111222223333");
		card.setPerson(person);       //此身份证属于此人
		session.save(card);           //保存card
		
		session.getTransaction().commit();
		
	} 
	@Test
	public void testGetPerson(){
		Configuration config = new Configuration().configure();
		SessionFactory factory = config.buildSessionFactory();
		Session session = factory.openSession();
	    session.beginTransaction();

        Card card = (Card)session.get(Card.class,1 ); //取出Card
        System.out.println("CardNumber: "+card.getNumber());
        
        Person person = card.getPerson();  //通过card.getPerson()方法 返回person
        System.out.println("PersonName: "+person.getName()); //取出person的name
	    
	    session.getTransaction().commit();
	    session.close();
	}
}
hibernate后天生成表结构sql为

 create table card (
        id integer not null,
        number varchar(255),
        primary key (id)
    )

    create table person (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )
   alter table card 
        add index FK2E7B10F83A3F5F (id), 
        add constraint FK2E7B10F83A3F5F 
        foreign key (id) 
        references person (id)


posted on 2011-12-30 21:54  itmyhome  阅读(225)  评论(0编辑  收藏  举报

导航