Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unfetched attribute error when loading an instance name in case of entities hierarchy #2860

Open
gorbunkov opened this issue Feb 7, 2024 · 1 comment
Labels
candidate Possible candidate for future releases in: core in: flowui size: M type: bug Something isn't working

Comments

@gorbunkov
Copy link
Contributor

Environment

Jmix version: 2.1.3

Bug Description

Let's say that we have a Pet entity:

@JmixEntity
@Table(name = "PET")
@Entity
public class Pet {
    @JmixGeneratedValue
    @Column(name = "ID", nullable = false)
    @Id
    private UUID id;

    @Column(name = "NAME")
    private String name;

    @Column(name = "SECOND_NAME")
    private String secondName;

// getters and setters
 
    @InstanceName
    @DependsOnProperties({"name"})
    public String getInstanceName(MetadataTools metadataTools) {
        return metadataTools.format(name);
    }
}

It has a descendant class:

@JmixEntity
@Entity
public class Dog extends Pet {
    @Column(name = "DOG_NAME")
    private String dogName;

// getters and setters

    @InstanceName
    @DependsOnProperties({"dogName"})
    public String getInstanceName(MetadataTools metadataTools) {
        return metadataTools.format(dogName);
    }
}

Dog's instance name contains attribute that is not present in Pet.

The Appointment entity has a reference to Pet:

@JmixEntity
@Table(name = "APPOINTMENT", indexes = {
        @Index(name = "IDX_APPOINTMENT_PET", columnList = "PET_ID")
})
@Entity
public class Appointment {
    @JmixGeneratedValue
    @Column(name = "ID", nullable = false)
    @Id
    private UUID id;

    @Column(name = "DATE_")
    private LocalDate date;

    @JoinColumn(name = "PET_ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Pet pet;

// getters and setters
}

The problem is that when we load a list of appointments in the appointments list view the error appears:

IllegalStateException: Cannot get unfetched attribute [dogName] from detached object com.company.instancenameissue.entity.Dog-31fd51d1-b51f-1c7c-459e-a4fb4275a224 [detached].

Fetch plan for Appointment contains a Pet attribute (it doesn't contain a dogName flield that is used by dog instance name). But the screen tries to evaluate an actual instance name (for Dog) instead of evaluating instance name for Pet.

The solution may be to introduce a new method in the InstanceNameProvider that in addition to entity instance will accept the class that should be used for instance name evaluation (we would pass Pet.class there). This new method should be somehow used by Jmix dataGrids and other components.

Sample Project

instanceNameIssue.zip

@gorbunkov gorbunkov added type: bug Something isn't working in: core triage Issue is waiting for triage size: M in: flowui and removed triage Issue is waiting for triage labels Feb 7, 2024
@gorbunkov gorbunkov added the candidate Possible candidate for future releases label Mar 1, 2024
@dtaimanov
Copy link
Contributor

dtaimanov commented Mar 12, 2024

The same issue is also caused these problems: https://forum.jmix.io/t/missing-childfield-on-association/3043

Subclass instances loaded by reference with superclass type have unfetched local fields when BASE fetch plan used. Unfortunately the only workaround is to reload entities by ids because properties defined in subclasses neither loaded automatically, nor can be specified in fetch plan.

Suggested solution:
Rework fetch plan processing in order to allow fields from subclasses and/or translate standard fetch plans correctly covering all possible fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
candidate Possible candidate for future releases in: core in: flowui size: M type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants