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 :)