-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: ERROR: duplicate key value violates unique constraint
PostPosted: Mon Mar 01, 2010 6:00 am 
Newbie

Joined: Mon Mar 01, 2010 5:51 am
Posts: 2
Hi!
I`m Hibernate newbie and coping with problem.

There are two entities: book and its creator(s)

Book:
Code:
@Entity
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id=-1L;
    private String title;
    @OneToMany(cascade ={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    private List<Creator> creator;
    private Lang lang;
    private Date date;
    @CollectionOfElements
    private List<String> keywords;
    @CollectionOfElements
    private List<String> categories;
    private String description;
    private String pdfLink;
    @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
    private User user;

    public Book() {
    }

    public Book(List<Creator> creator, String title, Lang lang, Date date, String description, List<String> keywords, List<String> categories, String pdfLink, User user) {
        this.creator = creator;
        this.title = title;
        this.lang = lang;
        this.date = date;
        this.description = description;
        this.keywords = keywords;
        this.categories = categories;
        this.pdfLink = pdfLink;
        this.user = user;
    }

    public Book(List<Creator> creator, String title, String pdfLink, User user) {
        this.creator = creator;
        this.title = title;
        this.pdfLink = pdfLink;
        this.user = user;
    }

    public Long getId() {
        return id;
    }

    ..getters + setters for every attribute

    @Column(name = "lang")
    @Enumerated(EnumType.ORDINAL)
    public Lang getLang() {
        return lang;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Book book = (Book) o;
        if (!id.equals(book.id)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }
}


Here is simple Creator entity:
Code:

@Entity
public class Creator {
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;
    private String creator;

    public Creator() {
    }

    public Creator(String creator) {
        this.creator = creator;
    }

    public Long getId() {
        return id;
    }
   
    ..getters + setters

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null) return false;
        if (!(o instanceof Creator)) return false;
        final Creator other = (Creator) o;
        if (getCreator() == null) {
            if (other.getCreator() != null)
                return false;
        } else if (!getCreator().equals(other.getCreator()))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        return creator.hashCode();
    }
}


When I`m accessing only Creator entity with my Dao object, saveOrUpdate(Creator c) works well..
When trying to persistance Book entity with its creators - everything works well ONLY when creating DataDefinitionLayer, when I`m trying to update existence book, it throws me an exception

Code:
Hibernate: insert into Book_Creator (Book_id, creator_id) values (?, ?)
1286 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 23505
1287 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Batch entry 0 insert into Book_Creator (Book_id, creator_id) values ('327680', '1') was aborted.  Call getNextException to see the cause.
1288 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 23505
1288 [main] ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: duplicate key value violates unique constraint "book_creator_creator_id_key"
1289 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)


I don`t know what to do, I`ve already read this forum topics but without success..
I`m using PostgreSQL and Hibernate 3.4.0

Thank you very much for your time and help :)


Top
 Profile  
 
 Post subject: Re: ERROR: duplicate key value violates unique constraint
PostPosted: Mon Mar 01, 2010 6:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
When trying to persistance Book entity with its creators - everything works well ONLY when creating DataDefinitionLayer, when I`m trying to update existence book, it throws me an exception


This sentence is very unclear. Try it to explain it more clear with separate sentences.
Describe how you update an existent book.


Top
 Profile  
 
 Post subject: Re: ERROR: duplicate key value violates unique constraint
PostPosted: Mon Mar 01, 2010 9:31 am 
Newbie

Joined: Mon Mar 01, 2010 5:51 am
Posts: 2
So I`m using HibernateDaoSupport.. Code for save or update book instance is this:

Code:
public Book editBook(Book book) {       
        getHibernateTemplate().saveOrUpdate(book);
        getHibernateTemplate().flush();
        return book;
    }


The thing is though, that when the new data definition layer [tables] is created, new key sequences are set. There is no conflict with unique keys.

But when updating, it seems that hibernate is trying insert a row with existing key instead of updating it.
I don`t know why.

Thank for your reply, I hope now it`s more clear now :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.