hibernate中关于sessionFactory中session方法问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate中关于sessionFactory中session方法问题相关的知识,希望对你有一定的参考价值。
package com.yourcompany;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
public class AdminDAOFactory
Session session;
Transaction tx;
public void add(Admin admin) throws HibernateException
try
session = SessionFactory.currentSession();//说这个方法不对,为什么?
tx = session.beginTransaction();
//Add a new admin
session.save(admin);
tx.commit ();
catch(HibernateException e)
throw e;
finally
if (tx!=null)
tx.rollback();
SessionFactory.closeSession();//这个方法也不对,,,
解决问题后,给分,谁能告诉我怎么办啊?为什么说currentSession和closeSession方法不存在?对session的使用错了吗?该怎么弄啊?
下面的回答我看了,我也知道什么意思,有谁可以给做个SessionFactory,帮忙定义个currentSession()方法啊,我不会做..做个注册页面用的,,,明天要交个作业,我很想自己做出来,可是我都快疯了还没做出来呢。..帮忙,我还有700分,全给你都行...
把这个类放到你工程src下的util包里面
确保你的hibernate.cfg.xml文件在src目录下面.
在你调用那块把你的SessionFactory换成我这个类的HibernateSessionFactory
你也可以把我的类名改成你的.怎么样都可以.
package util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see @link http://hibernate.org/42.html.
*/
public class HibernateSessionFactory
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
@SuppressWarnings("unchecked")
public static Session currentSession() throws HibernateException
Session session = (Session) threadLocal.get();
if (session == null)
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.openSession();
threadLocal.set(session);
return session;
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
@SuppressWarnings("unchecked")
public static void closeSession() throws HibernateException
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null)
session.close();
/**
* Default constructor.
*/
private HibernateSessionFactory()
参考技术A 因为在这个程序中,你导入的是org.hibernate.SessionFactory;这个类中没有你用的那些静态方法。你这样写是不对的
你自已肯定定义了一个SessionFactory的类(或者是ide帮助你定义的),比如它被定义到了hibernate文件夹,这时你要导入的类应该为
hibernate.SessionFactory,不能导入org.hibernate.SessionFactory.根据hibernate.SessionFactory中定义的方法取得Session,这个方法名是
自己定义的,有可以是SessionFactory.currentSession(),也有可能是SessionFactory.getSession(),就看定义的时候用的是什么方法名了本回答被提问者采纳 参考技术B 一般是要自己写一个SessionFactory ,
建议使用MyEclipse,自动给你产生一个SessionFactory. 参考技术C 应该是 getCurrentSession 而不是 currentSession
Spring管理 hibernate 事务配置的五种方式
Spring配置文件中关于事务配置总是由三个组成部分,DataSource、TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块!
首先我创建了两个类,一个接口一个实现:
- package com.dao;
- public interface UserDao {
- public void getUser();
- }
实现:
- package com.dao.impl;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import com.dao.UserDao;
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
- public void getUser(){
- }
- }
第一种:每个Bean都有一个代理:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <!-- 数据源 -->
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <!-- 定义事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 配置服务层 -->
- <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <bean id="userDao"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <!-- 配置事务管理器 -->
- <property name="transactionManager" ref="transactionManager" />
- <property name="target" ref="userDaoAgency" />
- <property name="proxyInterfaces" value="com.dao.UserDao" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- </beans>
第二种:所有Bean共享一个代理:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <!-- 数据源 -->
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <!-- 定义事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 定义事务 -->
- <bean id="base"
- class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
- lazy-init="true" abstract="true">
- <!-- 配置事务管理器 -->
- <property name="transactionManager" ref="transactionManager" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <!-- 配置服务层 -->
- <bean id="userDao"
- class="com.dao.impl.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 代理对象 -->
- <bean id="userDaoAgency" parent="base">
- <property name="target" ref="userDao" />
- </bean>
- </beans>
第三种:拦截器:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <!-- 数据源 -->
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <!-- 定义事务管理器(声明式的事务) -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 定义事务 -->
- <bean id="transactionInterceptor"
- class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager" ref="transactionManager" />
- <!-- 配置事务属性 -->
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames">
- <list>
- <value>*DaoImpl</value>
- </list>
- </property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- </list>
- </property>
- </bean>
- <!-- 配置服务层 -->
- <bean id="userDaoAgency" class="com.dao.impl.UserDaoImpl">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- </beans>
第四种:使用tx标签配置的拦截器:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 数据源 -->
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <context:annotation-config />
- <context:component-scan base-package="com.dao" />
- <!-- 定义事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 定义事务 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="*" propagation="REQUIRED" />
- </tx:attributes>
- </tx:advice>
- <!-- 定义切面 -->
- <aop:config>
- <aop:pointcut id="interceptorPointCuts" expression="execution(* com.dao.*.*(..))" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
- </aop:config>
- </beans>
第五种:注解:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- 数据源 -->
- <bean id="dataSource"
- class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url"
- value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&characterEncoding=UTF-8" />
- <property name="username" value="root" />
- <property name="password" value="root" />
- <!-- 连接池启动时的初始值 -->
- <property name="initialSize" value="10" />
- <!-- 连接池的最大值 -->
- <property name="maxActive" value="10" />
- <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
- <property name="maxIdle" value="20" />
- <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
- <property name="minIdle" value="10" />
- <property name="defaultAutoCommit" value="true" />
- </bean>
- <!-- 会话工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="mappingLocations">
- <list>
- <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- org.hibernate.dialect.MySQL5Dialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.format_sql">true</prop>
- </props>
- </property>
- </bean>
- <context:annotation-config />
- <!-- 使用注解的包路径 -->
- <context:component-scan base-package="com.dao" />
- <!-- 支持 @Transactional 标记 -->
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <!-- 定义事务管理器 -->
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- </beans>
如果使用了注解,那么实现类应该这样写:
- package com.dao.impl;
- import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
- import org.springframework.stereotype.Component;
- import org.springframework.transaction.annotation.Transactional;
- import com.dao.UserDao;
- @Transactional
- @Component("userDaoAgency")
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
- /**
- * 为方法增加事务处理特性
- */
- @Transactional(readOnly=true)
- public void getUser(){
- }
- }
这样每个方法都能自己定义自己的事务处理!
转自:http://www.iteye.com/topic/1123347
以上是关于hibernate中关于sessionFactory中session方法问题的主要内容,如果未能解决你的问题,请参考以下文章
spring+struts+mybatis中关于报错org.hibernate.exception.GenericJDBCException: Connection is read-only. Que
如何在 JBoss Wildfly 9 中关闭 Hibernate 调试日志记录?
单例设计模式详解+源代码+JDK源码应用——Java设计模式系列学习笔记