Hi, I m new to hibernate.
I have written a Hibernate test program on the parent / child relationship but I m getting below exception:
Caused by: java.sql.BatchUpdateException: ORA-02291: integrity constraint (ATSUSER.BOOK_R01) violated - parent key not found
The idea is an author can have many books:
Below are the details:
Test Program
public class Book_test
{
public static void main(String[] args)
{
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
Author author = new Author();
Book book = new Book();
book.setBookName("Kane and Abel");
Set<Book> bookSet = new HashSet<Book>();
author.addBook(book);
session.saveOrUpdate(author);
tx.commit();
session.close();
}
}
Author.java:
public class Author
{
private String authorName;
private int authorId;
private String bookName;
private Set<Book> authorBooks=new HashSet<Book>();
public Author(){}
public Author(String authorName, Set<Book> authorBooks)
{
this.authorName=authorName;
this.authorBooks=authorBooks;
}
public Author(int authorId, String authorName, Set<Book> authorBooks)
{
this.authorId=authorId;
this.authorName=authorName;
this.authorBooks=authorBooks;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public int getAuthorId() {
return authorId;
}
public void setAuthorId(int authorId) {
this.authorId = authorId;
}
public Set<Book> getAuthorBooks() {
return authorBooks;
}
public void setAuthorBooks(Set<Book> authorBooks) {
this.authorBooks = authorBooks;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public void addBook(Book book)
{
if(book==null)
{
setAuthorBooks(new HashSet<Book>());
}
getAuthorBooks().add(book);
}
}
Book.java:
package hibernate;
public class Book
{
private int Id;
private String bookName;
private int authorId;
public Book(){}
public Book(int id, String bookName)
{
this.Id=id;
this.bookName=bookName;
}
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public int getAuthorId() {
return authorId;
}
public void setAuthorId(int authorId) {
this.authorId = authorId;
}
}
Author.hbm.xml:
<hibernate-mapping>
<class name="hibernate.Author" table="author">
<id name="authorId" column="author_id">
<generator class="increment"/>
</id>
<property name="authorName" column="author_name"/>
<property name="bookName" column="book_name"/>
<set name="authorBooks" table="book" cascade="all">
<key column="author_id" />
<one-to-many class="hibernate.Book" />
</set>
</class>
</hibernate-mapping>
Database tables:
table AUTHOR:
AUTHOR_ID NUMBER, Primary key
BOOK_NAME VARCHAR2(40 BYTE),
AUTHOR_NAME VARCHAR2(30 BYTE)
table BOOK:
ID NUMBER(19) NOT NULL,
BOOKNAME VARCHAR2(255 BYTE),
AUTHOR_ID NUMBER NOT NULL FOREIGN KEY (AUTHOR_ID)
REFERENCES AUTHOR (AUTHOR_ID));
Please help to solve the problem.
Thnaks in advance!!!
Regards,
Chandni