论坛首页 Java企业应用论坛

Spring+Hibernate+HSQLDB遇到的问题,求协助

浏览 1527 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-05-11  
框架版本:
Spring 3.1.1
Hibernate 4.1.2
HSQLDB 1.8.0.10

数据库:
HSQLDB主要是使用内存的模式,测试的表只有TEST_MODEL,这个表是从实体映射创建的表。

问题描述:
-------------------------------------------------------------------------------------------------------
执行保存对象操作没有报错,但是执行hql查询或get操作时,却查询不回对象。

具体代码:
-------------------------------------------------------------------------------------------------------

测试第一步:

执行保存数据操作,代码如下:

DAO层:
执行代码“sessionFactory.getCurrentSession().save(testModel));”,能正常执行并生成主键ID,ID生成策略是用uuid。


Service层:
“testDao.saveTestModel(testModel);”,直接调用dao层的保存方法,返回对象ID。

Controller层:
@RequestMapping(params = "method=doSaveTestModel")
public String doSaveTestModel(HttpServletRequest req) {
req.getSession().setAttribute("testId", testService.saveTestModel(new TestModel("test", new Date())));
System.out.println("save success!");
return "test/test";
}
为了方便后面的测试,我把对象ID存入session里。


测试第二步

执行DAO层的更新操作,代码如下:

Controller层:

@RequestMapping(params = "method=doUpdateTestModel")
public String doUpdateTestModel(HttpServletRequest req) {
// 按ID查询对象
List<TestModel> testModelList = testService.queryTestModel(
req.getSession().getAttribute("testId").toString());
if (testModelList.size() > 0) {
TestModel testModel = testModelList.get(0);
testModel.setName("Vincent");
// 执行更新操作
testService.updateTestModel(testModel);
System.out.println("update success!");
}
return "test/test";
}


Service层:

public void updateTestModel(TestModel testModel) {
testDao.updateTestModel(testModel);
}


DAO层:

public void updateTestModel(TestModel testModel) {
System.out.println(sessionFactory.getCurrentSession().merge(testModel));
}


经过上述操作,发现无法进入红色字体的代码体,也就是说查询不到数据。

以下为关键部分的配置文件:
Spring:

<!-- 数据源配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${hsqldb.driverClassName}" />
<property name="jdbcUrl" value="${hsqldb.url}" />
<property name="user" value="${hsqldb.username}" />
<property name="password" value="${hsqldb.password}" />
<property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
<property name="maxStatements" value="${c3p0.maxStatements}" />
</bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>hibernate/TestModel.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop>
<prop key="hibernate.current_session_context_class">${hibernate.current_session_context_class}</prop>
<prop key="jdbc.use_scrollable_resultset">${jdbc.use_scrollable_resultset}</prop>
</props>
</property>
</bean>

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

<!-- 事务配置 -->
<tx:advice id="userTxAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="save" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="create*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />
<tx:method name="modify*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" />

<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
<tx:method name="load*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="pc"
expression="execution(public * net.bingosoft.services.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
</aop:config>


Hibernate4:

<hibernate-mapping>
<class name="net.bingosoft.model.TestModel"  table="TEST_MODEL" lazy="false">
<id name="id" type="java.lang.String" >
<column name="ID" />
<generator class="uuid" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="lastTime" type="java.util.Date">
<column name="LAST_TIME" />
</property>
</class>
</hibernate-mapping>


Web.xml:

<!-- OperSessionInViewFilter -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
  <filter-mapping>
  <filter-name>OpenSessionInViewFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

求协助,等待中,谢谢.....

附件中是完整的项目,使用maven管理jar包。
  • pro.rar (152.6 KB)
  • 下载次数: 12
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics