0

I have three entities:

@Entity
class Group {
    @id
    Long id;
    String name;
    String faculty;
@OneToMany(mappedBy = "group", fetch = FetchType.LAZY, cascade = CascadeType.ALL )
    List<Schedule> scedule;
    ...
}

@Entity
class Schedule {
    @id
    Long id;
    String name;
    Group group;
    @OneToMany(mappedBy = "scedule", fetch = FetchType.LAZY, cascade = CascadeType.ALL )
    List<Lesson> scedule;
    ...
}

@Entity
class Lesson {
    @id
    Long id;
    String name;
    Schedule scedule;
    ... 
}

I use Spring boot, and one my restcontroller return list of Lesson. But i need something like the following JSON for Lesson:

{
id: 1,
name: name,

But instead of schedule i need information about group:

group.name: groupName }

  • Just create a View Model class that has the fields id, name and groupName. In the RestController you have to map the Lesson entity to the View Model class and return and object of the View Model type. – Evgeni Dimitrov Mar 22 '16 at 13:16
  • @Evgeni Create a wrapper and return it? – Анатолий Сивенко Mar 22 '16 at 15:32
  • Yes, it's something like a wrapper. Take a look at the View Model pattern - https://blogs.msdn.microsoft.com/dphill/2009/01/31/the-viewmodel-pattern/. The JPA entities are your model and the view models are what you display on the web page. – Evgeni Dimitrov Mar 22 '16 at 15:35
  • @Evgeni I understood, thanks. I thought there are more beautiful solution for serialize object in two way. – Анатолий Сивенко Mar 22 '16 at 15:44
  • There is Json View, which is a Jackson mapper feature. See here: https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring#json-views But I'm not sure that it covers your use case. – Evgeni Dimitrov Mar 22 '16 at 19:29

0 Answers0