I know that similar questions have already been asked for plenty of times but I haven't found one that could help me.
So, could I ask you to help me to find the cause of why a Book
's title
is fetched EAGERly?
I have a very simple codebase, here is my entity:
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class Book {
@Id
@GeneratedValue
private int id;
@Lob
@Basic(fetch = FetchType.LAZY, optional = false)
private String title;
}
And here is a client piece of code, in this case represented by psvm()
:
public static void main(String[] args) {
final ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
final SessionFactory sessionFactory = context.getBean(SessionFactory.class);
Session session = sessionFactory.openSession();
Book book = Book.builder().title("Peace and War").build();
Transaction tx = session.beginTransaction();
session.save(book);
tx.commit();
session.close();
session = sessionFactory.openSession();
book = session.get(Book.class, book.getId());
}
I've also added a plugin to maven in order to enhance bytecode:
<build>
<plugins>
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>5.3.6.Final</version>
<executions>
<execution>
<configuration>
<enableLazyInitialization>true</enableLazyInitialization>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
But still, the title
is fetched eagerly with the following query:
Hibernate: select book0_.id as id1_0_0_, book0_.title as title2_0_0_ from Book book0_ where book0_.id=?
that I can see because of hibernate.show_sql=true
Could you help me to figure out what I am doing wrong? Seems that the answer is on the surface but I can't spot it.