This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes hibernate parent/child relationship Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate parent/child relationship" Watch "hibernate parent/child relationship" New topic
Author

hibernate parent/child relationship

Cherry Gupta
Greenhorn

Joined: Feb 09, 2010
Posts: 17
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
 
jQuery in Action, 2nd edition
 
subject: hibernate parent/child relationship
 
Similar Threads
Text box values put in the logic:iterate loop is not set in the form.
Creating a java bean in a servlet and accessing it in a jsp
descriptor information is NOT complete
Return same hashCode value for CaseInsensitive strings
Null POinter Exception