-->
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.  [ 5 posts ] 
Author Message
 Post subject: One-To_many mapping question
PostPosted: Tue Aug 28, 2007 12:28 am 
Newbie

Joined: Sat Aug 25, 2007 5:09 pm
Posts: 3
I am trying to do a simple one-to-many with JPA.

Entity 1 is publisher and Entity 2 is book. One book has only one publisher whereas one publisher has multiple books.

Excerpts from the code:


Code:
@Entity
public class Book implements Serializable{
   
   
   @Id
   private int book_id;
...
@ManyToOne(targetEntity=Publisher.class,optional=false,cascade={CascadeType.PERSIST})

@JoinColumn(name="publisher_id")

   public int getPublisher_id() {
      return publisher_id;
   }



And the publisher class has a collection of books:
Code:
@Entity
public class Publisher implements Serializable{

      @Id
      private int publisher_id;
private ArrayList<Book>publishedBooks;

@OneToMany(cascade={CascadeType.PERSIST},targetEntity=Book.class,mappedBy="publisher_id")
public ArrayList<Book> getPublishedBooks() {
         return publishedBooks;
      }



And in my client, I am doing something like:
Code:
EntityTransaction tx = em.getTransaction();
             tx.begin();
            
            
         
            
            Publisher p=new Publisher();
            p.setPublisher_name("Manning");
            p.setPublisher_id(1);
            
            ArrayList<Book> s=new ArrayList<Book>();
            Book b=new Book("Exorcist");
            b.setBook_id(386);
            s.add(b);
            p.setPublishedBooks(s);   
            
            em.persist(p);
            
            tx.commit();
             em.close();


It seems that Hibernate thinks the collection 'publishedBooks' is also a column, because it throws an exception:

Code:
Caused by: java.sql.BatchUpdateException: Unknown column 'publishedBooks' in 'field list'



What am I doing wrong?
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 12:53 am 
Regular
Regular

Joined: Wed Jun 20, 2007 1:53 am
Posts: 75
paste your mapping documents here..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 1:07 am 
Beginner
Beginner

Joined: Sat Jan 14, 2006 10:05 am
Posts: 22
Location: spb.ru
Code:
@Entity
public class Book implements Serializable{
   
   @Id
   private int book_id;
...
@ManyToOne(targetEntity=Publisher.class,optional=false,cascade={CascadeType.PERSIST})
@JoinColumn(name="publisher_id")
    Publisher publisher;

   public Publisher getPublisher() {
      return publisher;
   }

@Entity
public class Publisher implements Serializable{
      @Id
      private int publisher_id;
private List<Book>  publishedBooks = new ArrayList<Book>();

@OneToMany(cascade={CascadeType.PERSIST},targetEntity=Book.class,mappedBy="publisher")
public List<Book> getPublishedBooks() {
         return publishedBooks;
      }

EntityTransaction tx = em.getTransaction();
             tx.begin();
            Publisher p=new Publisher();
            p.setPublisher_name("Manning");
            p.setPublisher_id(1);
           
            Book b=new Book("Exorcist");
            b.setBook_id(386);

            // bidirectional association
            p.getPublishedBooks().add(b);
            p.setPublisher(p);   
           
            em.persist(p);
           
            tx.commit();
             em.close();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 9:29 am 
Newbie

Joined: Sat Aug 25, 2007 5:09 pm
Posts: 3
expp,
That solved the problem. Many thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 9:29 am 
Newbie

Joined: Sat Aug 25, 2007 5:09 pm
Posts: 3
expp,
That solved the problem. Many thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.