#
RoomType和RoomState的主键是Integer,在get的时候忘记转换为Integer了:
this
.getHibernateTemplate().execute(
new
HibernateCallback() {
public
Object doInHibernate(Session session)
throws
HibernateException {
room.setType((RoomType) session.get(RoomType.
class
, typeId));
room.setState((RoomState) session.get(RoomState.
class
, stateId));
session.update(room);
return
null
;
}
});
没想到hibernate居然大动干做,抛出一大堆的exception
java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: 1
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:1681)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3280)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3329)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:92)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
at org.hibernate.loader.Loader.doQuery(Loader.java:662)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1129)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:749)
at com.phopesoft.hms.room.service.impl.RoomServiceImpl$3.doInHibernate(RoomServiceImpl.java:151)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:334)
这样的错误真是让我一点莫不着头脑,还好从后台打出的hql看到了错误的信息
org.springframework.jdbc.UncategorizedSQLException: Hibernate operation:
could not execute query; uncategorized SQLException for SQL [
...
SQL state [null]; error code [17041]; 索引中丢失 IN 或 OUT 参数::1;
nested exception is java.sql.SQLException: 索引中丢失 IN 或 OUT 参数:: 1
不就是没找到吗,干嘛要这样呢
将String类型转换为Integer就没事了
this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
room.setType((RoomType) session.get(RoomType.class, new Integer(typeId)));
room.setState((RoomState) session.get(RoomState.class, new Integer(stateId)));
session.update(room);
return null;
}
});
在resin和weblogic上跑的很好的程序,今天早上移植到JBoss上 ,页面跑到<ww:if处没法正常运行,报错,换成<c:if 就没有问题了.
一大早的被郁闷了一下
感谢javaeye差沙和cac的回复.
OpenSessionInView默认的FlushMode为
FlushMode.NEVER
可以采用在写保存更新删除代码的时候手动更改FlushMode
this.getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
session.setFlushMode(FlushMode.AUTO);
session.save(user);
session.flush();
return null;
}
});
但是这样做太繁琐了,第二种方式是采用spring的事务声明
<bean id="baseTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="proxyTargetClass" value="true"/>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userService" parent="baseTransaction">
<property name="target">
<bean class="com.phopesoft.security.service.impl.UserServiceImpl"/>
</property>
</bean>
因为lazy的问题,还是采用了OpenSessionInView
一开始我的web.xml配置文件如下:
<filter>
<filter-name>opensession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
写了个保存的代码测试了一下,报如下的错误:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations
are not allowed in read-only mode (FlushMode.NEVER) - turn your Session
into FlushMode.AUTO or remove 'readOnly' marker from transaction definition
at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1076)
at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:616)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:614)
at com.phopesoft.security.service.impl.UserServiceImpl.addUser(UserServiceImpl.java:40)
at com.phopesoft.security.web.UserAction.add(UserAction.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.opensymphony.xwork.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:365)
.
在javaeye中搜索了一下,发现了Readonly的帖子,将web.xml修改为如下:
<filter>
<filter-name>opensession</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
</filter>
保存的代码顺利通过测试,但是删除的代码却不能通过了.出现如下的错误:
org.hibernate.HibernateException: Illegal attempt to associate a collection
with two open sessions
at org.hibernate.collection.AbstractPersistentCollection.setCurrentSession(AbstractPersistentCollection.java:410)
at org.hibernate.event.def.OnUpdateVisitor.processCollection(OnUpdateVisitor.java:40)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:72)
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:790)
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:768)
at org.springframework.orm.hibernate3.HibernateTemplate$25.doInHibernate(HibernateTemplate.java:772)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:766)
at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:762)
同样在论坛里面搜索到遇到这个问题的朋友,回帖说去掉singleSession!
迷惑了
正好这段时间有空,开始研读rails。本身我没有ruby基础,也一直没有太大的兴趣,这本书正好是对没有ruby基础人写的rails教程。
Thanks manning:)