Hibernate saveOrUpdate 方法总是试图更新 [抛出异常] ,当它实际上应该在JBoss EA7 Server中进行插入时。相同的代码在WebSphere Server中完全正常工作
服务器: JBoss EA7
Hibernate :3.2
Spring HibernateTemplate
数据库: Microsfot SQL
错误消息:无法与会话同步数据库状态:org.hibernate.StaleStateException:批量更新从update [0]返回意外行数;实际行数:0;预期:1
原因:在JBoss服务器中,代码总是尝试执行更新,这会导致上述异常。 有人可以帮我调整配置,使其适用于JBoss 。此代码(使用相同代码插入/更新)在Websphere中完美运行(使用相同代码插入/更新)。提前致谢。
Entity Class:
@Entity
@Table(name = "TAB_SAMPLE", schema = "MYSCHEMA", catalog = "MYSCHEMA", uniqueConstraints = {})
public class TestEntity extends BaseObject<Long> {
private static final long serialVersionUID = -12334423232L;
/** default constructor */
public TestEntity() {
}
/** minimal constructor */
public TestEntity(Long testId) {
super.setId(testId);
}
/** full constructor */
public TestEntity(Long testId, args......) {
super.setId(testId);
.............
}
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "MASTER_ID", unique = true, nullable = false, insertable = true, updatable = true)
public Long getId() {
return super.getPersistentId();
}
}
//class which sets the Id
@MappedSuperclass
public abstract class BaseObject<PK> implements Serializable {
private PK id;
/**
* GETTER must be defined in the subclass so that JPA attributes can be annotated there
* public PK getId();
*/
public final void setId(PK id) {
this.id = id;
}
@Override
public abstract String toString();
@Override
public abstract boolean equals(Object o);
@Override
public abstract int hashCode();
@Transient
protected PK getPersistentId() {
return this.id;
}
}
Code snippet which invokes the saveOrUpdate method
public T save(T entity) {// I did debug, and found the id=0 at the entity class.
super.getHibernateTemplate().saveOrUpdate(entity);
getHibernateTemplate().flush();
return entity;
}
配置:Spring
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.mycompany.TestEntity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
<prop key="hibernate.default_schema">MYSCHEMA</prop>
<prop key="hibernate.connection.isolation">1</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- Create/update the database tables automatically when the JVM starts up
<prop key="hibernate.hbm2ddl.auto">update</prop> -->
<!-- Turn batching off for better error messages under PostgreSQL
<prop key="hibernate.jdbc.batch_size">0</prop> -->
<prop key="hbm2ddl.auto">create</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>
</beans>