2

I have a class Project, a superclass Resource with two subclasses StorageResource and ComputeResource. I have defined a Hibernate mapping (I am using Hibernate version 5.3.7.Final) as follows:

    @Entity
    @Table(name = "PROJECTS", ...})
    public class Project implements Serializable {
        ...
        @OneToMany(
            mappedBy = "project",
            cascade = CascadeType.ALL,
            orphanRemoval = true
        )
        private List<Resource> resources;
        ...
    }


    @Entity
    @Table(name = "resources")
    @Inheritance(strategy = InheritanceType.JOINED)
    public class Resource implements Serializable {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id")
        private long id;

        @ManyToOne
        private Project project;
        ...
    }

    @Entity
    @Table(name = "storage_resources")
    @PrimaryKeyJoinColumn(name = "id")
    public class StorageResource extends Resource implements Serializable {
        ...
    }

    @Entity
    @Table(name = "compute_resources")
    @PrimaryKeyJoinColumn(name = "id")
    public class ComputeResource extends Resource implements Serializable {
        ...
    }

My Java code can save instances of both subclasses (StorageResource and ComputeResource) correctly to the database. However, when I load these records by loading a project and its resources, I have the following:

  • Storage resource records are loaded as Java objects that are instances of StorageResource,
  • Compute resource records are loaded as Java objects that are instances of Resource, not of ComputeResource as I would expect.

I have spent the last two days trying to figure out why this is happening: the mappings of both sub-classes are the same. Am I missing something?

EDIT

I found the problem. It was quite misleading, because there was no error message. The environment in which the bug occurred had an outdated configuration file. The Hibernate mapping for ComputeResource

<mapping class="....ComputeResource"/>

was missing. After adding this line the mapping is working as expected.

Giorgio
  • 5,023
  • 6
  • 41
  • 71
  • Perhaps the objects in the list are not fully loaded and you are dealing with lazy-load proxies? Otherwise I would suggest showing more code and/or adding additional logging to find the difference. – ewramner Nov 14 '22 at 13:48
  • @ewramner: Thanks for the feedback. I have tried running `Hibernate.unproxy()` on the objects but it has no effect: The class of the resulting object is still the superclass. I will review my code once more and then provide more information. – Giorgio Nov 14 '22 at 14:02
  • Can you add some more details, perhaps on the exact implementation of the entities and also on how exactly you are using them? Can you check the imports, is everything JPA? Can you enable query logging to see what is the SQL that Hibernate runs? Also double-check your data! I have a minimal JPA project for experimenting and could not reproduce this behavior. – Nikos Paraskevopoulos Nov 14 '22 at 14:23

0 Answers0