Skip to content
Advertisement

add a dynamic column to an entity without saving it to the table

I have a model class like the following:

JavaScript

And the method in a controller to show the output:

JavaScript

It does show an output everythin is just fine:

JavaScript

And I want to calculate each companies rating while showing data to the end user. So the output should be as follows:

JavaScript

Is it posible to add the rating column dynamicly to the company list or I should to create rating column in database, update method for it in the controller, iterate over the findAll() results and call it each time user tryes to acces /list endpoint?

Advertisement

Answer

You have two options:

  1. You may introduce a new attribute in the Buildcompanies class for the purpose and annotate it with @Transient. This will denote that the attribute need not be persisted in the DB and JPA won’t attempt to create a column in the table.

  2. The recommended approach is to not use the Entity class as a response object. You should ideally have a domain object and the database response should be mapped to this object. While mapping you can apply whatever custom details you want to add to it.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement