public class Persion {//
private int id;
private String name;
private int age;
private PersionIdCard persionIdCard;
public Persion (){
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public PersionIdCard getPersionIdCard() {
return persionIdCard;
}
public void setPersionIdCard(PersionIdCard persionIdCard) {
this.persionIdCard = persionIdCard;
}
}
//------------------------------------------------
public class PersionIdCard {// 身份证 现在拿到身份证以后可以找到对应的人 // 维护方
// 这个身份证能表示一个人
private Persion persion;
private int id;
private int number;
public PersionIdCard() {
}
public Persion getPersion() {
return persion;
}
public void setPersion(Persion persion) {
this.persion = persion;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
//------------------------------------------
//------------Persion映射文件--------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cn.nnny.pojo">
<class name="Persion" >
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
<property name="age"/>
<one-to-one name="persionIdCard" property-ref="persion"></one-to-one>
</class>
</hibernate-mapping>
//-----------------PersionIdCard映射文件------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cn.nnny.pojo">
<class name="PersionIdCard">
<id name="id">
<generator class="foreign">
<param name="property">persion</param>
</generator>
</id>
<property name="number"></property>
<one-to-one name="persion" class="Persion" constrained="true" lazy="false"/>
</class>
</hibernate-mapping>
//----------------------hibernate配置文件-------------------
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databaseName=hibernate
</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="myeclipse.connection.profile">hibernate</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<mapping resource="com/cn/nnny/pojo/Persion.hbm.xml"/>
<mapping resource="com/cn/nnny/pojo/PersionIdCard.hbm.xml"/>
</session-factory>
</hibernate-configuration>
//---------------------------测试-------------------------
public class MyTest {
@Test
public void test(){
Persion persion=new Persion();
persion.setAge(28);
persion.setName("罗德波");
PersionIdCard persionIdCard=new PersionIdCard();
persionIdCard.setNumber(1001);
persionIdCard.setPersion(persion);
persion.setPersionIdCard(persionIdCard);
SessionFactory sessionFactory=new AnnotationConfiguration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
session.save(persion);
session.save(persionIdCard);
session.getTransaction().commit();
session.close();
}
@Test
public void testgetpersion(){//这个没问题
Persion persion=new Persion();
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.openSession();
session.beginTransaction();
persion=(Persion)session.load(Persion.class,1);
System.out.println(persion.getPersionIdCard().getNumber());
session.getTransaction().commit();
session.close();
}
@Test
public void testget(){//这个有问题
PersionIdCard persionIdCard=new PersionIdCard();
SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
Session session=sessionFactory.getCurrentSession();
session.beginTransaction();
persionIdCard=(PersionIdCard)session.get(PersionIdCard.class,1);
System.out.println(persionIdCard.getPersion().getName());
session.getTransaction().commit();
session.close();
}
}