2016-12-19 5 views
0

こんにちは、私の@ManyToOne UserEntityはFetchType.Lazyが設定されていますが、熱心にフェッチしています。FetchType.Lazyは設定されていますが、Hibernate/Jpaは@ManyToOneオブジェクトを熱心に取得します

エンティティ:

@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(); 
} 

これは、コール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

選択文では、OWNER列に結合がないため、フィールド所有者は熱心に取得されません。

+0

あなたは正しいです。この質問には申し訳ありません。 UserEntityの負荷は、AuditedEntity CreatedByおよびLastModifiedByアノテーション付きフィールドの拡張です。 – Milso

関連する問題