0

I'm running my app in SpringBootTest. Using IN memory db.

I'm getting error in my spring app when i try to get record from db.

I'm using:


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

In this app/test I'm also doing save to db and it works fine.

Db table is build of two simple columns DUMMY_ID and REQUEST.

Error: Method threw 'org.hibernate.LazyInitializationException' exception. Cannot evaluate ....... $HibernateProxy$JjtlVUy6.toString()

Problematic method -> requestsRepository.getById("dummyId");
@Repository
@Transactional
public interface RequestsRepository extends JpaRepository<RequestsEntity, String> {
}

@Data
@Entity
@Component
@Table(name = "REQUESTS", schema = "", catalog = "")
public class RequestsEntity {
    @Id
    @Column(name = "DUMMY_ID")
    private String dummyId;
    @Column(name = "REQUEST")
    private String request;

    public RequestsEntity() {
    }

    public RequestsEntity(String dummyId, String request) {
        this.dummyId = dummyId;
        this.request = request;
    }
}

I don't know why getById is failing.

9
  • A relevant topic to your problem is described here, i mean answer of this question. Commented Sep 13, 2023 at 15:03
  • I think its not exactly same problem. In my case im saving object to db and then I'm trying to read it. In topic above its working. In My problem not and Im using jpa repository, so without dao. Commented Sep 13, 2023 at 15:44
  • You are using getById and you know that there are the problem , i recommend you to check this resource jpa-getReference-and-findById is really a good article. Because this kind of problem is a very popular one when peoples are working with hibernate Commented Sep 13, 2023 at 15:49
  • And also a good article for you handle-thelazyinitializationexception of course after above recommended article is is going to be it. Commented Sep 13, 2023 at 16:29
  • Still i don't see explanation for my problem. In my case findById or getById doesn't work. Should I create custom Repository implementation and there create method for finding record ? Currently I used auto implementation (SimpleJpaRepository) of jpa repository. Maybe there is a problem with EntityManager ? Commented Sep 13, 2023 at 16:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.