前几天学习hibernate ,使用eclipse上面的插件(hibrenatesyn),在网上看了点资料,发现www.hibernate.org.cn不错,不过就是 hibernate入门篇之新增特性_2:one-to-one(http://www.hibernate.org.cn/64.html)不是很详细,试了好长时间没有成功,而且作者对数据库也没有做设计,我想给初学者做个例子:
其实one-to-one在hibernate 的发布doc里面有很详细的说明,具体见 41页(chinese version)。根据doc,其实有两种方式实现 one to one.我就其中的一种示范下。(使用 many to one )hehe,我感觉只要一种可以实现的话,一般其他不成问题了,就怕一开始就失败了,那就没有学习的积极性了。
一.数据库设计:--我使用sqlserver
表1 person
字段 person_id key int
email varchar(50)
name varchar(50)
表2 author
字段 author_id key int
alias varchar(50)
person_id int foreign key
很简单的关系,具体在sqlserver里面怎么设表 我就不说了。
二.eclipse 里面使用 hibernatesyn等工具 我也不讲了,网上有很多这方面的资料的。不过,我有个问题:我使用hibernatesyn一开始 生成 * .hbm文件(也就是mapping )后,如果我数据库需要修改,我只能直接修改*.hbm文件,没有工具能直接update mapping,要是各位知道有该功能请告诉我啊。thanks!
三.hibernate.cfg.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- local connection properties -->
<property name="hibernate.connection.url">
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test;SelectMethod=cursor
</property>
<property name="hibernate.connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">scroll</property>
<!-- property name="hibernate.connection.pool_size"></property -->
<!-- dialect for Microsoft SQL Server -->
<property name="dialect">
net.sf.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_outer_join">true</property>
<!--
property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property
//-->
<mapping resource="com/tjlog/po/Person.hbm" />
<mapping resource="com/tjlog/po/Author.hbm" />
</session-factory>
</hibernate-configuration>
注意:我把 <!-- property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property //--> 注释了,因为我是直接使用JDBC的,如果不注释就报错!
四.person.hbm
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.tjlog.po">
<class
name="Person"
table="PERSON"
>
<id
name="Id"
type="java.lang.Integer"
column="Person_Id"
>
<generator class="identity" />
</id>
<property
name="Email"
column="Email"
type="string"
not-null="false"
length="100"
/>
<property
name="Name"
column="Name"
type="string"
not-null="false"
length="100"
/>
</class>
</hibernate-mapping>
五.author.hbm
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.tjlog.po">
<class name="Author" table="AUTHOR">
<id
column="Author_Id"
name="Id"
type="java.lang.Integer"
>
<generator class="identity" />
</id>
<property
column="Alias"
length="100"
name="Alias"
not-null="false"
type="string"
/>
<many-to-one
cascade="all"
class="com.tjlog.po.Person"
column="Person_Id"
name="person"
not-null="true"
outer-join="auto"
/>
</class>
</hibernate-mapping>
六.测试类 CommonExample (其实可以使用 HibernateUtil.java 的,仅测试,如果需要在实际使用,则就需要DAO封装)
/*
* 创建日期 2005-1-24
*
*/
package com.tjlog.example;
/**
* @author Administrator
* @CorpRight DongYiPing
*/
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import java.util.*;
import com.tjlog.po.*;
public class CommonExample
{
public CommonExample()
{}
public static void main(String[] args)
{
List list;
try {
Configuration cfg = new Configuration().configure();
SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
Person person = new Person();
person.setName("DongYiPing");
person.setEmail([email protected]);
Author author = new Author();
author.setAlias("pingping");
author.setPerson(author);
session.saveOrUpdate(person);
tx.commit();
session.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
在我机器数据插入成功!
If u have anything about this test application,u can ask me by email :[email protected].
I am also the fresh man of hibernate.
本文地址:http://com.8s8s.com/it/it11731.htm