Bruno Oliveira
unread,Jul 3, 2021, 12:46:47 AM7/3/21Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to H2 Database
Hey Folks! How are you all doing?
I found a problem and a temporary solution in my project. But now, I need to definetly solve the problem. I am noob here. But I intend to contribute a lot in this group.
Let's go to my question: I have a Spring Boot project that contains two classes with a ManyToMany relationship. The entities are Book and Author.
I created three profiles in it: test, dev and prod. All configurations for H2 Database are in the application-test.properties file:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=create
And, in the application.properties file, I did this way:
spring.profiles.active=${APP_PROFILE:test}
spring.jpa.open-in-view=false
It worked for several endpoints but when I do GET to retrieve all books by the author's name, I get a 500 status code, and in my IDE (IntelliJ) I have this:
Hibernate:
select
distinct book0_.id as id1_1_,
book0_.book_genre as book_gen2_1_,
book0_.isbn as isbn3_1_,
book0_.language as language4_1_,
book0_.print_length as print_le5_1_,
book0_.publication_year as publicat6_1_,
book0_.publisher as publishe7_1_,
book0_.synopsis as synopsis8_1_,
book0_.title as title9_1_,
book0_.url_cover as url_cov10_1_
from
tb_book book0_
inner join
tb_book_author authors1_
on book0_.id=authors1_.book_id
inner join
tb_author author2_
on authors1_.author_id=author2_.id
where
author2_.name like ? limit ?
2021-07-02 17:54:07.806 WARN 16158 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.bruno.project.entities.Book.authors, could not initialize proxy - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.bruno.project.entities.Book.authors, could not initialize proxy - no Session (through reference chain: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->com.bruno.project.entities.Book["authors"])]
The temporary solution I found was changing the content of the application.properties file, this way:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=create
That way, all endpoints run perfectly. But I will deploy my code into Heroku platform and it would be very good if I could user different profiles.
Have you ever experienced this situation? May you help me?
Thanks in advance!
Bruno