Displaying attributes of another entity inside an editor

Hi community,

for example I have Entity Student and Teacher.

Student have a manyToOne reference to Teacher.

When I create a student, I shall also “create” the reference to the teacher.
Means,
when I open the editor screen of the student, I shall have also the “attributes” of the teacher, that I have to fill inside the editor screen.

For example:
Student-Editor:

student name:
age:
Teacher name: //this is a attribute from Teacher Entity
Teacher subjects: //this is a attribute from Teacher Entity

Is it possible to display the informations like this?

Means, by creating an student, it will automatically create also the teacher…

Thanks

1 Like

Hi,

Yes, it’s possible. First of all, you need to define a nested datasource for the teacher property and then setup FieldGroup’s fields to refer to a teacher attributes:

<dsContext>
    <datasource id="studentDs"
                class="com.company.demo.entity.Student"
                view="student-view">
        <datasource id="teacherDs" property="teacher"/>
    </datasource>
</dsContext>
<dialogMode height="600"
            width="800"/>
<layout expand="windowActions"
        spacing="true">
    <fieldGroup id="fieldGroup"
                datasource="studentDs">
        <column width="250px">
            <field property="name"/>
            <field property="teacher.name"/>
            <field property="teacher.subjects"
                   rows="5"/>
        </column>
    </fieldGroup>
    <frame id="windowActions"
           screen="editWindowActions"/>
</layout>

In the screen controller override the initNewItem method as follows:

public class StudentEdit extends AbstractEditor<Student> {
    @Inject
    private Metadata metadata;

    @Override
    protected void initNewItem(Student item) {
        // Create a new teacher every time we create a student
        Teacher teacher = metadata.create(Teacher.class);
        item.setTeacher(teacher);
    }
}

Regards,
Gleb

Thanks a lot!

I will try it :slight_smile:

What does I have to specify here?

view="student-view">

Only the property for teacher? With view _local?

Thanks

Where do I use this datasource?

teacherDs

Why I have to make this nested datasource?

Thanks

I get this exception:

java.lang.ClassCastException: com.company.demo.entity.Teacher cannot be cast to java.util.Collection

:frowning:

Yes

No need to use, it’s needed for tracking changes in a teacher entity in order to commit it and show notification before the editor is closed in case of unsaved changes.

If something doesn’t work, please be more specific about the proper case to reproduce.

I use this as datasource for the fieldgroup

teacherDs

and it works…
If I use the other datasource and try to open the editor-screen of student, I just get the error, what I show you before…

Can it make sense?

I also do not need to make a “.” for the teacher like teacher.subject,
I can only write subject in the fieldgroup.

Could you please attache a demo project or at least screen’s descriptor and controller?

Hi Gorelov,

it works until now…

I will show it later, when it will break in one point :stuck_out_tongue:

The code is to much to show, thats the problem…