15
回答
Hibernate 4 得不到 sessionFactory
终于搞明白,存储TCO原来是这样算的>>>   
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<!-- 
			<property name="configLocation" value="classpath:hibernate.cfg.xml">
			<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
			<property name="configLocations" value="hibernate.model.xml" />
			-->
		<property name="dataSource">
			<ref local="dataSource" /></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<prop key="hibernate.jdbc.fetch_size">50</prop>
				<prop key="hibernate.jdbc.batch_size">25</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.current_session_context_class">managed</prop>
				<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
				<prop key="hibernate.current_session_context_class">thread</prop>
				<!-- 
					<prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop>
					<prop key="hibernate.current_session_context_class">thread</prop> -->
			</props>
		</property>
		<property name="packagesToScan">
			<list>
				<value>lovemie.com.beans</value>
			</list>
		</property>
	</bean>
@Autowired  
    private static SessionFactory sessionFactory;
举报
sjack
发帖于4年前 15回/8K+阅
共有15个答案 最后回答: 4年前
非Annotation时,使用org.springframework.orm.hibernate3.LocalSessionFactoryBean,
     它注入实体类的方式是setMappingResources(), 看看你的映射文件配置是否正确!
--- 共有 1 条评论 ---
sjack我想用 Hibernate 4 的 api , 怎么做呢 ? 4年前 回复

引用来自“小无赖——林勇”的答案

光看这个很难找出问题!起码异常贴出来吧!

@小无赖——林勇 



Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [l ovemie.com.controller.FallsController]: Constructor threw exception; nested exception is java.lang.N ullPointerException at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleI nstantiationStrategy.java:87) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateB ean(AbstractAutowireCapableBeanFactory.java:1006) ... 54 more Caused by: java.lang.NullPointerException at lovemie.com.dao.helper.HibernateUtil.currentSession(HibernateUtil.java:33) at lovemie.com.controller.FallsController.<init>(FallsController.java:34) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java: 39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorIm pl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148) ... 56 more 2013-06-04 10:19:01.016::WARN: Nested in org.springframework.beans.factory.BeanCreationException: E rror creating bean with name 'fallsController' defined in file [G:\JavaStudy\java_workspace_demo\web app\target\classes\lovemie\com\controller\FallsController.class]: Instantiation of bean failed; nest ed exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean cla ss [lovemie.com.controller.FallsController]: Constructor threw exception; nested exception is java.l ang.NullPointerException: java.lang.NullPointerException at lovemie.com.dao.helper.HibernateUtil.currentSession(HibernateUtil.java:33) at lovemie.com.controller.FallsController.<init>(FallsController.java:34) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java: 39)

public class HibernateUtil {

private static Session session;
    
    //创建一个局部线程变量
    @Autowired  
    private static SessionFactory sessionFactory;
    /*
     * 取得当前session对象
     */
    public static Session currentSession() throws HibernateException {
    session = sessionFactory.getCurrentSession();
    return session;
    }




--- 共有 2 条评论 ---
sjack回复 @小无赖——林勇 : 写了 4年前 回复
开源中国首席董事长sessionFactory set 和 get 方法 写没? 4年前 回复

引用来自“小无赖——林勇”的答案

这 个异常 也不是 找不到

sessionFactory

而是 初始化 类 有问题

Autowire 默认 注入 是 Byname


     String rtn = sessionFactory==null?"null":"not null";
    System.out.println("sessionFactory 1 : "+ rtn);
    System.out.println("sessionFactory 2 : "+ sessionFactory);
测试了下, 
sessionFactory 1 : null
sessionFactory 2 : null
sessionFactory 1 : null
sessionFactory 2 : null
get / set 都是有的 。


当有一个方法list 传播行为为Supports,当在另一个方法getPage()(无事务)调用list方法时会抛出org.hibernate.HibernateException: No Session found for current thread 异常。 这是因为getCurrentSession()在没有session的情况下不会自动创建一个

意思大概是:hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到

上次我也遇到这个问题,后面通过这个方法解决:


    每个包含session的方法都是用@Transaction(XXXXX=XXXXX.REQUIRED)

也可以通过这个解决:

<tx:advice id="txAdvice" transaction-manager="txManager">  
    <tx:attributes>  
        <tx:method name="save*" propagation="REQUIRED" />  
        <tx:method name="add*" propagation="REQUIRED" />  
        <tx:method name="create*" propagation="REQUIRED" />  
        <tx:method name="insert*" propagation="REQUIRED" />  
        <tx:method name="update*" propagation="REQUIRED" />  
        <tx:method name="merge*" propagation="REQUIRED" />  
        <tx:method name="del*" propagation="REQUIRED" />  
        <tx:method name="remove*" propagation="REQUIRED" />  
        <tx:method name="put*" propagation="REQUIRED" />  
        <tx:method name="use*" propagation="REQUIRED"/>  
        <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->  
        <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="count*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="list*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="*" read-only="true" />  
    </tx:attributes>  
</tx:advice>



--- 共有 1 条评论 ---
sjacken 、 首先没法得到 sessionFactory , 不知道为什么, 4年前 回复

引用来自“xmut”的答案


当有一个方法list 传播行为为Supports,当在另一个方法getPage()(无事务)调用list方法时会抛出org.hibernate.HibernateException: No Session found for current thread 异常。 这是因为getCurrentSession()在没有session的情况下不会自动创建一个

意思大概是:hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到

上次我也遇到这个问题,后面通过这个方法解决:


    每个包含session的方法都是用@Transaction(XXXXX=XXXXX.REQUIRED)

也可以通过这个解决:

<tx:advice id="txAdvice" transaction-manager="txManager">  
    <tx:attributes>  
        <tx:method name="save*" propagation="REQUIRED" />  
        <tx:method name="add*" propagation="REQUIRED" />  
        <tx:method name="create*" propagation="REQUIRED" />  
        <tx:method name="insert*" propagation="REQUIRED" />  
        <tx:method name="update*" propagation="REQUIRED" />  
        <tx:method name="merge*" propagation="REQUIRED" />  
        <tx:method name="del*" propagation="REQUIRED" />  
        <tx:method name="remove*" propagation="REQUIRED" />  
        <tx:method name="put*" propagation="REQUIRED" />  
        <tx:method name="use*" propagation="REQUIRED"/>  
        <!--hibernate4必须配置为开启事务 否则 getCurrentSession()获取不到-->  
        <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="count*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="list*" propagation="REQUIRED" read-only="true" />  
        <tx:method name="*" read-only="true" />  
    </tx:attributes>  
</tx:advice>



<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 配置事务的传播特性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="persis*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="get*" propagation="REQUIRED" read-only="true"/>  
            <tx:method name="query*" propagation="REQUIRED" read-only="true"/>
			<tx:method name="*" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 方法参与事务  -->
	<aop:config>
		<aop:pointcut id="allManagerMethod" expression="execution(* lovemie.com.dao..*.*(..))" />
		<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
	</aop:config>

<!--  这里有配事务  -->

引用来自“hantsy”的答案

http://my.oschina.net/hantsy/blog/134679

完整代码在 github 上。

https://github.com/hantsy/spring-sandbox/tree/master/snippets/jpa

你的问题,

1. 同时定义了hibernate.current_session_context_class 为managed 和 thread。。。查查 managed, thread 先?

<prop key="hibernate.current_session_context_class">managed</prop> 18 <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop> 19 <prop key="hibernate.current_session_context_class">thread</prop> 20 <!-- 21 <prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</prop> 22 <prop key="hibernate.current_session_context_class">thread</prop> --> 23 </props>

2. Inject 到一个 static field 上了,我不能确定在运行时创造代理会不会有问题。




@Autowired
SessionFactory sf;
Session se;


public FallsController() {
super();

String rtn = sessionFactory==null?"null":"not null";
    System.out.println("sessionFactory a : "+ rtn);
    System.out.println("sessionFactory b : "+ sessionFactory);
   
    rtn = sf==null?"null":"not null";
    System.out.println("sf a : "+ rtn);
    System.out.println("sf b : "+ sf);
}

// 还是得不到 sessionFactory

@hantsy

@xmut


org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean


问下, 这个class 在哪个jar 包里 ? 现在找不到

--- 共有 2 条评论 ---
sjack回复 @xmut : 我在 orm 3.2.3 里为什么没有找到 ? 那能不能用其它方式来使用 Hibernate 4 的 session 呢 ? 4年前 回复
xmutorg.springframework.orm-3.1.3.RELEASE.jar 4年前 回复
顶部