[原创]hibernate 一对一实践 by hjack(3)

2008-02-23 09:29:46来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/testhibernate</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<property name="show_sql">true</property>
<!--mapping files-->
<mapping resource="model/Author.hbm.xml"></mapping>
<mapping resource="model/Topic.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>

2)、新建Test类,用于测试。
/*
* 创建日期 2005-8-10
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
package test;

import model.Author;
import model.Topic;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

/**
* @author hjack

* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class Test {

Session sess;
Transaction tx;

public void insertTopic(Topic topic,int userID) throws HibernateException{
try{
sess = HibernateUtil.currentSession();
tx = sess.beginTransaction();
//新建一个author对象,并把作者id置入该对象里。
Author author = new Author();
author.setId(userID);
//新建一个topic对象,设置用户名和把author对象set进去。
topic.setAuthor(author);
//因为只是插入一个话题,并不必在author表中插入一条记录,所以只需save(topic)
sess.save(topic);
tx.commit();
}catch(HibernateException e){
System.out.println(e.toString());
}finally{
if(tx!=null){
tx.rollback();
}
HibernateUtil.closeSession();
}
}

public void insertAuthor(Author author) throws HibernateException{
try{
sess = HibernateUtil.currentSession();
tx = sess.beginTransaction();
sess.save(author);
tx.commit();
}catch(HibernateException e){
System.out.println(e.toString());
}finally{
if(tx!=null){
tx.rollback();
}
HibernateUtil.closeSession();
}
}


public Topic query(int id) throws HibernateException{
Topic topic = null;
try{
sess = HibernateUtil.currentSession();
topic=(Topic)sess.load(Topic.class,new Integer(id));
}catch(HibernateException e){
e.printStackTrace();
}finally{
HibernateUtil.closeSession();
}
return topic;
}

public static void main(String[] args) {

Test app = new Test();
try {
/*测试插入作者
Author author = new Author();
author.setName("jack");
app.insertAuthor(author);
*/
/*测试插入主题
Topic topic = new Topic();
topic.setName("helloworld.");
app.insertTopic(topic,1);
*/
/*测试查询主题
Topic topic = app.query(1);
System.out.println(topic.getAuthor().getName());
*/
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}
}

测试插入作者如图1所示,测试插入主题如图2所示,测试查询主题结果如下:
Hibernate: select topic0_.id as id1_, topic0_.name as name1_, topic0_.user_id as user_id1_, author1_.id as id0_, author1_.name as name0_ from topic topic0_ left outer join author author1_ on topic0_.user_id=author1_.id where topic0_.id=?
jack
生成的sql语句用到了join,查询结果为jack,与期望相符。

图一

图二

上一篇: eclipse下tanghan plugin连mssql实战[原创] by hjack
下一篇: osworkflow api 之 beanshell and jndi

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:osworkflow api 之 util

下一篇:SQL 中 Delete、Truncate、Drop