当前位置: 代码迷 >> Web前端 >> hibernate一对一联系关系映射
  详细解决方案

hibernate一对一联系关系映射

热度:255   发布时间:2012-07-31 12:33:46.0
hibernate一对一关联映射
知识点:

第一种:
一对一(Person - IdCard)
1)基于主键的one-to-one(person的映射文件)
<id name=”id”>
<generator class=”foreign”><param name=”property”>idCard</param></generator>
<id>
<one-to-one name=”idCard” constrained=”true”/>


第二种 :

一对一(Person - IdCard)
2)基于外健的one-to-one,可以描述为多对一,加unique=“true”约束
<one-to-one name=”idCard” property-ref=“person”/>
             property-ref用于指定关联类的一个属性,这个属性将会和本外键相对应
<many-to-one name=”person” column=”person_id” unique=”true” not-null=”true”/>
?<!-唯一的多对一,其实就便成了一对一了-->



(新建普通java工程)
第一步:配置hibernate环境 、编写hibernate.cfg.xml配置文件

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- jdbc:mysql:///test 以下表示localhost 3306 test:数据库-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student?useUnicode=true&amp;characterEncoding=UTF-8</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">liyong</property>

<!-- 下面指定方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 下面指定是否让hibernate产生ddl语句 DDL:数据定义语言-->
<property name="hbm2ddl.auto">update</property>

<mapping resource="com/liyong/domain/IdCard.hbm.xml"/>
<mapping resource="com/liyong/domain/Person.hbm.xml"/>
</session-factory>
</hibernate-configuration>

第二步:编写实体IdCard.java Person.java

IdCard.java

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;
}
}

Person.java

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.hbm.xml、Person.hbm.xml

IdCard.hbm.xml:

<hibernate-mapping package="com.liyong.domain">
<class name="IdCard">
<!-- 生成主键 以natvie 自增长-->
<id name="id">
<!--foreign 所对应的类中的 property属性设置值 这里表示从person中的主键来生成这个表的主键和外键 -->
<!-- 第一种:<generator class="foreign">

   <param name="property">person</param>
</generator>
-->
<!--第二种: 也可以类似于多对一映射 -->
<generator class="native"/>

</id>
<property name="usefulLife"/>
<!-- 增加约束 -->
<!-- 第一种: <one-to-one name="person" constrained="true"/>-->
<!--第二种: 也可以类似于多对一映射只是把person_id设置为唯一的 -->
<many-to-one name="person" unique="true" column="person_id"/>

</class>
</hibernate-mapping>

<?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.liyong.domain">

<class name="Person">
<id name="id">
<generator class="native" />
</id>
<property name="name" />
<!--<one-to-one name="idCard"/> 第一种-->
        <!-- 第二种 这里指定property-ref="person" 是因为如果不指定默认是idCard 类的id作为这个映射列 而这里IdCard 中的id和外键不是同一列所有指定这个属性意思是 在IdCard 中person中的id作为映射列-->
<one-to-one name="idCard" property-ref="person"/>
</class>

</hibernate-mapping>

第四步:编写测试

public class OneToOneTest {
public static void main(String[] args) {
query(1);
}

@Test
public static void add()
{
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
IdCard idCard = new IdCard();
idCard.setUsefulLife(new Date());

Person p = new Person();
p.setName("p1");
// p.setIdCard(idCard);

idCard.setPerson(p);

tx = s.beginTransaction();
s.save(p);
s.save(idCard);
tx.commit();

} finally {
if (s != null)
s.close();
}
}

@Test
public static void query(int id)
{
Session session = null;
Transaction tx = null;
try {
session = HibernateUtil.getSession();
//Person person=(Person)session.get(Person.class, id);
//System.out.println(person.getIdCard().getUsefulLife());
IdCard idCard=(IdCard)session.get(IdCard.class, id);
System.out.println(idCard.getUsefulLife());
} finally {
if (session != null)
session.close();
}
}
}
第五步:测试

源码见附件:

  相关解决方案