出现的异常信息:org.springframework.orm.hibernate3.HibernateSystemException: identifier of 

 
an instance of com.skeyedu.entity.SysUser was altered from 21 to 21;
解决办法:这种问题是因为实体类id的类型与数据库的类型不一致导致的,把它们改成一样即可;
 
 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 
 
'sysUserService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: 
 
Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 
 
'sessionFactory' or 'hibernateTemplate' is required
Caused by: 
java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
解决方法:service的实现类中不要继承HibernateDaoSupport
 
 
问题:org.hibernate.LazyInitializationException: failed to lazily initialize a collection 
 
of role: com.johe.edu.model.Role.menus, no session or session was closed
原因:出现这个错误的原因有多种,当我出现这个问题时(动态菜单的输出)在网上找了查了很多相关
 
资料都没解决。最后经过反复测试与一步步的输出找到错误根源,最后推测出是annotation在实体类中
 
的映射有问题。父菜单出来了,但是子菜单又重新调用session,导致出错.我在在登录的方法内输出一
 
下menu菜单内容。结果又好了。但是诡异的事情是,要是不写这个输出语句就会报错,至今不解。
问题:Bean named 'menu' must be of type [java.util.List], but was actually of type 
 
[com.johe.edu.model.Menu]
解决:这个低级的错误是annotation不熟悉导致的,在List类型的属性上注入了Menu对象类型,两种类
 
型不匹配
 
问题:10:06:21,480 ERROR AbstractFlushingEventListener:324 - Could not synchronize database 
 
state with session
org.hibernate.TransientObjectException: object references an unsaved transient instance - 
 
save the transient instance before flushing: com.johe.edu.model.Role
原因:update发生异常Could not synchronize database state with session。我的主键是手动生成的
 
,一开始调试,jsp页面的表单元素的值都被正常的封装到了Action所对应的Form的属性中,仔细一看,
 
原来 是自己粗心大意引起的。虽然在表单中hidden id了,且id的value不为空,但是提交到Action中时
 
,news.getId()为一个新值,而不是jsp页面的hidden id的值了,究其原因,调用news.getId()时,我
 
进行了处理,如果id属性为空,就根据当前时间重新生成一个新值,否则就返回原来的值。当我提 交到
 
Action时,news.getId()返回了一个新值,这就说明hidden id没有正确封装,将hidden 的name 改为
 
news.id,重写启动Tomcat,update测试通过。
     说了这么多,Could not synchronize database state with session这个异常引发的原因就很明
 
显了,当企图更新一个不存在的id实体时,便发生了这样的异常。
 
问题:在保存用户时出现如下错误:
1.org.hibernate.TransientObjectException: object references an unsaved transient instance - 
 
save the transient instance before flushing: com.johe.edu.model.Role
2.org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved 
 
transient instance - save the transient instance before flushing: com.johe.edu.model.Role; 
 
nested exception is org.hibernate.TransientObjectException: object references an unsaved 
 
transient instance - save the transient instance before flushing: com.johe.edu.model.Role
 
原因:首先就由出现问题的缘由分析,大概翻译一下:对象引用了一个为保存的transient实例,然后从
 
后面知道可能是由Role实体类引起的,检查一下Role实体类,确实在里面加了一个user属性,因为当时
 
数据库中没有user的相关字段,但是annotation映射时要往数据库映射,所以不想映射进去,于是我就
 
在user的get方法上加了一个@transient注解,本以为这样就是略过映射,可是现在却出现以上问题,现
 
在只好把它删掉就好了。
问题:org.springframework.orm.hibernate3.HibernateSystemException: a different object with 
 
the same identifier value was already associated with the session: 
 
[com.johe.edu.model.User#62]; nested exception is org.hibernate.NonUniqueObjectException: a 
 
different object with the same identifier value was already associated with the session: 
 
[com.johe.edu.model.User#62]
原因:一个对象绑定了一个session,当修改时就会有两个user对象使用同一个session,这样就造成冲
 
突。解决的办法是:
public void bindRole(User user)
{
User oldUser = this.userDao.getUser(user.getUser_id());
//重新创建一个user对象,否则会报a different object with the same identifier 
//value was already associated with the session: [com.johe.edu.model.User#62];
//nested exception is org.hibernate.NonUniqueObjectException: a different object
//with the same identifier value was already associated with the session: 
//[com.johe.edu.model.User#62]
Role role = new Role();
role.setRole_id(user.getRole().getRole_id());
oldUser.setRole(role);
this.userDao.update(oldUser);
}
如上方法,将action中传过来的user对象重新赋值给一个user对象,这样在存储的时候就会有另一个
 
user对象出现,绑定另一个session。
 
问题:在修改和删除时,没有报错,但是数据库中没有存进和删除数据,页面上也没有变化。
 
解决:在applicationContext配置文件中加了这样的配置:
<!-- hibernate事务管理设置 -->
<bean id="txManager" 
 
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 将sessionFactory 注入事务管理器 -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 对事务管理打开annotation驱动 -->
<tx:annotation-driven transaction-manager="txManager"/>
还有可能是没有在service方法上加@Transactional注解
 
 
问题:14:18:08,801 ERROR JDBCExceptionReporter:234 - Column 'role_name' cannot be null
14:18:08,825 ERROR AbstractFlushingEventListener:324 - Could not synchronize database state 
 
with session
解决:在jsp页面上加上role_name的隐藏域
 
问题:
org.springframework.orm.hibernate3.HibernateSystemException: Illegal attempt to associate a 
 
collection with two open sessions; nested exception is org.hibernate.HibernateException: 
 
Illegal attempt to associate a collection with two open sessions
解决:web.xml中
<filter>
<filter-name>OpenSessionInViewFilter</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>
<init-param>   
 <param-name>flushMode</param-name>   
  <param-value>AUTO</param-value>   
  </init-param>
</filter>
改<param-value>false</param-value>为
<param-value>true</param-value>,在整个请求过程中使用单个session,否则每个数据访问都会产生新的session