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