I have a model class like the following:
package com.example.model; import java.util.Map; import javax.persistence.Column; import javax.persistence.Convert; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import com.example.helpers.StringMapConverter; @Entity @Table(name = "buildingcompanies") public class Buildcompanies { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "shortname") private String shortname; @Column(name = "fullname") private String fullname; @Column(name = "address") private String address; @Column(name = "telephone") private String telephone; @Column(name = "website") private String website; @Column(name = "sociallinks") @Convert(converter = StringMapConverter.class) private Map<String, String> sociallinks; @Column(name = "foundationyear") private String foundationyear; public Buildcompanies() { } public Buildcompanies(String shortname, String fullname, String address, String telephone, String website, Map<String, String> map, String foundationyear) { this.shortname = shortname; this.fullname = fullname; this.address = address; this.telephone = telephone; this.website = website; this.sociallinks = map; this.foundationyear = foundationyear; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getShortname() { return shortname; } public void setShortname(String shortname) { this.shortname = shortname; } public String getFullname() { return fullname; } public void setFullname(String fullname) { this.fullname = fullname; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getWebsite() { return website; } public void setWebsite(String website) { this.website = website; } public Map<String, String> getSociallinks() { return sociallinks; } public void setSociallinks(Map<String, String> sociallinks) { this.sociallinks = sociallinks; } public String getFoundationyear() { return foundationyear; } public void setFoundationyear(String foundationyear) { this.foundationyear = foundationyear; } }
And the method in a controller to show the output:
public ResponseEntity<List<Buildcompanies>> getAllCompanies(@RequestParam(required = false) String name) { try { List<Buildcompanies> companies = new ArrayList<Buildcompanies>(); int test=0; if (name == null) { buildcompaniesRepository.findAll().forEach(companies::add); } else buildcompaniesRepository.findByShortnameContaining(name).forEach(companies::add); if (companies.isEmpty()) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } return new ResponseEntity<>(companies, HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } }
It does show an output everythin is just fine:
[ { "id": 81, "shortname": "testing", "fullname": "test", "address": "addrtest", "telephone": "380979379992", "website": "www.site.com", "sociallinks": { "facebook": "fb.com" }, "foundationyear": "1991" } ]
And I want to calculate each companies rating while showing data to the end user. So the output should be as follows:
[ { "id": 81, "shortname": "testing", "fullname": "test", "address": "addrtest", "telephone": "380979379992", "website": "www.site.com", "sociallinks": { "facebook": "fb.com" }, "foundationyear": "1991", "rating": "1.5" } ]
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:
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.
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.