4

In my module that I'm working on, I got this error, which is said caused by org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update and java.sql.BatchUpdateException (the full stack trace is in here : click here).

From what I've read from other posts, this is surely caused by violation of primary key. However I couldn't even replicate the problem so that I can at least trace the real bug and solve the problem. Every time I inserted an identical entry with the one which already in the database, it will simply merge with each other and no error. However, I was getting many of this error in the deployment, so I'm not quite sure what's going on in the deployment server (I'm just a student developer, so I'm still kinda 'noob' in this).

I would appreciate it even if anyone can point me to the direction. Thanks. (Inform me if there's something need to be added)

Here is the snippet of hibernate mapping for the module (hope this would help) :

<hibernate-mapping package="edu.umd.cattlab.schema.cattXML.extensions.VaTraffic"
default-lazy="false" default-cascade="all, delete-orphan" schema="vatraffic">

<typedef class="edu.umd.cattlab.schema.hibernate.util.XMLGregorianCalendarType" name="XMLCal"/>

<class name="VaTrafficAssociatedEvent" table="associated_event">
    <id name="associatedEventId" column="associated_event_id">
        <generator class="sequence">
            <param name="sequence">ritis.associated_event_id_seq</param>
        </generator>
    </id>

    <property name="secondaryEventId" column="secondary_event_id" not-null="true" />
    <property name="unassociatedTimestamp" type="XMLCal" column="unassociated" />
    <property name="autoRelated" column="auto_related" not-null="true" />
    <many-to-one name="relationshipType" column="relationship_type" not-null="true" cascade="none" />
 </class>

This is the part of java code that utilizes the above mapping: click here

vandershraaf
  • 905
  • 3
  • 11
  • 23

2 Answers2

3

You can have more constraints than just a primary key constraint. Could it be you have a foreign key constraint that you are violating? Or maybe a multiple column unique constraint. Could you please include the DDL for the table you are updating?

JustinKSU
  • 4,875
  • 2
  • 29
  • 51
2

In reviewing the logs, it is a violation of the pk constraint. Specifically ERROR: duplicate key violates unique constraint "associated_event_pk"

Trying to determine why this is happening may be a deep dive, but for starters how are you generating values for this field? In your ddl it shows as a "nextval" field but your log appears to indicate there is an explicit value. Where is this value coming from? Why are you not letting postgre set the value itself?

Taylor
  • 3,942
  • 2
  • 20
  • 33
  • Thanks for the insight, but can you explain more on that? I don't quite understand it – vandershraaf Apr 28 '11 at 21:05
  • 1
    The stacktrace is caused by the your associated_event_pk primary key constraint. As in you're breaking your primary key by attempting to add a second primary key to your table that already exists. How are you generating your primary keys? Are you allowing the database to provide you a primary key or are you generating them yourself? Are you attempting to update a object, but instead of calling saveorupdate are you calling save on the hibernate session? – hooknc Apr 28 '11 at 21:15
  • So the primary key is generated by a sequence "ritis.associated_event_id_seq". It will be generated every time a new associated event inserted to the database. However, if the event already exist, it will only update the associated event. Is there something wrong with that? – vandershraaf Apr 28 '11 at 21:21
  • So in looking at your logs, it looks like you're trying to explicitly set the id. Is this correct? If so why are you doing this and not letting the db determine the value itself? Doing this from the application can lead to weird behaviour in high volume scenarios. DB's have this figured out quite dependably, let them deal with it, instead of reinventing the wheel. – Taylor Apr 28 '11 at 21:35
  • Hurmm, actually the module doesn't explicitly determine the id, and in fact, the id keys are all managed by the database (as you can see from the hibernate mapping). What you see in the logs, perhaps those come from the log4j logging that shows any queries that cause error whenever it happens. It has nothing to do with the way the module handles the primary id. – vandershraaf Apr 28 '11 at 21:45
  • The logs are pretty explicit: ERROR: duplicate key violates unique constraint "associated_event_pk". It looks like hibernate is generating the sequence (incorrectly). I tend to work with mysql, not postgre, but the stuff I've read online suggest that the generator class in your mapping should be "native", e.g.: based on http://archives.postgresql.org/pgsql-jdbc/2005-04/msg00130.php – Taylor Apr 28 '11 at 22:11
  • This link seems to also hold some relevant info: http://stackoverflow.com/questions/3778110/hibernate-and-postgresql-generator-class-in-hibernate-mapping-file (this submit comment on enter thing is killing me) – Taylor Apr 28 '11 at 22:13
  • One more point: seeing the relevant java code would be helpful. – Taylor Apr 28 '11 at 22:14
  • (For the java code, I already added to the latest edit). What's the advantage of using native class over sequences class? I can't find it in the documentation. – vandershraaf Apr 28 '11 at 22:57
  • Hmm, that java code is missing some key parts. How do you instantiate/retrieve the event? How do you persist back to session? – Taylor Apr 29 '11 at 15:27