我正在使用Wicket,Spring和Hibernate开发Web应用程序,我遇到了更新记录的问题。我已经验证调用了saveOrUpdate方法,并且域对象中的数据已更改。但是,SQL输出不会显示已对数据库进行任何更改(示例中为UPDATE),并且尚未更新受影响的记录。
我认为使用update()更有意义,但saveOrUpdate()确实设法创建新记录,但它不会更新它们。我已经验证了这个方法被调用,并且传递的UserVO确实包含更新的字段。这是DAO方法:
public class SkuldwebDAOImpl extends HibernateDaoSupport implements SkuldwebDAO {
public void updateUser(UserVO userVO) {
getSession().saveOrUpdate(userVO);
}
}
这是我的属性文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/skuldweb_dev;AUTO=MULTI;CURSOR=READONLY
jdbc.username=
jdbc.password=
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.use_outer_join=true
hibernate.cache.use_query_cache=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider=org.hibernate.cache.HashtableCacheProvider
hibernate.schemaUpdate=true
这是applicationContext.xml中的sessionFactory bean:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="use_outer_join">${hibernate.use_outer_join}</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
<prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider}</prop>
<prop key="hibernate.connection.pool_size">10</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.jdbc.batch_size">1000</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.upbeat.app.skuldweb.domain.UserVO</value>
<value>com.upbeat.app.skuldweb.domain.UserLevelVO</value>
</list>
</property>
<property name="schemaUpdate" value="${hibernate.schemaUpdate}"/>
</bean>
希望你们其中一人可以帮助我。
更新以下是日志中的一些信息(onSubmit()在日志中设置这些条目 - 最后一个条目应该是将请求重定向到另一个页面时(记录应该有已更新)。
[DEBUG] 2010-07-23 00:29:26,302 :org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:239): Using SessionFactory 'sessionFactory' for OpenSessionInViewFilter [DEBUG] 2010-07-23 00:29:26,302 :org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:214): Returning cached instance of singleton bean 'sessionFactory' [DEBUG] 2010-07-23 00:29:26,302 :org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:181): Opening single Hibernate Session in OpenSessionInViewFilter [DEBUG] 2010-07-23 00:29:26,302 :org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:318): Opening Hibernate Session [DEBUG] 2010-07-23 00:29:26,303 :org.hibernate.impl.SessionImpl.(SessionImpl.java:247): opened session at timestamp: 5242215490777088 [TRACE] 2010-07-23 00:29:26,303 :org.hibernate.impl.SessionImpl.setFlushMode(SessionImpl.java:1316): setting flush mode to: NEVER [DEBUG] 2010-07-23 00:29:26,305 :org.apache.wicket.Session.getPage(Session.java:700): Getting page [path = 4:userprofile_form, versionNumber = 0] [DEBUG] 2010-07-23 00:29:26,306 :org.apache.wicket.markup.html.form.persistence.CookieValuePersister.getCookie(CookieValuePersister.java:210): Unable to find Cookie with name=userprofile_form.email and request URI=/upbeat-app-skuld-web/ [TRACE] 2010-07-23 00:29:26,308 :org.hibernate.engine.IdentifierValue.isUnsaved(IdentifierValue.java:127): id unsaved-value: 0 [TRACE] 2010-07-23 00:29:26,308 :org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:546): detached instance of: com.upbeat.app.skuldweb.domain.UserVO [TRACE] 2010-07-23 00:29:26,308 :org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:228): updating detached instance [TRACE] 2010-07-23 00:29:26,308 :org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:295): updating [com.upbeat.app.skuldweb.domain.UserVO#1] [TRACE] 2010-07-23 00:29:26,310 :org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:346): updating [com.upbeat.app.skuldweb.domain.UserVO#1] [TRACE] 2010-07-23 00:29:26,311 :org.hibernate.engine.Cascade.cascade(Cascade.java:138): processing cascade ACTION_SAVE_UPDATE for: com.upbeat.app.skuldweb.domain.UserVO [TRACE] 2010-07-23 00:29:26,312 :org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:239): cascading to saveOrUpdate: com.upbeat.app.skuldweb.domain.UserLevelVO [TRACE] 2010-07-23 00:29:26,312 :org.hibernate.engine.IdentifierValue.isUnsaved(IdentifierValue.java:127): id unsaved-value: 0 [TRACE] 2010-07-23 00:29:26,312 :org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:546): detached instance of: com.upbeat.app.skuldweb.domain.UserLevelVO [TRACE] 2010-07-23 00:29:26,312 :org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:228): updating detached instance [TRACE] 2010-07-23 00:29:26,313 :org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:295): updating [com.upbeat.app.skuldweb.domain.UserLevelVO#1] [TRACE] 2010-07-23 00:29:26,313 :org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:346): updating [com.upbeat.app.skuldweb.domain.UserLevelVO#1] [TRACE] 2010-07-23 00:29:26,313 :org.hibernate.engine.Cascade.cascade(Cascade.java:173): done processing cascade ACTION_SAVE_UPDATE for: com.upbeat.app.skuldweb.domain.UserVO [DEBUG] 2010-07-23 00:29:26,314 :org.apache.wicket.RequestCycle.setRequestTarget(RequestCycle.java:644): replacing request target org.apache.wicket.request.target.component.listener.ListenerInterfaceRequestTarget@676067951[Page class = com.upbeat.app.skuldweb.web.user.UserProfilePage, id = 4, version = 0]->userprofile_form->interface org.apache.wicket.markup.html.form.IFormSubmitListener.IFormSubmitListener (request paramaters: [RequestParameters componentPath=4:userprofile_form pageMapName=null versionNumber=0 interfaceName=IFormSubmitListener componentId=null behaviorId=null urlDepth=-1 parameters={email=john@upbeat.no,userprofile__form2_hf_0=} onlyProcessIfPathActive=false]) with [BookmarkablePageRequestTarget@1030849724 pageClass=com.upbeat.app.skuldweb.web.user.UserProfilePage]
更新2
这是没有getter / setter的UserVO
@Entity @Table(name = "USERS") @NamedQueries({ @NamedQuery(name = "user.getById", query = "from UserVO item where item.id = :id"), @NamedQuery(name = "user.getAllUsers", query = "from UserVO item order by item.registerDate desc"), @NamedQuery(name = "user.countAll", query = "select count(item) from UserVO item"), @NamedQuery(name = "user.getByUsername", query = "from UserVO item where item.username = :username"), @NamedQuery(name = "user.authenticate", query = "from UserVO item where item.username = :username AND item.passwordHash = :passwordHash") }) public class UserVO extends BaseVO {
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") protected long id; @OneToOne(cascade = CascadeType.ALL) protected UserLevelVO userLevelVO; @Basic @Column(name = "USERNAME") protected String username; @Basic @Column(name = "PASSWORD_HASH") protected String passwordHash; @Basic @Column(name = "EMAIL") protected String email; @Temporal(TemporalType.TIMESTAMP) @Column(name = "REGISTER_DATE") protected Date registerDate; @Temporal(TemporalType.TIMESTAMP) @Column(name = "LAST_LOGIN_DATE") protected Date lastLoginDate;
@Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "ID") protected long id; @OneToOne(cascade = CascadeType.ALL) protected UserLevelVO userLevelVO; @Basic @Column(name = "USERNAME") protected String username; @Basic @Column(name = "PASSWORD_HASH") protected String passwordHash; @Basic @Column(name = "EMAIL") protected String email; @Temporal(TemporalType.TIMESTAMP) @Column(name = "REGISTER_DATE") protected Date registerDate; @Temporal(TemporalType.TIMESTAMP) @Column(name = "LAST_LOGIN_DATE") protected Date lastLoginDate;
答案 0 :(得分:25)
Hibernate通常会延迟更新,直到会话被刷新为止。要测试这是否是您的问题,请在更新语句后插入getSession().flush()
。
您如何管理交易?提交会话时将自动刷新,但如果您的事务配置有问题,您可能最终提交JDBC连接但不提交与Hibernate会话绑定的事务。
编辑:根据您的更新,我看到FlushMode在某一行设置为NEVER:
[TRACE] 2010-07-23 00:29:26,303 :org.hibernate.impl.SessionImpl.setFlushMode(SessionImpl.java:1316): setting flush mode to: NEVER
我怀疑这是问题所在。它会导致会话永远不会自动刷新 - 这通常是您想要在只读事务中执行的操作,而不是在修改数据时。看起来你在没有事务的情况下运行(autocommit设置为true - 这不是推荐的方式)。 OpenSessionInViewFilter的Javadoc提供了一些线索:
默认情况下,此过滤器不会刷新Hibernate会话,刷新模式设置为FlushMode.NEVER。它假定与关注刷新的服务层事务结合使用:活动事务管理器将在读写事务期间临时将刷新模式更改为FlushMode.AUTO,并在最后将刷新模式重置为FlushMode.NEVER每笔交易。如果您打算在没有事务的情况下使用此过滤器,请考虑更改默认刷新模式(通过“flushMode”属性)。
换句话说,您有两个选择:将OpenSessionInViewFilter上的flushMode
设置为AUTO,或者关闭自动提交并配置事务管理器,例如HibernateTransactionManager。
答案 1 :(得分:6)
由于您使用的是Spring,我建议您使用Spring的PlatformTransactionManager来管理您的事务。作为事务管理的一部分,Spring会自动刷新会话。这意味着您不必担心代码中的任何这些方面。
Spring有一个OpenSessionInViewFilter,它连接到事务管理器来启动/刷新会话,你可以用Spring的@Transactional注释你的方法,以表明你想要一个特定方法的'write'事务。这应该更新您的记录。