0

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!

3
  • Shouldn't the relationship from model2 to model1 be a one-to-many? Commented Aug 2, 2011 at 22:25
  • I realize this could be pretty confusing. I'm using the many-to-one field as a one-to-one field as a work around for a bug between Andromda and Hibernate. It seems to be something else... Commented Aug 2, 2011 at 23:15
  • 1
    SYS_C005810470 is the unique constraint on the MODEL2.ID column? Commented Aug 3, 2011 at 2:35

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

That definitely would cause problems, but I'm being careful not to do that. The relationship of one-to-many is upheld.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.