0

Hi I have a relation like this in BookEO and BookAreaEO classes respectively. Now, I wanted to understand what is the purpose of mappedBy and how I can write a query like below in BookEO using JPA

select * from book b where exists (
    select book_id from book_report_area ba
    where b.book_id = ba.book_id and ba.subject_area_id=200);
// BookEO.java
Set<BookArea> bookAreas;
@Override
@PrivateOwned
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "book", targetEntity = BookAreaEO.class, orphanRemoval = true)
public Set<BookArea> getBookAreas() {
    return bookAreas;
}

// BookAreaEO.java
Book book;
@Override
@ManyToOne(fetch = FetchType.LAZY, targetEntity = BookEO.class, optional = false)
@JoinColumn(name = "BOOK_ID", nullable = false)
public Book getBook() {
    return book;
}
Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
user3310115
  • 1,372
  • 2
  • 18
  • 48
  • What is the database table name of `BookAreaEO` entity? Is it `book_report_area`? – Abdullah Khan Jun 06 '17 at 08:16
  • yes its book_report_area – user3310115 Jun 06 '17 at 08:32
  • 1
    There are ample docs for how to write JPQL on the internet. JPQL comes with subqueries, so I see nothing obviously hard in that. Post what you have tried – Neil Stockton Jun 06 '17 at 08:32
  • Is a subquery necessary here? Simple JPQL join would do i believe! – Abdullah Khan Jun 06 '17 at 08:39
  • likely it isnt necessary, but until the poster at least tries something it is a pointless exercise – Neil Stockton Jun 06 '17 at 08:45
  • I know how to write basic jpql queries but in this case I'm stuck because BookEO doesn't contain BookArea. So that is y, I was not able to start in JPQL. However, I was able to write a SQL query for this select * from book b where b.book_id in (select ba.book_id from book_area ba where ba.book_area_id=900); – user3310115 Jun 06 '17 at 12:07
  • If I had the BookArea object in Book then I would have done something like select * from book b where b.book_id in (select ba.book_id from book_area ba where ba.book_area_id=:bookAreaId) – user3310115 Jun 06 '17 at 12:10

0 Answers0