I know this question was asked several times but I couldn't find a clear example and answer about this topic(I also tried other possible solutions).
I am using Spring JPA
and Hibernate
and trying to make a lazy fetch for a OneToONe relation. I have 2 simple Entity classes, one Repository class and using h2 database to lazy load an entity. I tried build time bytecode instrumentation to achive this and these are my classes.
Class A
@Entity
public class A {
private String name;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(optional = false, fetch = FetchType.LAZY, mappedBy = "a")
@LazyToOne(LazyToOneOption.NO_PROXY)
private B b;
}
Class B
@Entity
public class B {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "A_ID")
private A a;
}
Repository
public interface ARepository extends JpaRepository<A, Long> {
A findByName(String name);
}
pom.xml bytecode enhancer
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<version>${hibernate.version}</version>
<executions>
<execution>
<configuration>
<failOnError>true</failOnError>
<enableLazyInitialization>true</enableLazyInitialization>
</configuration>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
and lastly initdata.sql for h2 database
insert into a (name, id) values ('a', 1);
insert into b (a_id, id) values (1, 1);
When I call findByName()
method in a test class it still executes 2 queries for A
and B
. How can I lazy fetch B
class ? Thanks in advance.