我有以下方法
@Query(value = "SELECT * FROM property p JOIN address a USING(a_id) JOIN address pa ON p.post_a_id = pa.a_id " +
"JOIN section s ON s.p_id = p.p_id " +
"WHERE p.organization_id = :orgId AND p.name ILIKE " +
"ANY(SELECT UNNEST(ARRAY(SELECT CONCAT('%', UNNEST(STRING_TO_ARRAY(:search, ' ')), '%'))))",
nativeQuery = true)
Page<Property> findPropertiesByOrganizationIdAndName(@Param("orgId") Long organizationId,
@Param("search") String search,
Pageable pageable);
但当它试图获取节实体时,会得到lazyinitializationexception
如何使用本机sql动态更改节enity的获取类型?
财产实体:
@Entity
@Table
public class Property extends CreatedDateAuditedEntity {
@OneToMany(mappedBy = "property", cascade = CascadeType.REMOVE)
@Fetch(value = FetchMode.SUBSELECT)
private Set<Section> sections = new HashSet<>();
}
部门实体:
@Entity
public class Section extends CreatedDateAuditedEntity {
@ManyToOne(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
@JoinColumn(name = "p_id", referencedColumnName = "p_id", updatable = false, nullable = false)
private Property property;
}
1条答案
按热度按时间r6l8ljro1#
您需要创建一个customdialect,它从您正在使用的方言类扩展而来,并在注册器中以以下方式创建您不能与jpql一起使用的函数:
你把方言放在哪里: