0

Usring SDR. i have an entity, PersonRole, based on a join table. For a report i want the min date between two different columns of the tables joined by the join table. i work on an intranet so her is some pseudo code...

select 
pr, 
if pr.dateOne < pr.dateTwo then pr.dateOne else pr.dateTwo end as minDate
from 
PersonRole pr
...

now, i've used DTOs to get extra data along with my entities when i only care about getting the DTO data and no HATEOAS related data.

basically i want something like this...

personRole : [ {
 person : {
   dateOne: blah
 },
 role : {
   dateTwo: blah
 },
 minDate: this is a min of person.dateOne and role.dateTwo
}]
user2052618
  • 556
  • 2
  • 7
  • 20

2 Answers2

0

You can add a new method to your entity, which will calculate the minDate, e.g.:

@JsonProperty
public Date getMinDate() {
   if (dateOne < dateTwo)
       return dateOne;
   else
       return dateTwo;
}

Spring Data REST will pick up this method and serialize it together with your entity.

Adam Kučera
  • 424
  • 5
  • 15
0

I may be a little late but this sort of functionality can be achieved with projection and @Value annotation. If you define the projection as follows, it would work:

@Projection(name="report",types={PersonRole.class})
public interface PersonRoleProjection{

   @Value("target.dateOne < target.dateTwo ? target.dateOne : target.dateTwo")
   Date getMinDate()

   .... Define other getters here...
}
ArslanAnjum
  • 1,674
  • 2
  • 17
  • 31