gpt4 book ai didi

hibernate - 如何在 Query DSL 4 上使用 JPA 2.1 中的 loadgraph 和 fetchgraph

转载 作者:行者123 更新时间:2023-12-02 18:31:40 25 4
gpt4 key购买 nike

环境:

org.hibernate:hibernate-core:5.2.14.Final
org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final
org.hibernate.common:hibernate-commons-annotations:5.0.1.Final
org.hibernate.validator:hibernate-validator:6.0.7.Final

org.springframework.data:spring-data-jpa:2.0.5.RELEASE
org.springframework.data:spring-data-commons:2.0.5.RELEASE

com.querydsl:querydsl-jpa:4.1.4
com.querydsl:querydsl-core:4.1.4
com.querydsl:querydsl-apt:4.1.4

实体:

@Getter
@DynamicUpdate
@Entity
@Table(
name = TABLE_NAME,
uniqueConstraints = {
@UniqueConstraint(columnNames = {CODE_COLUMN_NAME}, name = CATEGORY_CODE_UNIQUE),
@UniqueConstraint(columnNames = {NAME_COLUMN_NAME}, name = CATEGORY_NAME_UNIQUE)
})
public class Category extends Auditable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Generated(GenerationTime.INSERT)
@Column(name = CODE_COLUMN_NAME, nullable = false, updatable = false, unique = true)
private Long code;

@NotNull
@Column(name = NAME_COLUMN_NAME, nullable = false, unique = true, length = NAME_COLUMN_MAX_LENGTH)
private String name;

@ManyToOne
@JoinColumn(name = PARENT_ID_COLUMN_NAME)
private Category parent;

@OneToMany(mappedBy = TABLE_NAME, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<CategoryCountry> categoriesCountries = new HashSet<>();

@OneToMany(mappedBy = TABLE_NAME, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<CategoryTranslation> categoryTranslations = new ArrayList<>();

}

使用 JPA 2.1 的 CategoryRepository:

public Category findByIdEager(final Long id) {
final EntityGraph categoryEntityGraph = createCategoryEntityGraph();

final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

final CriteriaQuery<Category> criteriaQuery = criteriaBuilder.createQuery(Category.class);

final Root<Category> root = criteriaQuery.from(Category.class);
criteriaQuery.where(criteriaBuilder.equal(root.get("id"), id));

return categoryTypedQuery(criteriaQuery, categoryEntityGraph).getSingleResult();
}


private EntityGraph createCategoryEntityGraph() {
final EntityGraph categoryEntityGraph = entityManager.createEntityGraph(Category.class);
categoryEntityGraph.addSubgraph("categoryTranslations");
categoryEntityGraph.addSubgraph("categoriesCountries");

return categoryEntityGraph;
}


private TypedQuery<Category> categoryTypedQuery(final CriteriaQuery<Category> criteriaQuery, final EntityGraph categoryEntityGraph) {
final TypedQuery<Category> typedQuery = entityManager.createQuery(criteriaQuery);
typedQuery.setHint("javax.persistence.loadgraph", categoryEntityGraph);

return typedQuery;
}

带有查询DSL的CategoryRepository:

public Category findByIdEager(final Long id) {

QCategory category = QCategory.category;
QCategoryTranslation categoryTranslation = QCategoryTranslation.categoryTranslation;
QCategoryCountry categoryCountry = QCategoryCountry.categoryCountry;

return from(category)
.leftJoin(categoryCountry).on(categoryCountry.category.id.eq(category.id))
.leftJoin(categoryTranslation).on(categoryTranslation.category.id.eq(category.id))
.where(category.id.eq(id))
.fetchOne();
}

类别服务:

@Transactional
public getFindByIdEager() { ... }

问题:

使用 JPA 2.1 方法,当 CategoryService.getFindByIdEager 带有 @Transactional 时检索类别时包含已获取的所有国家/地区和翻译,但包含重复行

但是,当使用 QueryDSL 方法时,服务 CategoryService 返回类别实体,但不会获取国家/地区和翻译。我最终遇到了异常org.hibernate.LazyInitializationException

数据库 - 类别实体:

  ID: 2
NAME: Sport
PARENT: { ID: 1, NAME: Shoes}
COUNTRIES: [NG, EG]
TRANSLATIONS: [Shoes_Sport_1, Shoes_Sport_2, Shoes_Sport_3]

使用 JPA 2.1 进行响应

  ID: 2
NAME: Sport
PARENT: { ID: 1, NAME: Shoes}
COUNTRIES: [NG, EG]
TRANSLATIONS: [Shoes_Sport_1, Shoes_Sport_1, Shoes_Sport_2, Shoes_Sport_2, Shoes_Sport_3, Shoes_Sport_3]

使用 QueryDSL 进行响应

  ID: 2
NAME: Sport
PARENT: { ID: 1, NAME: Shoes}
COUNTRIES: `org.hibernate.LazyInitializationException`
TRANSLATIONS: `org.hibernate.LazyInitializationException`

请问有什么建议吗?

最佳答案

尝试使用 fetch Join() 而不是 left Join()。

关于hibernate - 如何在 Query DSL 4 上使用 JPA 2.1 中的 loadgraph 和 fetchgraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50216289/

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