Hibernate
Tapestry4正式版在2006年1月7日终于发布了。这一天,我等了半年。从beta2试用开始,一直等到beta13,再从rc1等到rc3.
之所以等到正式版出台,才开始学习历程,是因为在beta2试学3天,一无所获,错误千出。
学习了10天,终于入门,有感Tapestry4资料之少,写本文,希望能够有助于后来者,少走弯路。
Tapestry学习资料少,其中多为Tapestry3的资料,Tapestry4的资料则少之少,而且T4与T3差异甚大,有不少地方是不一样的。
本文简介以主流的开发框架:Tapestr4+Sping+Hibernate,eclipse 3.1为背景。
一,T4学习曲线在java的众多web 框架中以长且陡著称,入门要多长时间?
老夫愚钝,用了10个工作日。估计,少年才俊7天就可以了。但若无任何TTapestry基础,5天是少不了的。
二,特别提示一:
目前最新的Spring官方指南(V1.26)关于Spring+Tapestry结合部分,只适用于Tapestry3,不必浪费时间了。
Spring+Tapestry4主要参考是Tapestry4Spring.国内的干扁四季豆的相关部分基本上是照搬。
三,T4正式版自带了几个例子:Vlib,Workbench,我个人看并不怎么样,不是最佳入门例程。对无基础者尤其无用。不要浪费太多时间去试图编译通过。非要编译它,也不要相信其文档所言。我的看法是,很难自动完成,得多次手工干预。其实,不理也罢。
四,正式版所带的文档比较有价值,尤其水UserGuide,40来个components(For,insert...)介绍应该是必读,值得打印。特别提示:T4文档对打印比较友好,修改print.css即可,既省墨,又可有更好的阅读效果。
五,我个人最感谢的是Enjoying Web Development with Tapestry,这本书针对T4进行了更新,也是T4网站推荐的几本书中的第二本。
这本书是我看的材料中最符合初学者友好的教程,提供前4章免费下载,并且可以下载全书代码。我打印了这4章,作为重点教材仔细阅读。
书中的代码,可以轻松地直接导入到ecipse,大大节省了打字时间,而且不象很多网上教程,代码基本上没有错误,可以直接编译通过。
六,2006年ibm dw上面的资料被吹捧得很高,我个人看,没有价值,都是重复的大陆货,不值得一读。
不要花太多时间去google,baidu,或者去javaeye,springforum之类的站点,把我上面提到的一本书,几个文档仔细阅读,就可以了。
虽然基本上是英文的,但语言都很通俗,四级水平应该就可以了。
之所以等到正式版出台,才开始学习历程,是因为在beta2试学3天,一无所获,错误千出。
学习了10天,终于入门,有感Tapestry4资料之少,写本文,希望能够有助于后来者,少走弯路。
Tapestry学习资料少,其中多为Tapestry3的资料,Tapestry4的资料则少之少,而且T4与T3差异甚大,有不少地方是不一样的。
本文简介以主流的开发框架:Tapestr4+Sping+Hibernate,eclipse 3.1为背景。
一,T4学习曲线在java的众多web 框架中以长且陡著称,入门要多长时间?
老夫愚钝,用了10个工作日。估计,少年才俊7天就可以了。但若无任何TTapestry基础,5天是少不了的。
二,特别提示一:
目前最新的Spring官方指南(V1.26)关于Spring+Tapestry结合部分,只适用于Tapestry3,不必浪费时间了。
Spring+Tapestry4主要参考是Tapestry4Spring.国内的干扁四季豆的相关部分基本上是照搬。
三,T4正式版自带了几个例子:Vlib,Workbench,我个人看并不怎么样,不是最佳入门例程。对无基础者尤其无用。不要浪费太多时间去试图编译通过。非要编译它,也不要相信其文档所言。我的看法是,很难自动完成,得多次手工干预。其实,不理也罢。
四,正式版所带的文档比较有价值,尤其水UserGuide,40来个components(For,insert...)介绍应该是必读,值得打印。特别提示:T4文档对打印比较友好,修改print.css即可,既省墨,又可有更好的阅读效果。
五,我个人最感谢的是Enjoying Web Development with Tapestry,这本书针对T4进行了更新,也是T4网站推荐的几本书中的第二本。
这本书是我看的材料中最符合初学者友好的教程,提供前4章免费下载,并且可以下载全书代码。我打印了这4章,作为重点教材仔细阅读。
书中的代码,可以轻松地直接导入到ecipse,大大节省了打字时间,而且不象很多网上教程,代码基本上没有错误,可以直接编译通过。
六,2006年ibm dw上面的资料被吹捧得很高,我个人看,没有价值,都是重复的大陆货,不值得一读。
不要花太多时间去google,baidu,或者去javaeye,springforum之类的站点,把我上面提到的一本书,几个文档仔细阅读,就可以了。
虽然基本上是英文的,但语言都很通俗,四级水平应该就可以了。
hofman
2006-01-23 21:34:31
阅读:11167
评论:5
引用:0
这是我的实战JSP进阶教程之四。
本文,主要是想给Hibernate初学者,一点可以实际参考的例子。这段代码,就是本站bbs实际使用的代码片断,其实,也是我初学Hibernate的第一个习作,因此,比较幼稚,但也因此,可能更便于初学者理解。
代码比较简单,因此不作过多的说明。其中的HibernateUtil,就是我上篇Jsp教学文章贴的。
package bbs;
// added on 2005-01-03
import net.sf.hibernate.*;
import java.math.BigDecimal;
import java.util.*;
import org.apache.log4j.Logger;
public final class ThreadDAO {
private final static Logger logger = Logger.getLogger(ThreadDAO.class);
public static List getThreads(String fid, String pno) throws HibernateException {
long forumid;
forumid = new Long(fid).longValue();
if(forumid > 9) forumid = 3;
return getThreads0(forumid, pno);
}
public static List getThreads2(String fid, String pno) throws HibernateException {
long forumid;
forumid = new Long(fid).longValue();
// if(forumid > 10) forumid = 3;
return getThreads0(forumid, pno);
}
//用hibernate进行分页显示的例子。
// added on 2004-12-27
public static List getThreads0(long forumid, String pno) throws HibernateException {
List list = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
int PAGE_SIZE = 20;
try {
tx = s.beginTransaction();
String q = "select t from bbs.Threads t where t.forumid="+forumid+
" and t.modvalue>=5 order by t.modvalue desc, t.modificationdate desc ";
Query query = s.createQuery (q);
int pnum = new Integer(pno).intValue();
int start = (pnum-1) * PAGE_SIZE;
/* 设置返回记录的起始位置 */
query.setFirstResult(start);
/* 设置返回记录数量的最大值 */
query.setMaxResults(PAGE_SIZE);
list = query.list();
tx.commit(); } catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return list;
}
// added on 2004-12-27
public static int getTotalThreads(String fid) throws HibernateException {
int total = 0;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
/* 使用hibernate 的session的load()方法, 根据主键获取实例
这是本程序反复使用的方法
*/
Forum forum = (Forum) s.load(Forum.class,new Long(fid));
total = forum.getThreads().intValue();
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return total;
}
// added on 2004-12-29
public static Threads getThreads(String tid) throws HibernateException {
Threads t = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
t = (Threads) s.load(Threads.class,new Long(tid));
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return t;
}
// added on 2004-12-28
public static String getUsername(String uid) throws HibernateException {
User u = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
u = (User) s.load(User.class,new Long(uid));
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return u.getUsername();
}
// added on 2004-12-28
public static String getThreadSubject(String tid) throws HibernateException {
Threads t = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
t = (Threads) s.load(Threads.class,new Long(tid));
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return t.getSubject();
}
/* 更新数据库的update操作 */
// added on 2004-12-29
public static boolean hideThread(String tid) throws HibernateException {
int total = 0;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
Threads t = (Threads) s.get(Threads.class,new Long(tid));
if(t == null) return false;
BigDecimal mVal = t.getModvalue();
BigDecimal negOne = new BigDecimal("-1");
t.setModvalue(mVal.add(negOne));
s.saveOrUpdate(t);
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return true;
}
// added on 2004-12-29
public static boolean topThread(String tid) throws HibernateException {
int total = 0;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
Threads t = (Threads) s.get(Threads.class,new Long(tid));
if(t == null) return false;
BigDecimal mVal = t.getModvalue();
// BigDecmal oneVal = new BigDecimal("1");
BigDecimal oneVal = new BigDecimal("1");
t.setModvalue(mVal.add(oneVal));
s.saveOrUpdate(t);
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return true;
}
}
本文,主要是想给Hibernate初学者,一点可以实际参考的例子。这段代码,就是本站bbs实际使用的代码片断,其实,也是我初学Hibernate的第一个习作,因此,比较幼稚,但也因此,可能更便于初学者理解。
代码比较简单,因此不作过多的说明。其中的HibernateUtil,就是我上篇Jsp教学文章贴的。
package bbs;
// added on 2005-01-03
import net.sf.hibernate.*;
import java.math.BigDecimal;
import java.util.*;
import org.apache.log4j.Logger;
public final class ThreadDAO {
private final static Logger logger = Logger.getLogger(ThreadDAO.class);
public static List getThreads(String fid, String pno) throws HibernateException {
long forumid;
forumid = new Long(fid).longValue();
if(forumid > 9) forumid = 3;
return getThreads0(forumid, pno);
}
public static List getThreads2(String fid, String pno) throws HibernateException {
long forumid;
forumid = new Long(fid).longValue();
// if(forumid > 10) forumid = 3;
return getThreads0(forumid, pno);
}
//用hibernate进行分页显示的例子。
// added on 2004-12-27
public static List getThreads0(long forumid, String pno) throws HibernateException {
List list = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
int PAGE_SIZE = 20;
try {
tx = s.beginTransaction();
String q = "select t from bbs.Threads t where t.forumid="+forumid+
" and t.modvalue>=5 order by t.modvalue desc, t.modificationdate desc ";
Query query = s.createQuery (q);
int pnum = new Integer(pno).intValue();
int start = (pnum-1) * PAGE_SIZE;
/* 设置返回记录的起始位置 */
query.setFirstResult(start);
/* 设置返回记录数量的最大值 */
query.setMaxResults(PAGE_SIZE);
list = query.list();
tx.commit(); } catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return list;
}
// added on 2004-12-27
public static int getTotalThreads(String fid) throws HibernateException {
int total = 0;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
/* 使用hibernate 的session的load()方法, 根据主键获取实例
这是本程序反复使用的方法
*/
Forum forum = (Forum) s.load(Forum.class,new Long(fid));
total = forum.getThreads().intValue();
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return total;
}
// added on 2004-12-29
public static Threads getThreads(String tid) throws HibernateException {
Threads t = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
t = (Threads) s.load(Threads.class,new Long(tid));
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return t;
}
// added on 2004-12-28
public static String getUsername(String uid) throws HibernateException {
User u = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
u = (User) s.load(User.class,new Long(uid));
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return u.getUsername();
}
// added on 2004-12-28
public static String getThreadSubject(String tid) throws HibernateException {
Threads t = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
t = (Threads) s.load(Threads.class,new Long(tid));
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return t.getSubject();
}
/* 更新数据库的update操作 */
// added on 2004-12-29
public static boolean hideThread(String tid) throws HibernateException {
int total = 0;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
Threads t = (Threads) s.get(Threads.class,new Long(tid));
if(t == null) return false;
BigDecimal mVal = t.getModvalue();
BigDecimal negOne = new BigDecimal("-1");
t.setModvalue(mVal.add(negOne));
s.saveOrUpdate(t);
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return true;
}
// added on 2004-12-29
public static boolean topThread(String tid) throws HibernateException {
int total = 0;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
Threads t = (Threads) s.get(Threads.class,new Long(tid));
if(t == null) return false;
BigDecimal mVal = t.getModvalue();
// BigDecmal oneVal = new BigDecimal("1");
BigDecimal oneVal = new BigDecimal("1");
t.setModvalue(mVal.add(oneVal));
s.saveOrUpdate(t);
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return true;
}
}
hofman
2005-04-08 17:53:51
阅读:6710
评论:1
引用:0
Hibernate的配置文件,默认的有2个:hibernate.properties,hibernate.cfg.xml,这二者,可选其一,也可以合并使用。如果存在对相同属性的定义,xml文件会覆盖properties文件的相关内容,“Hibernate In Action"上是这么说的。
这几天,碰到一个问题,那就是,本站有好几个webapp都开始使用Hibernate,如果bbs.war先发布没事,但是如果blog.war先发布,则bbs除了与blog公用的在线用户,其它什么都无法从数据库中读取。
看日志,知道是因为这种发布顺序,则bbs.war中的Forum.cfg.xml、Thread.cfg.xml之类,都没有起作用。
为什么会这样呢?
痛苦了2天。
原来这2个包都使用了hibernate.cfg.xml,并且在HibernateUtil中未使用其它的方式都显式地调用hibernate配置文件。
package token;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
/* 方法一 */
// sessionFactory = new Configuration().configure().buildSessionFactory();
/* 方法二 */
/* sessionFactory = new Configuration()
.addClass(Counter.class)
.addClass(Online.class)
.buildSessionFactory();
*/
/* 方法三 */
sessionFactory = new Configuration().configure("/token.cfg.xml").buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: "
+ ex.getMessage(), 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();
}
}
原来我一直使用的是方法一,而方法一虽然简单,却必须调用hibernate.cfg.xml来初始化Hibernate,如果二个包都用这样的HibernateUtil,则2个Hibernate.cfg.xml只第一个起作用。
方法二,可以解决方法一可能带来的问题。但如果自己了新的POJO,则仍然需要修改HibernateUtil.java。
所以,我试着使用了方法三,也成功了。
这几天,碰到一个问题,那就是,本站有好几个webapp都开始使用Hibernate,如果bbs.war先发布没事,但是如果blog.war先发布,则bbs除了与blog公用的在线用户,其它什么都无法从数据库中读取。
看日志,知道是因为这种发布顺序,则bbs.war中的Forum.cfg.xml、Thread.cfg.xml之类,都没有起作用。
为什么会这样呢?
痛苦了2天。
原来这2个包都使用了hibernate.cfg.xml,并且在HibernateUtil中未使用其它的方式都显式地调用hibernate配置文件。
package token;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
/* 方法一 */
// sessionFactory = new Configuration().configure().buildSessionFactory();
/* 方法二 */
/* sessionFactory = new Configuration()
.addClass(Counter.class)
.addClass(Online.class)
.buildSessionFactory();
*/
/* 方法三 */
sessionFactory = new Configuration().configure("/token.cfg.xml").buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Exception building SessionFactory: "
+ ex.getMessage(), 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();
}
}
原来我一直使用的是方法一,而方法一虽然简单,却必须调用hibernate.cfg.xml来初始化Hibernate,如果二个包都用这样的HibernateUtil,则2个Hibernate.cfg.xml只第一个起作用。
方法二,可以解决方法一可能带来的问题。但如果自己了新的POJO,则仍然需要修改HibernateUtil.java。
所以,我试着使用了方法三,也成功了。
hofman
2005-01-13 23:45:49
阅读:3314
评论:0
引用:0
