Peter Illes
Peter Illes

Reputation: 186

How to create an entity view with list of related entities in JHipster 3.x?

New to JHipster, so bear with me :). I'd like to display lists of related entities on an entity detail view. How can I do that? Since this is common in this application, it would be best to have a generic way of doing this instead of modifying each entity (e.g. by modifying the generator), but per entity editing is OK if there is no other way.

An example: I have Courses and each Course may have many Lessons, Students and Clients connected. Now on the Course detail view below the Course fields I'd like to have three lists for each of the related Lessons, Students and Clients (e.g. similar to what SalesForce does).

The question has two sides: how can I get the proper data (serialization), and how can I change the view? The latter (change view) seems to be simple if I do it by hand for each entity, but would be nice to have it generated.

This seems to be a common UX theme. Is there a sample application somewhere covering this use case?

Thanks for any pointers!

Upvotes: 4

Views: 2054

Answers (1)

David Steiman
David Steiman

Reputation: 3145

In short:

1) you make your CourseResource delievering eager fetched entities of Course, which have no @JsonIgnore over your Set of lessons. In the end, by calling one course, the JSON should contain the related entities.

in detail (edit):

consider, you generated your app with a JDL like this:

entity Course {
    courseName String
}

entity Lesson {
    lessonName String
}

relationship OneToMany {
    Course{lesson} to Lesson
}

Then first go to Course class and change the Set to the following:

@OneToMany(mappedBy = "course", fetch = FetchType.EAGER)
//@JsonIgnore - remove this
private Set<Lesson> lessons = new HashSet<>();

This is the quickiest way, but not the most perfomant, since everytime you query courses, you join on lessions, too. To avoid that, you must build some @Query into your repository where you manually join to that...but I will keep it simple for now.

The removal of @JsonIgnore will lead to the lessons implicitly being part of your Course representation, so you will got all options to visualize this in the UI

2) find some way to display it, in a way you want

generally I feel it seems to be a good idea of having some generator, but it might be harder than you think. Like, how to represent the data. If you got multiple relationships to that entity, will you use tabs? If tabs, but there is only one relation ship, does the tab still is adequate? Is that more on this, still adequate to decide this in JHipsters central generator, or this should be open for the developer?

I think, it worths a discussion, but nevertheless there could be done some JHipster module to accomplish this

Best Regards

P.S.: if my answer was to short, ask me on...it's just late and I am lazy to paste down a full blown answer :)

Upvotes: 3

Related Questions