struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了

J2EE 码拜 9年前 (2015-04-05) 768次浏览 0个评论

这个问题已经两天了,诶愁死我了,快来大神帮忙帮忙看看呗???
具体问题如下:
如题版本的ssh框架整合,还用了一点点spring security3.1.0里面的一小点东西,只是用了一个扩展UserDetails,重写了一个UserDetailsService再配置上去了,发现这一块是没什么问题的,就测试发现取不到数据库里的数据,更可恶的是居然不报错,什么都没提示,就是拿不到。结果后来我测试跟踪了,发现源头在Hibernate的createQuery(String hql)这个方法上,调用这个方法后什么都不返回,然后下一句的System.out.println()都没给执行了,控制台上还是什么都不提示,偶滴神啊,这是要作死的节奏啊。我用的数据库是MySql5.0的。
下面是我的程序的一部分,信息不全我再贴点出来,大神大神你在哪里呀
果断先贴上配置,我用的是注解的方式配置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: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-3.1.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd 
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.1.xsd
	http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

	<description>spring公共配置文件</description>

	<!--   配置注解扫描目录 -->  
	<context:component-scan base-package="com.iquante.dg.action" />
	<context:component-scan base-package="com.iquante.dg.service" />
	<context:component-scan base-package="com.iquante.dg.dao" />
	<context:component-scan base-package="com.iquante.dg.pojo" />
	<!-- 读入属性文件 -->
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:com/iquante/dg/config/jdbc.properties</value>
			</list>
		</property>
	</bean>
	<!--  配置数据源 --> 
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass"><value>${jdbc.driver_class}</value></property>
		<property name="jdbcUrl"><value>${jdbc.url}</value></property>
		<property name="user"><value>${jdbc.username}</value></property>
		<property name="password"><value>${jdbc.password}</value></property>
		<!--连接池中保留的最小连接数 -->
		<property name="minPoolSize" value="${c3p0.min_pool_size}" />
		 <!--连接池中保留的最大连接数,默认为15 -->
		<property name="maxPoolSize" value="${c3p0.max_pool_size}" />
		<!-- 初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间 -->
		<property name="initialPoolSize" value="${c3p0.initial_pool_size}" />
		 <!--最大空闲时间,单位秒,若为0则永不丢弃,默认为0 -->
		<property name="maxIdleTime" value="${c3p0.max_idle_time}" />
		 <!--当连接池中的连接耗尽时c3p0一次同时获取的连接数,默认为3 -->
		<property name="acquireIncrement" value="${c3p0.acquire_increment}" />
		 <!--检查连接池中所有的空闲时间间隔,单位秒,默认为0 -->
		<property name="idleConnectionTestPeriod" value="${c3p0.idle_connection_test_period}" />
		 <!--定义在从数据库获取新连接失败后重复尝试的次数,默认为30 -->
		<property name="acquireRetryAttempts" value="${c3p0.acquire_retry_attempts}" />
	</bean>

	<!-- Hibernate SessionFactory的配置 -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource"/>
		</property> 

		<!-- Hibernate相关配置 -->
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialog">${hibernate.dialog}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
			</props>
		</property>
	</bean>

<!-- 	 EHCache二级缓存的配置
	 配置EHCache缓存管理工厂 
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
		<property name="configLocation">
			<value>classpath:com/iquante/dg/ehcache.xml</value>
		</property>
	</bean>
	 配置EHCache缓存工厂 
	<bean id="cache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
		<property name="cacheManager" ref="cacheManager" />
		<property name="cacheName">
			<value>EHCache</value>
		</property>
	</bean> -->

	<!-- 事务管理的配置 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- 使用annotation定义事物 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

</beans>]

接下来是我的BaseDaoImpl,下面的UserDaoImpl就是去继承这个类的

/**
 * @author JOU
 * @see com.iquante.dg.dao.BaseDao
 * @since Iquante/1.0
 * Copyright 2014 LeXiao Corporation All Rights Reserved
 */
package com.iquante.dg.dao.impl;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.iquante.dg.dao.BaseDao;

/**
 * 基本dao实现类
 * @param T 泛型
 */
@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDao<T> {

	/**
	 * 所确定的泛型pojoClass
	 */
	private Class<T> pojoClass;

	/**
	 * HibernaterTemplate(相当于session)
	 */
	@Resource
	private SessionFactory sessionFactory;

	/**
	 * @return the pojoClass
	 * @since Iquante/1.0
	 */
	public Class<T> getPojoClass() {
		return pojoClass;
	}

	/**
	 * @param pojoClass the pojoClass to set
	 * @since Iquante/1.0
	 */
	public void setPojoClass(Class<T> pojoClass) {
		this.pojoClass = pojoClass;
	}

	/**
	 * @return the sessionFactory
	 * @since Iquante/1.0
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * @param sessionFactory the sessionFactory to set
	 * @since Iquante/1.0
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/** constructor */
	public BaseDaoImpl() {

	}

	protected void init() {
		Type genType = getClass().getGenericSuperclass();
		ParameterizedType pt = (ParameterizedType)genType;
		pojoClass = (Class<T>)pt.getActualTypeArguments()[0];
	}

	public Session openSession() {
		return this.getSessionFactory().getCurrentSession();
	}

	/**
	 * load pojo对象
	 * @param id pojo id
	 * @return T
	 * @see com.iquante.dg.dao.BaseDao#load(Serializable)
	 * @since Iquante/1.0
	 */
	@Override
	public T load(Serializable id) {
		// TODO Auto-generated method stub
		return (T) this.getSessionFactory().getCurrentSession().load(pojoClass, id);
	}

	/**
	 * get pojo对象
	 * @param id pojo id
	 * @return T
	 * @see com.iquante.dg.dao.BaseDao#get(Serializable)
	 * @since Iquante/1.0
	 */
	@Override
	public T get(Serializable id) {
		// TODO Auto-generated method stub
		return (T) this.getSessionFactory().getCurrentSession().get(pojoClass, id);
	}

	/**
	 * 获取pojo的所有对象
	 * @return List<T>
	 * @@see {@link com.iquante.dg.dao.BaseDao#loadAll()}
	 * @since Iquante/1.0
	 */
	@Override
	public List<T> loadAll() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 保存pojo对象
	 * @param pojo pojo对象
	 * @see com.iquante.dg.dao.BaseDao#save(Object)
	 * @since Iquante/1.0
	 */
	@Override
	public void save(T pojo) {
		// TODO Auto-generated method stub

	}

	/**
	 * 移除pojo对象
	 * @param pojo pojo对象
	 * @see com.iquante.dg.dao.BaseDao#remove(Object)
	 * @since Iquante/1.0
	 */
	@Override
	public void remove(T pojo) {
		// TODO Auto-generated method stub

	}

	/**
	 * 更新pojo对象
	 * @param pojo pojo对象
	 * @see com.iquante.dg.dao.BaseDao#update(Object)
	 * @since Iquante/1.0
	 */
	@Override
	public void update(T pojo) {
		// TODO Auto-generated method stub
	}
	/**
	 * 根据HQL语言查询
	 * @param hql HQL语言
	 * @return List
	 * @see com.iquante.dg.dao.BaseDao#find(String)
	 * @since Iquante/1.0
	 */
	@Override
	public List find(String hql) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 根据HQL语言传参查询
	 * @param hql HQL语言
	 * @param parem 参数
	 * @return List
	 * @since Iquante/1.0
	 */
	@Override
	public List find(String hql, String param) {
		// TODO Auto-generated method stub
		System.out.println("List find中");
		List list = this.getSessionFactory().openSession().createQuery(hql).setString(0, param).list();
		System.out.println("sdfdsfdsf");
		return list;
	}
	/**
	 * 根据HQL语言传参查询
	 * @param hql HQL语言
	 * @param parems 参数
	 * @return List
	 * @see com.iquante.dg.dao.BaseDao#find(String, Object[])
	 * @since Iquante/1.0
	 */
	@Override
	public List find(String hql, Object[] parems) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 对延迟加载的pojo对象执行初始化
	 * @param pojo pojo对象
	 * @see com.iquante.dg.dao.BaseDao#initialize(Object)
	 * @since Iquante/1.0
	 */
	@Override
	public void initialize(T pojo) {
		// TODO Auto-generated method stub
	}
}

接下来的是UserDaoImpl类

package com.iquante.dg.dao.impl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.iquante.dg.constant.UserConstant;
import com.iquante.dg.dao.UserDao;
import com.iquante.dg.pojo.User;
/**
 * 用户dao实现类
 */
@Repository("userDao")
public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao<User> {

	public UserDaoImpl() {
		super.init();
	}
	/**
	 * 通过手机获取用户对象
	 * @param mobilePhone 手机
	 * @return User
	 * @since Iquante/1.0
	 */
        @Transactional
	@Override
	public User getUserByMobilePhone(String mobilePhone) {
		// TODO Auto-generated method stub
		String hql = "from User as user where user.mobilePhone=?";
		System.out.println("UserDaoImpl里");
		System.out.println(this.getSessionFactory());
		User user = (User) this.getSessionFactory().openSession().createQuery(hql).setString(0, mobilePhone);
		System.out.println(user);
		return user;
	}
}

MySql仅有一条数据当做测试的
struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了

struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了
15分
你忘了执行HQL了 一般createQuery 后边还有 什么 list()  un….. exe….这三个 是执行语句的动作
struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了
其实我之前也也加过.list()的,还是没用,可能之后修改的时候忘记加这句了。能帮我再看看配置什么的是不是有问题了呢
struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了
25分
@Test
	public void saveTest() {
		Session s = sessionFactory.openSession();
		for (int i = 0; i < 100; i++) {
			Tuser t = new Tuser(i + "");
			s.save(t);
		}
		s.close();
	}

	@Test
	public void getTest() {
		String mobilePhone = "1";
		Session s = sessionFactory.openSession();
		Tuser t = (Tuser) s.createQuery("from Tuser t where t.mobilePhone=?")
				.setParameter(0, mobilePhone).uniqueResult();

		logger.info(t);
		logger.info(json(t));

		s.close();

	}

Hibernate: 
    select
        tuser0_.ID as ID0_,
        tuser0_.MOBILEPHONE as MOBILEPH2_0_ 
    from
        TUSER tuser0_ 
    where
        tuser0_.MOBILEPHONE=?
[test.UserDaoImplTest]entity.Tuser@22976eda
[test.UserDaoImplTest]{“id”:3,”mobilePhone”:”1″}

这怎么会出不来呢,我这就建了一个Dao就出来了呀。

struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了
你的 Hibernate是4.1.4的吗?那我再试试看。还有哦,给HQL语句赋值怎么是setParameter呢,怎么不是setString呢?
struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了
10分
如果后面的out都没执行的话,那可能是个Error,而不是个Exception,暂时用捕获一下Throwable试试。
struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了
我终于把这个问题解决了,原来是Hibernate4的不同,需要配置hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext,没配置这个的话getcurrentsession方法不会去用当前session,所以需要配置这个东西去绑定到springcontext中去用,这个是用JUnit工具测试出来的,真tm好用啊
struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了
请问你是怎么解决的呢,我把 hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext 加了还是不行,这是我的applicationCountext.xml 代码:

<?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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-3.1.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd">

	<context:component-scan base-package="com.hirisun.xx.sshTest" />
	<!-- 数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
		<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
		<property name="username" value="workUser " />
		<property name="password" value="work" />
	</bean>
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
			</props>
		</property>
		<property name="packagesToScan">
			<list>
				<value>com.hirisun.xx.sshTest.bean</value>
			</list>
		</property>
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>

帮我看看问题出那了,谢谢了。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明struts2.1+spring3.1.1+hibernate4.1.4取不到数据,运行creageQuery()跳出未执行了
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!