0

I have a table containing four columns:

CREATE TABLE `participants` (
 `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
 `name` VARCHAR(128) NOT NULL,
 `function` VARCHAR(255) NOT NULL,
 `contact` VARCHAR(255) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE INDEX `name_function_contact` (`name`, `function`, `contact`)
)

From the application I get participants-objects, which might have values for name, functionand contactwhich are already in that exact matter in the database. In this case I want Hibernate to get me the idof that object, otherwise I want to save the object.

Using saveOrUpdate()I just get an:

org.hibernate.exception.ConstraintViolationException: Duplicate entry 'NAME-FUNCTION-CONTACT: NAME' for key 'name_function_contact'

How can I accomplish this? Thanks a lot!

5
  • 1
    Your application would have to do this itself by first querying the database to pull out any object that matches those 3 fields. If no object is found, then you call save. Otherwise, you set the id on the object with the id you just found, and call save. (I deleted my first comment because I re-read the question and realized you already understood what I typed). Commented Nov 12, 2013 at 16:27
  • Your other option is to do an insert and IGNORE the error, but if there are other fields in the object that need updated, then this approach does not work since those additional fields would not be updated. Commented Nov 12, 2013 at 16:30
  • Ok. Did it quick n dirty: for (Participants p : participants) { try { session.save(p); } catch (ConstraintViolationException e) { p.setId(((Participants) session.createQuery("FROM Participants WHERE name LIKE '" + p.getName() + "' AND function LIKE '" + p.getFunction() + "' AND contact LIKE '" + p.getContact() + "'").uniqueResult()).getId()); } finally { } } Commented Nov 12, 2013 at 16:52
  • Don't forget to call session.save(p) again inside your catch {} block. Also, I would recommend using name= and function= and contact= for clarity/performance instead of using LIKE. If this works for you, you might want to post it as an Answer. Commented Nov 12, 2013 at 18:55
  • ended up doing it slightly different. see my answer Commented Nov 13, 2013 at 9:50

2 Answers 2

1

Since the answers suggested that Hibernate cannot do it on its own (bummer!) I solved it the "native sql" way:

Participants tempParti = ((Participants) session.createQuery("FROM Participants WHERE name = '" + p.getName() + "' AND function = '" + p.getFunction() + "' AND contact = '" + p.getContact() + "'").uniqueResult());
if (tempParti != null) {
    p = tempParti;
} else {
    session.save(p);
}

Works like a charm! Thanks to all of you!

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

2 Comments

Look like this will work great as long as the Participants object does not have any additional internal data that needs persisted at the time of the save. Glad you got a concise solution that works!
true. But it does not :) Thanks again for all the help!
0

I am no expert in Hibernate. But from Mysql perspective, you do the following.

use INSERT IGNORE INTO... to add the value in the table. If the number of rows inserted is 0, then you can manually get the ID of the row by a SELECT statement.

EDIT: LAST_INSERT_ID() was wrong here. I have edited the answer.

Comments

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.