Question

Doing a lookup of a previously saved 1-to-many entity. Entitles are:

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="book", orphanRemoval=true)
    private Collection<Author> authors;

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

        @Override
    public String toString() {
        return "Book [id=" + id + ", authors=" + authors + ", title=" + title
                + ", isbn=" + isbn + "]";
    }
}

Author.java:

@Entity
@Table(name = "Authors")
public class Author {

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

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="book_id")
    private Book book;

        ...

    @Override
    public String toString() {
         return "Author [id=" + id + ", book=" + book + ", firstName="
            + firstName + ", lastName=" + lastName + "]";
    }
}

Ignoring the dubious cardinality of the data model, the lookup code is

long bookID = 10; // saved entity ID
entityManager.getTransaction().begin();
Book savedBook = entityManager.find(Book.class, bookID);
System.out.println("Book " + savedBook);
entityManager.getTransaction().commit();

which gives the output

Book Book [id=10, authors=[], title=Treasure Island, isbn=123456]

Any ideas why authors is empty?


Retrieving 11...

Exception in thread "main" java.lang.StackOverflowError
    at java.lang.Long.toString(Long.java:240)
    at java.lang.Long.toString(Long.java:100)
    at java.lang.String.valueOf(String.java:2946)
    at java.lang.Long.toString(Long.java:733)
    at java.lang.String.valueOf(String.java:2827)
    at java.lang.StringBuilder.append(StringBuilder.java:115)
    at org.hibstore.domain.Book.toString(Book.java:64)
    at java.lang.String.valueOf(String.java:2827)
    at java.lang.StringBuilder.append(StringBuilder.java:115)
    at org.hibstore.domain.Author.toString(Author.java:67)
    at java.lang.String.valueOf(String.java:2827)
    at java.lang.StringBuilder.append(StringBuilder.java:115)
    at java.util.AbstractCollection.toString(AbstractCollection.java:422)
    at org.hibernate.collection.internal.PersistentBag.toString(PersistentBag.java:501)
    at java.lang.String.valueOf(String.java:2827)
    at java.lang.StringBuilder.append(StringBuilder.java:115)
    at org.hibstore.domain.Book.toString(Book.java:64)
    at java.lang.String.valueOf(String.java:2827)
Was it helpful?

Solution

The problem might be while inserting the records. You need to set parent to child and add child to parent like book.addAuthor(author); and author.setBook(book) as the child is driving the relationship here.

OTHER TIPS

Another solution to this problem could have been to set the Entity Author > book field mapping of @ManyToOne to @ManyToOne(fetch=FetchType.LAZY, cascade = CascadeType.PERSIST)

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