gpt4 book ai didi

java - Hibernate/Jpa 急切地获取 @ManyToOne 对象,尽管设置了 FetchType.Lazy

转载 作者:行者123 更新时间:2023-12-01 09:06:33 24 4
gpt4 key购买 nike

您好,我的 @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 ?我如何强制加载这个惰性加载或根本不加载?

最佳答案

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

关于java - Hibernate/Jpa 急切地获取 @ManyToOne 对象,尽管设置了 FetchType.Lazy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41223712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com