我最近一直在使用Struts + Hibernate 框架,有2个疑问,希望大家能分别详细解析。
一、使用MyEclipse 自动增加 Hibernate 框架 调用自动生成的HibernateSessionFactory获得Session是否重复创建SeesionFactory问题。
我用MyEclipse 自动增加 Hibernate 框架,自动生成两个文件分别是:HibernateSessionFactory.java
hibernate.cfg.xml我在 hibernate.cfg.xml 中设置映射、数据库连接等信息
我的感觉我好像我好像在不停的创建SessionFactory ,这样会很消耗服务器资源的,大家看我在访问数据库类中是这样写的: public List query(String hql)throws Exception{
List list=new ArrayList();
Transaction tx=null;
org.hibernate.Session session=HibernateSessionFactory.getSession();
try{
tx = session.beginTransaction();
Query q=session.createQuery(hql);
list=q.list();
tx.commit();
}
catch(Exception e)
{
if(tx!=null)
tx.rollback();
e.printStackTrace();
throw e;
}
finally{
session.close();
}
return list;
} public void update(BaseObject baseBean)throws Exception{
org.hibernate.Session session=HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try{
session.update(baseBean);
tx.commit();
}
catch(Exception e)
{
if(tx!=null)
tx.rollback();
e.printStackTrace();
throw e;
}
finally{
session.close();
}
}
二、MVC的应用
hibernate.model-------a.java //hibernate POJO模型实体类
hibernate.mapping-----a.xml //hibernate 映射文件logic.model-------a.java //业务类
logic.impl -------ManagerImpl.java //业务接口struts.action----AAction.java
Struts.form------aForm.java大家都知道Hibernate 要操作数据库 首先要有两个文件,一个是 模型POJO类,一个是模型的映射文件,当我们查询数据库得到的数据是以类形式存在的,那么当我们前台需要展示这些数据的时候,我们是直接的将Hibernate模型类推到前台逻辑上?还是将hibernate的 模型类的值赋予给 业务类。
如果用Hibernate 是不是 先将 hibernate 的实体类 赋值给 业务类,然后将业务类 赋值给 Struts 的Form类?第一个问题,我个人觉得是没有问题的,但是我看一些产品源码的时候发现他们都不同程度的进行了更改,所以我难免会有一些猜疑。
一、使用MyEclipse 自动增加 Hibernate 框架 调用自动生成的HibernateSessionFactory获得Session是否重复创建SeesionFactory问题。
我用MyEclipse 自动增加 Hibernate 框架,自动生成两个文件分别是:HibernateSessionFactory.java
hibernate.cfg.xml我在 hibernate.cfg.xml 中设置映射、数据库连接等信息
我的感觉我好像我好像在不停的创建SessionFactory ,这样会很消耗服务器资源的,大家看我在访问数据库类中是这样写的: public List query(String hql)throws Exception{
List list=new ArrayList();
Transaction tx=null;
org.hibernate.Session session=HibernateSessionFactory.getSession();
try{
tx = session.beginTransaction();
Query q=session.createQuery(hql);
list=q.list();
tx.commit();
}
catch(Exception e)
{
if(tx!=null)
tx.rollback();
e.printStackTrace();
throw e;
}
finally{
session.close();
}
return list;
} public void update(BaseObject baseBean)throws Exception{
org.hibernate.Session session=HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
try{
session.update(baseBean);
tx.commit();
}
catch(Exception e)
{
if(tx!=null)
tx.rollback();
e.printStackTrace();
throw e;
}
finally{
session.close();
}
}
二、MVC的应用
hibernate.model-------a.java //hibernate POJO模型实体类
hibernate.mapping-----a.xml //hibernate 映射文件logic.model-------a.java //业务类
logic.impl -------ManagerImpl.java //业务接口struts.action----AAction.java
Struts.form------aForm.java大家都知道Hibernate 要操作数据库 首先要有两个文件,一个是 模型POJO类,一个是模型的映射文件,当我们查询数据库得到的数据是以类形式存在的,那么当我们前台需要展示这些数据的时候,我们是直接的将Hibernate模型类推到前台逻辑上?还是将hibernate的 模型类的值赋予给 业务类。
如果用Hibernate 是不是 先将 hibernate 的实体类 赋值给 业务类,然后将业务类 赋值给 Struts 的Form类?第一个问题,我个人觉得是没有问题的,但是我看一些产品源码的时候发现他们都不同程度的进行了更改,所以我难免会有一些猜疑。
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/moisten/hibernate/config/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}}
2.你问的问题比较好,比较普遍的一种方法就是实体类的子类作为业务类,而FormBean会把业务类作为它的属性来解决的,这样即节省了代码又会造成实体类、业务类、FormBean混乱
2.第二个问题,你可以建立自己的javabean,然后将N个实体类作为该javabean的属性,这样会比较灵活一点。这个是填充数据时用actionform。至于你说的展示数据的话,从后台返回的是已经具有业务的model了,应该就是属于MVC中的V了。可以直接request.setAttribute(name,name)进行外网的展示。