ArgumentException: Encounter "" at character xx, but expected: []

168 Views Asked by At

I have issue with simple SELECT query with join clause.

I am trying to get Author with list of its Books. My query looks like:

public Author findAuthorWithBooks(Integer authorId) {
    return (Author) em.createQuery("SELECT a FROM Author a LEFT JOIN a.books WHERE a.id = :authorId")
        .setParameter("authorId", authorId)
        .getSingleResult();
}

But when code is running I am getting ArgumentException: Encounter "" at character xx, but expected: []

Simplified entities looks like that:

public class Author {
    @OneToMay(mappedBy = "author", fetch=LAZY)
    private Collections<Book> books;
}

public class Book {
    @ManyToOne(fetch=LAZY)
    @JoinColumn(name="AUTHOR_ID")
    private Collections<Book> books;
}

I suppose I did some pretty stupid mistake, but I cant find it.

1

There are 1 best solutions below

0
João Dias On

Either this is a typo in your question or your Book class is wrong. It should be as follows:

public class Book {
    @ManyToOne(fetch=LAZY)
    @JoinColumn(name="AUTHOR_ID")
    private Author author;
}

Additionally, your query does not seem right. Try the following:

public Author findAuthorWithBooks(Integer authorId) {
    return (Author) em.createQuery("SELECT a FROM Author a LEFT JOIN a.books b WHERE b.id = :authorId")
        .setParameter("authorId", authorId)
        .getSingleResult();
}