A small plugin,easy to understand and easy to use.I haven't contact Hibernate before and feel hibernate is very difficult to use,but,with the help of this plugin,I spent half a day to finish a Hibernate based demo.
But,I am doubtful,a tool so easy to use,the code created by it if is good or not?
标签:hibernate
很久没来了
换了一个工作,忙着做交接。主要是原来的工作太轻闲了,实在对自己的发展不利。新的工作与原来的类似,但是公司规模更大一些,也更正规一些。目前也在使用Hibernate+Struts的框架,我现在正在学习,平时零散的学习笔记会写在自己的blog上,以后会整理后贴到这里的。
我觉得使用开源软件对于学习的最大的好处就是可以去看源码,看看牛人们是怎么写程序的,而且可以从中理解框架的原理和思想。譬如DAO、譬如MVC、由譬如DI和AOP,为什么要实现这些模式,又是怎么实现这些模式的。今天看到csdn的java emag上采访Adams,JDO成员组的专家,他说,JDO不是凭空出现的,早在java诞生前,他们就用c做过一个O/R Mapping概念的组件了。实际上Hibernate、Struts、Spring这些眼下很红火的东东,其中的思想也是很早就有了,它们都是多年来经验的总结。
另外,看源码的另一个好处就是知道了很多很实用但一直不被多数了解的东西,即框架背后默默无闻的那些工具包,比如负责事务处理的jta,有比如jakarta commons下面的许多包,像Digester和BeanUtils,还有像Antlr。另外也包括像maven这样的工具,还有docbook,原来大牛们都用啊......
看源码最后一个好处就是遇到后来新出的东西也不会害怕了,即使是不同的思想,但是框架的原理是一样的,或者目的是一样的,只是实现方式不同,就算不一样,也可以有个参照么。像我们这些整体闲着的,或者是学生,没太多实践的机会,如果看通源码,那出去和人侃,照样能把人侃晕啊,hiahia~~
need "j2ee.jar" added to classpath when hibernate
Today,I am trying Hibernate and use Hibernatesynch for eclipse2.1 plugin got some classes.
When I run this program on Tomcat5.0,the page always reports:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.NoClassDefFoundError: javax/transaction/Synchronization
net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:314)
net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:327)
net.sf.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:335)
com.asprise.business._BaseRootDAO.createSession(_BaseRootDAO.java:116)
com.asprise.business._BaseRootDAO.createSession(_BaseRootDAO.java:107)
com.asprise.business._BaseRootDAO.getSession(_BaseRootDAO.java:99)
com.asprise.business._BaseRootDAO.find(_BaseRootDAO.java:162)
com.asprise.business.UserBean.findUsers(UserBean.java:23)
com.asprise.struts.action.OwnerAction.performViewUsers(OwnerAction.java:61)
com.asprise.struts.action.OwnerAction.execute(OwnerAction.java:51)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:448)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:263)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1176)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:454)
javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.25 logs.
--------------------------------------------------------------------------------
Apache Tomcat/5.0.25
I found there are two ways to resolve this problem:
1.add j2ee.jar to program's "/web-inf/lib" folder or javax.transaction.Synchronization.class to it's "/web-inf/classes" folder;
2.add the file to tomcat's "/common/lib" or "/common/classes" as above.
I don't know why this happened,I have configed the classpath environment "d:\j2ee1.3\lib\j2ee.jar",and configed the "J2EE_HOME" is "d:\j2ee1.3".
Is there anyone can answer me this question,by any chance please?
DAO
DAO-数据访问对象(Data Access Object),是将业务逻辑层与持久性数据层解耦的一个集成层模式,它处理与持久性数据(关系数据库、面向对象数据库、纯文件、集成时遇到的遗产或者异类系统)的事务性交互,生成VO-值对象(Value Object)并将其返回给业务逻辑层。这样,业务逻辑层就不需要了解持久性数据的具体操作,而是直接操作值对象。(JSP页面里不会再有Connection conn = DriverManager.getConnection……什么的了)
这样,DAO模式中的四个参与角色就很明白了:
业务对象Business Object
数据访问对象Data Access Object
数据源Data Source
值对象Value Object
DAO模式实际上是一种代理模式,使对象(资源)的访问具有间接性,分类资源层和其他层客户端,如业务层或者表示层。
一个典型的 DAO 实现有以下组件:
一个 DAO 工厂类
一个 DAO 接口
一个实现了 DAO 接口的具体类
数据传输对象(有时称为值对象)
具体的 DAO 类包含访问特定数据源的数据的逻辑。
以上是从Core J2EE Patterns书中DAO一节以及一些网上资料中摘录的,Hibernate作为一个ORM的实现,分离出了持久层的操作,所以也是DAO模式。参照DAO的基本组件构架和“与猫同乐”的那个简单的例子,来分析一下Hibernate的基本构架和关键类。
DAO接口:net.sf.hibernate.Session接口,获取实例的方法SessionFactory.openSession();
Session接口是Hibernate应用的主要接口,一个Session实例是轻量级的,可以轻易的创建和销毁,由于应用程序在调用Hibernate时,会经常性的创建和销毁Session实例,所以Session实例必须是轻量级的。同时,Session不是线程安全的,一个session能并只能被一个线程调用。
Session处理对象的各种持久性操作,如存储、重新载入等,ses.save(princess);
DAO工厂类:net.sf.hibernate.SessionFactory类,生成方法
sessionFactory = new Configuration().configure().buildSessionFactory();
由于Hibernate的配置信息存放在xml里,所以由一个net.sf.hibernate.cfg.Configuration类来完成配置,它的configure()方法将读取hibernate.cfg.xml里的内容,Configuration是使用Hibernate需要的第一个类,以后使用的SessionFactory和Session等多为接口。具体的配置过程参见Configuration类的protected Configuration doConfigure(org.dom4j.Document doc) throws HibernateException;方法。
SessionFactory接口向应用程序提供Session实例,SessionFactory不是轻量级的,所以最好被所有的应用共享,通常为每个数据源创建一个SessionFactory。SessionFactory会处理各个ORM配置XML(Cat.hbm.xml),在它的实现类的构造函数里就载入所有的Mapping配置,见net.sf.hibernate.impl.SessionFactoryImpl的构造函数。
与猫同乐(Tomcat+Hibernate)
这是一个Hibernate文档中的简单例子,环境是Tomcat+MySql+Hibernate
下载了Tomcat5.0,安装……
下载了Hibernate2,解包……
下载了MySql-nt5.0,安装……
下载数据库驱动mysql-connector-java-3.0.15-ga,解包……
数据库连接池配置:把数据库驱动mysql-connector-java-3.0.15-ga-bin.jar拷贝到%Tomcat%\common\lib下,在%Tomcat%\conf\server.xml里找到<Host>……</Host>,在其间添加
<Context path="/mytest" docBase="mytest" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!-- MySQL dB username and password for dB connections -->
<parameter>
<name>username</name>
<value>root</value>
</parameter>
<parameter>
<name>password</name>
<value>root</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/test</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
</ResourceParams>
</Context>
解释:Context里的path是应用程序的路径,就是%tomcat%\webapps下的目录。
然后配置Hibernate:
在%tomcat%\webapps下建一个mytest目录,以及相应的WEB-INF、classes、lib目录,将Hibernate2.jar拷到lib下,将Hibernate解包后的lib目录下需要用到的包也拷过去:
cglib-full-2.0.2.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.4.jar
ehcache-0.9.jar
jta.jar
log4j-1.2.8.jar
odmg-3.0.jar
log4j还有一个properties文件要拷到classes目录下
在WEB-INF下的web.xml里添加数据库连接池的设置
<web-app>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
这用来指明数据库连接池。然后是设置Hibernate使用这个容器管理的连接池来连接数据库:在classes目录下建一个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>
<property name="connection.datasource">java:comp/env/jdbc/mysql</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这里可以看到连接的设置,其中dialect是用来指明一个类,来处理数据库数据类型和Java数据类型之间的对照的。<mapping resource="Cat.hbm.xml"/>是映射设置,下面会提到。先看看根据数据库表结构制作的java bean:
数据库结构为:CAT
Column | Type | Modifiers
--------+-----------------------+-----------
cat_id | character(32) | not null
name | character varying(16) | not null
sex | character(1) |
weight | real |
Indexes: cat_pkey primary key btree (cat_id)
对应的java bean源程序:
package net.sf.hibernate.examples.quickstart;
public class Cat {
private String id;
private String name;
private char sex;
private float weight;
public Cat() {
}
public String getId() {
return id;
}
private void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
然后在classes目录下创建一个Cat.cfg.xml来配置PO对象:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="net.sf.hibernate.examples.quickstart.Cat" table="CAT">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>
在这里可以看到java bean类net.sf.hibernate.examples.quickstart.Cat和数据库表CAT的映射关系。
编译好Cat类,放到相应目录下,OK,配置就完成了。下面是在JSP里应用的例子。
首先我们再创建一个类HibernateUtil,用来处理一些诸如取/关Session的功能,这些功能也可以直接写在JSP里:
package net.sf.hibernate.examples.quickstart;
import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
然后是Cat.JSP,很简单
<%@ page language="java" pageEncoding="GB2312" %>
<%@ page import="net.sf.hibernate.Transaction"%>
<%@ page import="net.sf.hibernate.Session"%>
<%@ page import="net.sf.hibernate.cfg.*"%>
<%@ page import="net.sf.hibernate.Query"%>
<%@ page import="net.sf.hibernate.examples.quickstart.HibernateUtil"%>
<%@ page import="net.sf.hibernate.examples.quickstart.Cat"%>
<%@ page import="java.util.*"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en"><html><head><title>Lomboz JSP</title></head><body bgcolor="#FFFFFF">
<%
//添加一只Cat
Session ses = HibernateUtil.currentSession();
Transaction tx= ses.beginTransaction();
Cat princess = new Cat();
princess.setName("ahxu");
princess.setSex('F');
princess.setWeight(7.4f);
ses.save(princess);
tx.commit();
HibernateUtil.closeSession();
//读取库里所有Cat
ses = HibernateUtil.currentSession();
tx= ses.beginTransaction();
Query query = ses.createQuery("select c from Cat as c where c.sex = :sex");
query.setCharacter("sex", 'F');
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println("Female Cat: " + cat.getName() );
}
tx.commit();
HibernateUtil.closeSession();
%>
</body>
</html>
学习Hibernate
Hibernate是一个开放源代码的O/R Mapping (对象关系映射)框架,它对JDBC进行了轻量级的对象封装,使Java程序员可以随心所欲的使用对象编程思维来操纵数据库。由于EJB在部署、调试等方面使用不便,而JDO尚处于开发阶段,还没有成熟统一的标准,Hibernate虽然并不是java官方的持久层方案,仍然被越来越多的人所使用,以至于在即将发布的EJB3.0的标准中,也能看到不少Hibernate的影子。