Hibernate 框架设计问题

北京朝五晚九 2010-03-16 03:34:03
我最近一直在使用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类?



第一个问题,我个人觉得是没有问题的,但是我看一些产品源码的时候发现他们都不同程度的进行了更改,所以我难免会有一些猜疑。

...全文
121 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
longlonglong25 2010-03-19
  • 打赏
  • 举报
回复
可以做个BEAN文件将SessionFactory放进去。然后进行实例化调用!!!另外SessionFactory只会创建一次。。。
beanj 2010-03-18
  • 打赏
  • 举报
回复
SessionFactory不是轻量级的。对应一个数据库。只创建一个。
xusheng1018 2010-03-18
  • 打赏
  • 举报
回复
1.HibernateSessionFactory 是重量级的,多实例化几次,项目可能挂掉的,这个只会实例化一次,放心吧,具体可以看源码。
2.第二个问题,你可以建立自己的javabean,然后将N个实体类作为该javabean的属性,这样会比较灵活一点。这个是填充数据时用actionform。至于你说的展示数据的话,从后台返回的是已经具有业务的model了,应该就是属于MVC中的V了。可以直接request.setAttribute(name,name)进行外网的展示。
llqoo 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yujinjin9 的回复:]
1.HibernateSessionFactory 不能被实例化,它里面的都是静态方法,所以你不可能不停的创建SessionFactory
2.你问的问题比较好,比较普遍的一种方法就是实体类的子类作为业务类,而FormBean会把业务类作为它的属性来解决的,这样即节省了代码又会造成实体类、业务类、FormBean混乱
[/Quote]

说得好。。学习
yujinjin9 2010-03-18
  • 打赏
  • 举报
回复
写错了,是又不会造成实体类、业务类、FormBean混乱
yujinjin9 2010-03-18
  • 打赏
  • 举报
回复
1.HibernateSessionFactory 不能被实例化,它里面的都是静态方法,所以你不可能不停的创建SessionFactory
2.你问的问题比较好,比较普遍的一种方法就是实体类的子类作为业务类,而FormBean会把业务类作为它的属性来解决的,这样即节省了代码又会造成实体类、业务类、FormBean混乱
happyfmy 2010-03-18
  • 打赏
  • 举报
回复
类和值都可以在前台用啊。
看你怎么用了
xierenxin 2010-03-18
  • 打赏
  • 举报
回复
只有一个sessionFactory 放心使用
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lirunjie1124 的回复:]
public class HibernateSessionFactory {

/**
* 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;
……
[/Quote]
只创建了一些放在连接池中供使用,你可以放心……
BeyondBob 2010-03-18
  • 打赏
  • 举报
回复
学习。。。。
zidasine 2010-03-18
  • 打赏
  • 举报
回复
只有一个sessionFactory 放心使用
北京朝五晚九 2010-03-18
  • 打赏
  • 举报
回复
public class HibernateSessionFactory {

/**
* 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;
}

}
Defonds 2010-03-16
  • 打赏
  • 举报
回复
不错,支持开源。
panhaichun 2010-03-16
  • 打赏
  • 举报
回复
关键看 HibernateSessionFactory 里面怎么样了
Kevin-anycode 2010-03-16
  • 打赏
  • 举报
回复
学习。。。。。。。。。
生活 2010-03-16
  • 打赏
  • 举报
回复
关注下。。。。。。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧