0

I have a database with a table called Car. The car table looks like this:

+----+------+--------------+-----------+----------+------+
| Id | Name | Desccription |   Make    |  Model   | Year |
+----+------+--------------+-----------+----------+------+
|  1 | A    | something1   | Ford      | Explorer | 2010 |
|  2 | B    | something2   | Nissan    | Ultima   | 2005 |
|  3 | C    | something3   | Chevrolet | Malibu   | 2012 |
+----+------+--------------+-----------+----------+------+

Different pages on my website want to display different information. Some pages only want to display the name, others wants to display the make and model, etc.

I have an api that the web calls to retrieve all this information. The api uses JPA and QueryDSL to communicate with the database and fetch information. I want to only fetch the information that I want for that particular page. I'm thinking about implementing some sort of builder patter to my repo to allow for me to only retrieve what I want but I'm not quite sure how to go about it.

For example, my home page only wants to display the Name of the car. So it'll call the HomeController and the controller will call the HomeService which will call the repository layer something like this:

carRepository.getCarById(1).withName().build();

Some other page that wants to display the make and model would make a repo call like this:

carRepository.getCarById(1).withMake().withModel.build();

What is the best way to implement something like this in Java/Jpa?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Richard
  • 5,840
  • 36
  • 123
  • 208
  • I'd recommend starting with some basic tutorials of JPA and build that into your car API as you come to understand JPA more. As it it's written, this question is far too broad for SO. – Andy Guibert Jun 24 '17 at 00:33
  • @AndyGuibert we actually already have a jpa player to fetch data but the problem is we're always retrieving too much for all the pages. When we have a getById method it retrieves the entire car entity which is too much information for some pages. So then we started writing methods for specific pages like getCarByIdForHomePage or getCarByIdForAdminPage etc. But this quickly gets out of hand so I'm wondering if there's a better approach – Richard Jun 24 '17 at 15:16

1 Answers1

1

If I understand the question correctly, you want queries for different projections of your entities to be built dynamically.

In that case, dynamic entity graphs are what you want (see e.g. here: https://www.thoughts-on-java.org/jpa-21-entity-graph-part-2-define/). You start with an empty entity graph, and each call to one of your with() method simply adds a field to the graph.

The base query remains unchanged, you just need to set the fetch graph hint (javax.persistence.fetchgraph) upon calling build() (note that the samples in the above link use load graphs instead of fetch graphs; the subtle difference between the two is described here: What is the diffenece between FETCH and LOAD for Entity graph of JPA?)

crizzis
  • 9,978
  • 2
  • 28
  • 47