我的应用程序包含多个同时运行的线程,并且所有线程都在我的表中插入或更新几行,所以我使用 saveOrUpdate() 来避免重复输入问题。但它发生了好几次。
我的表结构
CREATE TABLE `question` (`id` varchar(100) CHARACTER SET latin1 NOT NULL COMMENT 'It will be the post_id in )
我的 HBM
<class name="com.xminds.bestfriend.frontend.model.Question" table="question" >
<id name="id" type="string">
<column name="id" length="100" />
<generator class="assigned" />
</id>
<many-to-one name="users" class="com.xminds.bestfriend.frontend.model.Users" fetch="select">
<column name="answer_uid" />
</many-to-one>
<many-to-one name="questionType" class="com.xminds.bestfriend.frontend.model.QuestionType" fetch="select">
<column name="question_type_id" />
</many-to-one>
<property name="data1" type="string">
<column name="data1" length="65535" />
</property>
<property name="data2" type="string">
<column name="data2" length="65535" />
</property>
<property name="fbCreatedDate" type="timestamp">
<column name="fb_created_date" length="19" />
</property>
<property name="permaLink" type="string">
<column name="perma_link" length="1000" />
</property>
<property name="fbUpdatedDate" type="timestamp">
<column name="fb_updated_date" length="19" />
</property>
<property name="contentActivity" type="java.lang.Integer">
<column name="content_activity" />
</property>
<property name="nbConsumptions" type="int">
<column name="nb_consumptions" not-null="true" />
</property>
<property name="createdDate" type="date">
<column name="created_date" length="10" />
</property>
<property name="updatedDate" type="date">
<column name="updated_date" length="10" />
</property>
<property name="cover_url" type="string">
<column name="cover_url" length="1000"/>
</property>
<property name="category" type="string">
<column name="category" length="1000"/>
</property>
<property name="width" type="int">
<column name="width" not-null="true" />
</property>
<property name="height" type="int">
<column name="height" not-null="true" />
</property>
<property name="coverOffsetX" type="java.lang.Integer">
<column name="cover_offset_x" />
</property>
<property name="coverOffsetY" type="java.lang.Integer">
<column name="cover_offset_y" />
</property>
<property name="latitude" type="java.lang.Double">
<column name="latitude" precision="12" scale="0" />
</property>
<property name="longitude" type="java.lang.Double">
<column name="longitude" precision="12" scale="0" />
</property>
<property name="is_active" type="boolean">
<column name="is_active" not-null="true" />
</property>
<set name="questionConsumptions" inverse="true">
<key>
<column name="question_id" length="100" not-null="true">
<comment>QuestionId</comment>
</column>
</key>
<one-to-many class="com.xminds.bestfriend.frontend.model.QuestionConsumption" />
</set>
</class>
得到异常
WARN JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000
springframework.dao.DataIntegrityViolationException:无法插入:[com.xminds.bestfriend.frontend.model.Question];SQL [插入问题(answer_uid、question_type_id、data1、data2、fb_created_date、perma_link、fb_updated_date、content_activity、nb_consumptions、created_date、updated_date、cover_url、类别、宽度、高度、cover_offset_x、cover_offset_y、纬度、经度、is_active、id)值( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; 约束[空];嵌套异常是 org.hibernate.exception.ConstraintViolationException: could not insert: [com.xminds.bestfriend.frontend.model.Question] at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:637) at org. springframework.orm.hibernate3.HibernateTransactionManager。
@Override
public void saveOrUpdate(Object instance)
{
//log.debug("attaching dirty instance");
try
{
getHibernateTemplate().saveOrUpdate(instance);
//log.debug("attach successful");
}
catch (RuntimeException re)
{
// log.error("attach failed", re);
throw re;
}
}