环境:
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();
}
CategoryService:
@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`
有什么建议吗?
答案 0 :(得分:0)
尝试使用fetchJoin()而不是leftJoin()。