2

We have this school project where we are creating a book library system where the main purpose is to use a database connection. In this case JPA and ObjectDB.

Our problem is that we have 3 tables. One that describes a book (title, author and published year), the second a borrower (first name, last name, address, email and phonenumber) and the last table the lending-function that gets the primary ID-key from the other two tables.

The problem is that when we are using the list borrowed book-function it prints out only the IDs from the two other tables. We think the problem is the SQL-query and we haven't gotten the chance to learn SQL properly.

So our question is, how do we create a SQL query that gets the title from the book-table and the first and last name from the borrower table and not the IDs.

The SQL-query where we have only managed to work is this:

TypedQuery<BokLån> q = em.createQuery("SELECT bookborrow FROM BookBorrow BookBorrow", BookBorrow.class);
ObjectDB
  • 1,312
  • 8
  • 9

2 Answers2

0

IT is problem of fetch and fetch type mechanism. currently your entities are loaded lazy, you can load these eagerly in entity class

zaffargachal
  • 802
  • 7
  • 21
0

You need to write a proper JOIN statement to combine the lending-table to the other tables, and retrieve the info from there.

Something along the lines of:

SELECT 
    book.title, 
    person.firstname, 
    person.lastname 
FROM
   borrower
     JOIN book ON book.bookid = borrower.bookID
     JOIN person ON person.personid = borrower.personid

The way it works is the ON condition specifies how to tie the tables together. In your case, the borrower table has a personID which relates to the PersonID column in the person table. Establishing the link with the JOIN, you can then access the other columns in the Person table.

The exact same method works for the book table.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92