0

I have two entities:

Book:

@Entity
public class Book {
    @Id
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "library_id")
    private Library library;
    // Getters and setters
}

and Library:

@Entity
public class Library {
    @Id
    private Long id;

    @OneToMany(mappedBy = "library", fetch = FetchType.EAGER)
    private List<Book> books;
    // Getters and setters
}

What I want to do is eagerly fetch all the books for a given queried library:

Library library = em.find(Library.class, 1L);
System.out.println(library.getBooks());

But it gives me null. How do I get list of all the linked books? I have searched and tried many solutions from S.O. but none works.

P.S. - I can assure you that there is linked data present in the tables.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
  • and why not look at the LOG then? since you want to DEBUG your usage –  Mar 25 '17 at 18:47
  • who said look for an error? A JPA provider issues SQL. Perhaps read it –  Mar 25 '17 at 18:50
  • @BillyFrost - Last SQL I can see in the logs is an INSERT that happened on persisting a book object. There is no further SQL issued. – Gurwinder Singh Mar 25 '17 at 18:54
  • If you do a find and then access the collection field then either the object will come from the L1/L2 cache or there will be SQL. The log should tell you. –  Mar 25 '17 at 18:55

2 Answers2

0

The issue is with @JoinColumn(name = "library_id") because you have don't have library_id column, rather change it to @JoinColumn(name = "id") or else the other option is to change the field id to library_id inside Library class.

It seems like you didn't map the Entity classes with table column names (because they are with different names), you need to map them using @Column that as shown below:

    @Entity
    public class Book {
        @Id
        @Column(name="book_id")
        private Long id;

        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "library_id")
        private Library library;
        // Getters and setters
    }



    @Entity
    public class Library {
       @Id
       @Column(name="library_id")
        private Long id;

        @OneToMany(mappedBy = "library", fetch = FetchType.EAGER)
        private List<Book> books;
        // Getters and setters
   }
Vasu
  • 21,832
  • 11
  • 51
  • 67
0

It turns out the object was being queried from cache rather than database and it wasn't an updated copy. As I tried cleaning up the cache before querying, it worked:

em.clear();

I figured instead of clearing the whole context, I'll just detach one object and query it again.

em.getTransaction().begin();

Library library = new Library(1L, "Central library");
em.persist(library);

em.persist(new Book(1L, "Harry potter", "123-123-123-123", 1000, library));

em.getTransaction().commit();
em.detach(library);   // This did the trick

library = em.find(Library.class, 1L);
System.out.println(library.getBooks());

For some reason I wanted it worked without this hack. I am still open to better solutions.

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76