开发者

Hibernate ConstraintViolationException: could not insert, SQL Error

开发者 https://www.devze.com 2023-03-25 16:00 出处:网络
I have a script which queries my jboss server which results in new hibernate objects being saved.I\'m creating model1 and model2, calling create on model1 and letting cascade=\"save-update\" take care

I have a script which queries my jboss server which results in new hibernate objects being saved. I'm creating model1 and model2, calling create on model1 and letting cascade="save-update" take care of model2. However, this approach is leading to a primary key constraint failure in model2 some of the time, in which case hibernate blows up. Does anyone have any ideas of how I might fix this?

Here's my mapping, and the error I'm getting. Since the error is only happening some of the time (and the data going in is all totally unique) I suspect I may have an issue with jboss returning before the session is fully committed and then the next data to be added starts and gets an ID it shouldn't. Or any thoughts on the matter?

Model1.hbm.xml

<hibernate-mapping default-cascade="none">
<class name="Model1Impl" table="MODEL1" dynamic-insert="false" dynamic-update="false">
    <cache usage="read-write" />
    <id name="id" type="java.lang.Long" unsaved-value="null">
        <column name="ID" sql-type="NUMBER(19)"/>
        <generator class="native">
        </generator>
    </id>
    ...
    <many-to-one name="Model2" class="Model2Impl" foreign-key="MODEL1_MODEL2_FKC" cascade="save-update" lazy="false" fetch="select">
        <column name="MODEL2_FK" not-null="false" sql-type="NUMBER(19)" unique="true"/>
    ...
</class>
</hibernate-mapping>

Model2.hbm.xml

<hibernate-mapping default-cascade="none">
<class name="Model2Impl" table="MODEL2" dynamic-insert="false" dynamic-update="false">
    <cache usage="read-write" />
    <id name="id" type="java.lang.Long" unsaved-value="null">
        <column name="ID" sql-type="NUMBER(19)"/>
        <generator class="native">
        </generator>
    </id>
    ...
    <one-to-one name="Model1" class=Model1Impl"  property-ref="Model2" cascade="none" lazy="proxy" fetch="select"/>
</class>
</hibernate-mapping>

Error

13:42:15,734 ERROR [JDBCExceptionReporter] ORA-00001: unique constraint (DBSERVER.SYS_C005810470) violated

13:42:15,734 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [Model1Impl]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
...

Caused by: java.sql.SQLException: ORA-00001: unique constraint (DBSERVER.SYS_C005810470) violated
    at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
    at oracle.开发者_如何转开发jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
    at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:582)
    at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1986)
    at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java:1144)
    at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:2152)

Thanks so much!


It turns out that this error, as it should have seemed by the "Could not synchronize database," was a database issue.

Turns out one of my sequences got reset (not sure how yet) and all sorts of primary keys were colliding. To fix it I advanced my database sequence to be higher than my highest ID. Thanks for your thoughts, and I hope this helps anyone who finds themselves in similar situations!


Do you have more than one row on Model1 pointing to the same row on Model2? Your MODEL1_MODEL2_FKC column is declared unique, which would prevent that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号