一对一关联映射(主键+双向)






一对一关联映射(双向)
要实现一对一双向关联映射,我们还是以人和身份证为例来理解:
hibernate一对一主键关联映射(双向关联Person<---->IdCard)

需要在idcard映射文件中加入<one-to-one>标签指向person,指示hibernate如何加载person
默认根据主键加载

第一步:首先建立人和身份证的实体关系类:如下:
public class IdCard {
private int id;
private String cardNo;
private Person person;//在Person中也要持有IdCord的引用

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getCardNo() {
return cardNo;
}

public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}

public Person getPerson() {
return person;
}

public void setPerson(Person person) {
this.person = person;
}
}
这个是Person的实体关系类:
public class Person {

private int id;
private String name;
private IdCard idCard; //在Person中也要持有IdCord的引用
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;
}
}


第二步:建立实体的映射文件:
person实体的映射文件为:
<hibernate-mapping>
<class name="com.bjsxt.hibernate.Person" table="t_person">
<id name="id">
<generator class="foreign">
<param name="property">idCard</param>
</generator>
</id>
<property name="name"/>
<one-to-one name="idCard" constrained="true"/>
</class>
</hibernate-mapping>

IdCord实体的映射文件为:
<hibernate-mapping> <class name="com.bjsxt.hibernate.IdCard" table="t_idcard"> <id name="id"> <generator class="native"/> </id> <property name="cardNo"/> <one-to-one name="person"/>//在IdCord映射文件中也要加入<one-to-one name="person"/> </class> </hibernate-mapping>

第三步:添加Hibernate配置文件:
<hibernate-configuration> <session-factory> <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_one2one_pk_2</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">bjsxt</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.show_sql">true</property> <mapping resource="com/bjsxt/hibernate/IdCard.hbm.xml"/> <mapping resource="com/bjsxt/hibernate/Person.hbm.xml"/> </session-factory> </hibernate-configuration>


第四步:写测试代码:如下: public class One2OneTest extends TestCase { public void testSave1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); IdCard idCard = new IdCard(); idCard.setCardNo("88888888888888"); Person person = new Person(); person.setName("菜10"); person.setIdCard(idCard); //不会出现TransientObjectException异常 //因为一对一主键关联映射中,默认了cascade属性 session.save(person); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Person person = (Person)session.load(Person.class, 1); System.out.println("person.name=" + person.getName()); System.out.println("idCard.cardNo=" + person.getIdCard().getCardNo()); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } public void testLoad2() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); //从IdCord这一端来加 IdCard idCard = (IdCard)session.load(IdCard.class, 1); System.out.println("idcard.cardNo=" + idCard.getCardNo()); System.out.println("idcard.person.name=" + idCard.getPerson().getName()); session.getTransaction().commit(); }catch(Exception e) { e.printStackTrace(); session.getTransaction().rollback(); }finally { HibernateUtils.closeSession(session); } } }




































































































你可能感兴趣的:(关联映射)