您的位置:首页 > 编程语言 > Java开发

eclipse下wtp+HibernateTools开发笔记

2006-08-31 20:24 399 查看
作者fbysss
msn:jameslastchina@hotmail.com
blog:blog.csdn.net/fbysss
声明:本文由fbysss原创,转载请注明出处
关键字:eclipse,hibernate

准备包:
HibernateTools-3.1.0.beta4.zip
GEF-SDK-3.1.1.zip
hibernate-3.1.2.zip
JEM-SDK-1.1.0.1.zip
wtp-sdk-M200602010238.zip
1.新建一个java Project,建立src目录,建立com.sss.common包,
2.Java->Builder Path->User Libraies 新建一个Hibernate的用户类库,把hibernate中hibernate3.jar以及/lib下面的jar都加入进去
3.然后建立一个MSSQL用户库,把SQL JDBC的类库加入。(注意,如果安装的是sp4的类库,只能对应SQLSERVER2000 SP4,否则到时hibernate tool无法刷新出数据库结构。)
然后在工程的属性中设定Java Build Path,在Libraries页中加入Hibernate和MSSQL类库。
4.新建Hibernate.cfg.xml:new->Other


路径选择src下。选定db类型,并填写url:jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ssstest;SelectMethod=cursor
填写用户名/密码。
5.新建一个Hibernate Console Configuration,命名为sssPrj。需要注意的是Classpath那里需要将jdbc driver包加入



(在window->show view->Other->Hibernate Configurations),


下方面版上就会出现Hibernate Console Configuration的视图,可以对Hibernate Console Configuration进行编辑、删除等操作。
6.新建一个Hibernate.reveng.xml,路径依然选择src,Console Configuration 选择刚才建立的sssPrj,然后点击Refresh,数据库的结构就出来了。选中指定的表,点击“include”按钮,加入到右边的Table Filter列表中。然后Finish。


5.自动生成java代码







输出页中第一项一定要选择,那就是生成pojo代码的。DAO是一个Home文件,用于封装部分操作。
设置好之后,点击apply,run,代码就自动生成了。
需要修改的两个地方:
protected SessionFactory getSessionFactory() {
try {
//return (SessionFactory) new InitialContext().lookup("SessionFactory");这句不能通过,会提示Could not locate SessionFactory in JNDIjavax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial

修改为
return (SessionFactory) new Configuration().configure().buildSessionFactory();
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI"+e.toString());
}
}



public Tb1 findById( java.lang.String id) {
log.debug("getting Tb1 instance with id: " + id);
try {
System.out.println("test0....."+sessionFactory);
//Tb1 instance= (Tb1)sessionFactory.getCurrentSession().get(“com.sss.common”,id);//这句有问题。会提示No CurrentSessionContext configured!
修改为如下两句
Session session = sessionFactory.openSession();//先加入import org.hibernate.Session;
//Tb1 instance = (Tb1) session.load("com.sss.commmon.Tb1", id);这句也可以
Tb1 instance = (Tb1) session.get("com.sss.commmon.Tb1", id);

if (instance==null) {
log.debug("get successful, no instance found");
}
else {
log.debug("get successful, instance found");
}
return instance;
}
catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
6.编写测试类。
package com.sss.commmon;

public class TestHib {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Tb1Home tb1Home = new Tb1Home();
Tb1 tb1 = tb1Home.findById("111");
//************************
System.out.println("tb1.id is:"+tb1.getId());
System.out.println("tb1.name is :"+tb1.getName());
}

}

输出为:
test0.....org.hibernate.impl.SessionFactoryImpl@10ab323
tb1.id is:111
tb1.name is :第一条记录
大功告成!
下一步将进行数据操作测试。


更新:update
public static void update()
{
try
{
//通过Configuration获得一个SessionFactory对象
SessionFactory sf
= new Configuration().configure().buildSessionFactory();
//打开一个Session
Session session = sf.openSession();
//开始一个事务
Transaction tx = session.beginTransaction();
//创建一个Student对象
Tb1 tb1 = findById("222");
//if(true)return ;
//通过Student的setter方法改变它的属性
//注意student_id不用我们设置
//tb1.setId("111");
tb1.setName("更新第一条");

//通过session的save()方法将Student对象保存到数据库中
System.out.println("ttttttt1.");
session.update(tb1);
//save(tb1);插入用save
System.out.println("ttttttt2.");
//提交事务
tx.commit();
//关闭会话
session.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}

hbm文件:

<hibernate-mapping>
<class name="com.sss.commmon.Tb1" table="tb1" schema="dbo" catalog="ssstest">
<id name="id" type="string">
<column name="id" length="10" />
<generator class="native"></generator>
</id>
<property name="name" type="string">
<column name="name" length="10" />
</property>
</class>
</hibernate-mapping>

这里generator设置为native,表示id的类型由数据库自己决定,需要数据库支持自动增长列,比如MSSQL,就需要把id设置为整型,并设置为“标识”字段。Hibernate不负责生成id。如果设置为identity,则由hibernate来自动增长。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: