Mcmil
Mcmil

Reputation: 897

Hibernate/Jpa fetch eagerly @ManyToOne object although FetchType.Lazy is setted

Hello my @ManyToOne UserEntity is fetching eagerly although FetchType.Lazy is setted on it.

Entity :

@Entity
@Table(name = "TEST_GROUPS")
@Getter
@NoArgsConstructor
public class TestGroupEntity extends AuditedEntity{

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "owner", nullable = false)
    @JsonIgnore
    protected UserEntity owner;

    @Column(name = "description")
    @Setter
    protected String description;

    @Column(name = "name")
    @Setter
    protected String name;

    @Column(name = "finished")
    @Setter
    protected Boolean finished = false;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    protected Set<TestEntity> tests = Sets.newHashSet();

    @SuppressWarnings("deprecation") // binding this to parent
    public boolean addTest(TestEntity testEntity) {
        testEntity.setTestGroupEntity(this);
        return tests.add(testEntity);
    }

    public boolean removeTest(TestEntity testEntity) {
        return tests.remove(testEntity);
    }

    @SuppressWarnings("deprecation")
    public TestGroupEntity(String name, String description, Set<TestEntity> tests) {
        this.name = name;
        this.description = description;
        this.tests = tests;
        this.tests.stream().forEach(testEntity ->  testEntity.setTestGroupEntity(this));
    }

    @Deprecated
    public void setOwner(UserEntity owner) {
        this.owner = owner;
    }
}

Repository:

@Repository
public interface TestGroupRepository extends PagingAndSortingRepository<TestGroupEntity, Long> {

    Collection<TestGroupPlayerListProjection> findByFinishedFalse();
}

Projection/Dto:

public interface TestGroupPlayerListProjection {
    Long getId();
    String getName();
    String getDescription();

    @Value("#{target.tests.size()}")
    Integer getTestsNumber();
}

This is the select statement generated by Hibernate after call repostiory findByFinishedFalse method :

select samples0_.test_entity_id as test_ent1_4_0_, samples0_.samples_id as samples_2_4_0_, testsample1_.id as id1_15_1_, testsample1_.uuid as uuid2_15_1_, testsample1_.created_by_id as created_8_15_1_, testsample1_.created_date as created_3_15_1_, testsample1_.updated_by_id as updated_9_15_1_, testsample1_.updated_date as updated_4_15_1_, testsample1_.version as version5_15_1_, testsample1_.file_name as file_nam6_15_1_, testsample1_.key as key7_15_1_, testsample1_.resource_id as resourc10_15_1_, userentity2_.id as id1_17_2_, userentity2_.uuid as uuid2_17_2_, userentity2_.created_by_id as created_9_17_2_, userentity2_.created_date as created_3_17_2_, userentity2_.updated_by_id as updated10_17_2_, userentity2_.updated_date as updated_4_17_2_, userentity2_.version as version5_17_2_, userentity2_.enabled as enabled6_17_2_, userentity2_.password as password7_17_2_, userentity2_.username as username8_17_2_, userentity3_.id as id1_17_3_, userentity3_.uuid as uuid2_17_3_, userentity3_.created_by_id as created_9_17_3_, userentity3_.created_date as created_3_17_3_, userentity3_.updated_by_id as updated10_17_3_, userentity3_.updated_date as updated_4_17_3_, userentity3_.version as version5_17_3_, userentity3_.enabled as enabled6_17_3_, userentity3_.password as password7_17_3_, userentity3_.username as username8_17_3_, userentity4_.id as id1_17_4_, userentity4_.uuid as uuid2_17_4_, userentity4_.created_by_id as created_9_17_4_, userentity4_.created_date as created_3_17_4_, userentity4_.updated_by_id as updated10_17_4_, userentity4_.updated_date as updated_4_17_4_, userentity4_.version as version5_17_4_, userentity4_.enabled as enabled6_17_4_, userentity4_.password as password7_17_4_, userentity4_.username as username8_17_4_, userentity5_.id as id1_17_5_, userentity5_.uuid as uuid2_17_5_, userentity5_.created_by_id as created_9_17_5_, userentity5_.created_date as created_3_17_5_, userentity5_.updated_by_id as updated10_17_5_, userentity5_.updated_date as updated_4_17_5_, userentity5_.version as version5_17_5_, userentity5_.enabled as enabled6_17_5_, userentity5_.password as password7_17_5_, userentity5_.username as username8_17_5_ from audio_tests_samples samples0_ inner join test_samples testsample1_ on samples0_.samples_id=testsample1_.id left outer join users userentity2_ on testsample1_.created_by_id=userentity2_.id left outer join users userentity3_ on userentity2_.created_by_id=userentity3_.id left outer join users userentity4_ on userentity3_.updated_by_id=userentity4_.id left outer join users userentity5_ on testsample1_.updated_by_id=userentity5_.id where samples0_.test_entity_id=?

Why there is eagerly load of UserEntity ? How I can enforce to load this lazy or none at all?

Upvotes: 1

Views: 350

Answers (1)

Peter Š&#225;ly
Peter Š&#225;ly

Reputation: 2933

In select statement there is no join on OWNER column, field owner is not eagerly fetched.

Upvotes: 1

Related Questions