虽然设置了FetchType.Lazy,但Hibernate / Jpa急切地获取@ManyToOne对象

时间:2016-12-19 13:29:58

标签: java hibernate jpa

你好我的@ManyToOne UserEntity正在热切地提取,虽然它上面设置了FetchType.Lazy。

实体:

@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
public interface TestGroupRepository extends PagingAndSortingRepository<TestGroupEntity, Long> {

    Collection<TestGroupPlayerListProjection> findByFinishedFalse();
}

投影/ DTO:

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

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

这是Hibernate在调用repostiory findByFinishedFalse方法后生成的select语句:

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=?

为什么会急切加载UserEntity? 我如何强制加载这个懒惰或根本没有?

1 个答案:

答案 0 :(得分:1)

在select语句中,OWNER列上没有连接,不会急切地获取字段所有者。