0

I'm new to hibernate and trying to understand it better. I've been attempting to ingest data into my database and on my Track Table there is a unique constraint on the track_uuid.

I've attempted to do just a save and catch the exception and move on to next insert however I started using up to many connections.

So I started looking around and found that a session has a function saveOrUpdate. http://www.javabeat.net/difference-between-hibernates-saveupdate-and-saveorupdate-methods/

However when I change my save to saveOrUpdate I continue to get an error saying duplicate key value violates unique constraint "track_track_uuid_key".

My code:

/**
 * @param p_track the track to parse
 * @param p_dbTrackMessage the database track message
 * @param p_session the database session
 * @return {@link database.model.Track} Returns the ingested database track
 * @throws Exception error
 */
private static database.model.Track parseTrack(final Track p_track,
        final database.model.TrackMessage p_dbTrackMessage,
        final Session p_session) throws Exception {

    // the db track model
    final database.model.Track dbTrack = new database.model.Track();

    // get and set track uuid
    dbTrack.setTrackUuid(p_track.getTrackUUID());

    // get and set track number
    dbTrack.setTrackNumber(p_track.getTrackNumber());

    // get and set the track excerise indicator
    dbTrack.setTrackExerciseIndicator(EnumDatabaseLoader.loadByName(
            TrackExerciseIndicator.class, p_track.getExerciseIndicator().name(), p_session));

    // get and set track simulation indicator
    dbTrack.setTrackSimulationIndicator(EnumDatabaseLoader.loadByName(
            TrackSimulationIndicator.class, p_track.getSimulationIndicator().name(), p_session));

    // get and set the track status
    final TrackStatus trackStatus = p_track.getTrackStatus();
    if (trackStatus != null) {
        dbTrack.setTrackStatus(EnumDatabaseLoader.loadByName(
                database.model.TrackStatus.class, trackStatus.name(),
                p_session));
    }

    try {
        p_session.getTransaction().begin();
        // save the track
        p_session.save(dbTrack);     //  Error happens here I've attempted to change this to saveOrUpdate getting same error.

        // create the mapping
        final TrackMessageToTrackMapping trackMessageToTrackMapping =
                new TrackMessageToTrackMapping();
        trackMessageToTrackMapping.setTrackMessage(p_dbTrackMessage);
        trackMessageToTrackMapping.setTrack(dbTrack);

        // save the mapping
        p_session.save(trackMessageToTrackMapping);
        p_session.getTransaction().commit();
    }
    catch (Exception exception) {
        exception.printStackTrace();
        p_session.getTransaction().rollback();
        throw exception;
    }

    // return the track
    return dbTrack;
}

So my question is why does a saveOrUpdate not recognize that the record is already there and attempt to update it instead of coming back with the record already exist with a matching unique constraint on 1 of the columns.

Side note this works fine if the data isnt in the database it loads everything.

Jeremy
  • 935
  • 5
  • 18
  • 33
  • There's a lot wrong with your code. For starters, you're not releasing connections which is why you're getting that CannotAcquireResourceException. Changing to saveOrUpdate won't affect this. As to the difference between save and saveOrUpdate, that is covered extensively in previous questions http://stackoverflow.com/questions/161224/what-are-the-differences-between-the-different-saving-methods-in-hibernate – Taylor Dec 31 '13 at 15:34
  • I close the connection after it leaves this function everytime. The reason for the CannotAcquireResourceException was I was flushing the session each time an exception ocurred. When reading through the stack trace is actually tells you not to do that which solved my problem related to that. – Jeremy Dec 31 '13 at 19:55
  • no, that's not possible. You've since removed your stack trace, so you must have gotten a different one. (I'm familiar with the error message from Hibernate about not flushing a session after an error, that's NOT what you had posted earlier). – Taylor Dec 31 '13 at 20:42

2 Answers2

2

saveOrUpdate is only about primary keys, not arbitrary unique constraints. To decide whether it's a save or update, Hibernate doesn't consult the database at all: it just checks whether the primary key property is set on the object under consideration.

Unfortunately it will be quite difficult to make Hibernate generate the appropriate statement which will not fail on unique constraint violation. You don't have direct control over the generated SQL and the syntax is highly database-specific.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

The stacktrace pasted was showing up error i.e "Not able to open db connection". Not related to save or update.

theexamtime.com