![](/img/trans.png)
[英]Hibernate makes insert twice and results in unique key constraint violation
[英]Hibernate Violation of UNIQUE KEY constraint
我正在嘗試使用Hibernate在MS SQL中插入對象,如果該對象在數據庫中不存在。 我叫find查詢方法,它返回空列表,但實際上在表中有這個對象! 結果,當我調用insert方法時,hibernate希望插入對象,因為它認為表為空,因此會拋出違反UNIQUE KEY約束的情況 ! 它可以在Oracle上正常工作。 這可能是由於我的許可,架構或目錄引起的嗎?
<property name="schemaUpdate" value="${schemaUpdate}"/>
<property name="hibernateProperties">
<props>
<!--<prop key="hibernate.default_schema">${database.schema}</prop>-->
<prop key="hibernate.default_schema">${database.schema}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
<prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.use_outer_join">true</prop>
和,
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.show_sql=true
#true: if the database is needed to be created otherwise false
schemaUpdate=false
hibernate.hbm2ddl.auto=update
base.packages=com.cityid
database.schema=dbo
hibernate.cache.provider_class=net.sf.ehcache.hibernate.SingletonEhCacheProvider
REM hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
如前所述,唯一約束異常被拋出。 我認為您執行的查詢與您執行的本機查詢不同,或者您遇到字符集問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.