Vikum Dheemantha
Vikum Dheemantha

Reputation: 843

Spring Hibernate: Unable to find entity with Id even data available in the database (Only in one to many relationship)

In the spring boot project I have 2 entities named Assessment and Request andtheir relationship is defined as given below.

    @Entity
    public class Assessment {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id", nullable = false)
        private Long id;
    
        @ManyToOne
        @JoinColumn(name = "arequest_id")
        @JsonIgnore
        private Request request;

        // getters and setters and rest of the properties
    }

and the Request class defined as given below (Please note that this relationship is only defined in Assessment)

@Entity
public class Request {
    @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id", nullable = false)
        private Long id;
       
        // Getters and setters along with the rest of the properties
        // No relationship defined here for the Assessment class

}

Then for my requirement, I need to get all the assessments by Request.id for that I create the following method as a derived query.

public List<CandidateAssessment> findAllByRequestId(long requested);

But when I run the program it gives the following error message for the Request IDs that have data in the request table and assessment data in the database. The interesting part is for the requests that do not have data in assessment it shows an empty array but for the requests that contain assessments, it always gives the following error.

javax.persistence.EntityNotFoundException: Unable to find com.example.entity.Request with id 127
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:183) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1106) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1025) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:716) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.type.EntityType.resolve(EntityType.java:502) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:170) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:144) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:1115) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.processResultSet(Loader.java:973) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:921) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2554) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2540) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]

I know I can define it as a bidirectional and call for request first and then call the getAssessments method to fetch the data but this cannot be work like that.

And there are several other entities that implemented as same but working without issue.

How can I fix this issue?

Note

I have tried following the methods

  1. add bidirectional definition to entity management
  2. tried different method derived queries like findAllByRequest_Id
  3. Try to write manual queries with @Query annotation with JPA queries and native queries.
  4. tried some answers given in following questions, but so far no idea to resolve this issue.

Upvotes: 0

Views: 237

Answers (0)

Related Questions