在本机查询中将fetchtype.lazy更改为fetchtype.eager temporary at runtime(hibernate/jpa)

egdjgwm8  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(353)

我有以下方法

@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;

}
r6l8ljro

r6l8ljro1#

您需要创建一个customdialect,它从您正在使用的方言类扩展而来,并在注册器中以以下方式创建您不能与jpql一起使用的函数:

public CustomDialect() {
    super();

    registerFunction(
        "func_custom",
        new StandardSQLFunction(
            "func_custom",
            StandardBasicTypes.STRING
        )
    );
}

你把方言放在哪里:

<property name="hibernate.dialect" value="your.package.CustomDialect"/>

相关问题