Question

I'm currently attempting to map these 2 entities in a bi-directional relationship but getting an exception as shown. The entities

Book.java:

@Entity
@Table(name = "Book")
public class Book {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;


    @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="bookId")
    private Collection<Author> authors;

    public Collection<Author> getAuthors() {
        return authors;
    }

        ...
}

Author.java:

@Entity
public class Author {

    @Id
    @GeneratedValue
    @Column(name="id")
    private Long id;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="author_id")
    private Long bookId;

        ...
}

The exception:

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: BookStore] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:889)
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at org.hibstore.dao.DAOTest.main(DAOTest.java:10)
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on org.hibstore.domain.Author.bookId references an unknown entity: java.lang.Long
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1514)

Any ideas?

Was it helpful?

Solution

Refer to this post for how to use JoinColumn.

To elaborate: your Author entity must point to a Book entity, not to its id. That's the advantage of using JPA: you work with entities, not their keys.

Here's an example:

@Entity
@Table(name = "Book")
public class Book {
  @OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="book")
  private Collection<Author> authors;
  // ...
}

@Entity
public class Author {
  @ManyToOne(fetch=FetchType.LAZY)
  @JoinColumn(name="book_id")
  private Book book;
  // ...
}

Why you would have one book per author (the join column points to the book here) is however questionable, you should probably rethink your data model: a many to many relationship might make more sense but you are going to need a third table for that (there are examples of how to setup your JPA entities accordingly).

OTHER TIPS

I would recommend to go through the following link http://www.objectdb.com/api/java/jpa/annotations/relationship for Simple Relationships.

try to understand the error message the ORM provides..

org.hibstore.domain.Author.bookId references an unknown entity: java.lang.Long

ORM says the Long ( which is Author.bookId) is not an entity. yes because you are trying to associate field not the Object which is annotated as Entity . You have to give "Book" type instead of bookid.

Change to

private Book book;

Note** , I just gave solution for the exception, you have to change your declaration in Book too. but i want you understand concepts and fix yourself rather than pointing answers here..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top