日期:2014-05-16  浏览次数:20581 次

Spring+Hibernate+HSQLDB遇到的问题,求协助
框架版本:
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