环境:
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
返回了Category实体,但是国家和翻译没有被获取。我最终遇到了异常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`
有什么建议吗?