java新手痛苦时期,请教怎么在web应用程序中创建一个一直生存的对象

java新手痛苦时期,请教如何在web应用程序中创建一个一直生存的对象?
以前搞C++,开发模式和思维方式感觉不能用在Web开发里面。

在我的测试project中,使用spring   +   hibernate。我想创建一个Hibernate的SessionFactory,我只想创建它一次,希望能保存它,然后到处使用;或者生成一个全局的applicationContext,供各个DAO从xml中创建bean对象,供整个应用程序使用,但一直不知道如何实现。

另外,哪些高人能否告知,如何在Hibernate中使用oracle的Sequence?我看到generator可以指定为sequense,但配置文件中配置了,我的oracle数据库中那么多sequense,它用哪个呢?我在什么地方告知hibernate这个表用这个sequense,那个表用那个sequeense呢?

初学java的web开发,感觉里面东西太多了。开源的技术也多,一头雾水,我前段时间一直觉得直接用JDBC最直接和直观,干嘛整个hibernate,还这么麻烦?现在对它又稍稍接受了点了。但目前真感觉自己在被这些这么多的技术拖累。也希望好心人指指路!

------解决方案--------------------
把他申明为静态的不就好了```每次创建session时做一次判断

MyEclipse里自动生成是这样的``

public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml ";

private static final ThreadLocal threadLocal = new ThreadLocal();

private static final Configuration cfg = new Configuration();

private static org.hibernate.SessionFactory sessionFactory;


public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();

if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err
.println( "%%%% Error Creating SessionFactory %%%% ");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}

return session;
}

public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}
private HibernateSessionFactory() {
}

}

那啥````ORACLE我没用Hibernate连过``不知道了``
------解决方案--------------------
我用postgresql,Sequence应该跟oracle差不多。给你参考一下。
<?xml version= "1.0 "?>

<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd " >

<hibernate-mapping package= "cn.net.fornet.cms.admin ">

<class name= "User " table= "tb_admin_user " dynamic-insert= "false " dynamic-update= "false ">
<cache usage= "read-write "> </cache>
<id name= "id " type= "long ">
<generator class= "sequence ">
<param name= "sequence "> tb_admin_user_id_seq </param>
</generator>
</id>
<property name= "name " column= "name " type= "string " not-null= "true "/>
<property name= "password " column= "password " type= "string " not-null= "true "/>
</class>

</hibernate-mapping>
------解决方案--------------------
对了,你的第三个问题。对于自己不熟悉的东西,慎重用于产品和项目中,对个人来说这种新技术是有风险的。
------解决方案--------------------
普便的方法是,创造一个对象,然后把它放到context中去...
context.serAttribute()..
这样的对象会在站台的生存期一直存在。