使用构造函数表达式和JPQL使用DTO投影获取列表

时间:2018-07-19 07:11:49

标签: spring spring-mvc spring-boot spring-data-jpa jpql

在DisabScreenRequest上执行搜索,并同时获取其子级详细信息。使用构造函数表达式和JPQL使用DTO投影。

具有子表的父实体。

@Entity
@Table(name = "SCREEN_REQUEST")
public class DisabScreenRequest implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id 
    private long requestId;

    @Column(name = "CIVILID")
    private Long civilId;

    @ManyToMany()
    @JoinTable(name = "_DISAB_SCREEN_REQ_DETAILS", joinColumns = {
            @JoinColumn(name = "REQUEST_ID") }, inverseJoinColumns = { @JoinColumn(name = "DISABILTY_TYPE_ID") })
    private Set<DisabMaster> disabilities = new HashSet<DisabMaster>();

    public DisabScreenRequest() {
    }

}

这是残疾表。

@Entity
@Table(name="DISAB_MASTER")
@Immutable 
public class DisabMaster implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="DIS_TYPE_ID")
    private long disabilityTypeId;

    @Column(name="DIS_TYPE_DESC")
    private String disTypeDesc;


    public DisabMaster() {
        super();
    }   

}

必须提取所有请求以及每个请求的残障状态。

搜索DTO(使用此方法,除了这里提到的一个以外,我还有其他联接可以添加。)

public class RequestSearchDto {

    private long requestId;     

    private Long civilId;   

    private  Set<DisabMaster> disabilities;

    public RequestSearchDto() {
        super();
    }

    public RequestSearchDto(long requestId, Long civilId) {
        super();
        this.requestId = requestId;     
        this.civilId = civilId;     
    }

    public RequestSearchDto(long requestId, Long civilId, Set<DisabMaster>  disabilities) {
        super();
        this.requestId = requestId;     
        this.civilId = civilId;     
        this.disabilities = disabilities;
    }   


}

这是我的JPQL查询

public interface ReposJPQL {

    public String GET__REQUEST = "SELECT DISTINCT new org.test.RequestSearchDto "
            + "(dsr.requestId, dsr.civilId, dsr.disabilities)"
            + " FROM DisabScreenRequest dsr WHERE 1=1 ";


}

这将得到一个 org.hibernate.exception.SQLGrammarException:无法提取ResultSet。

我在这里做错了什么,如何获取子表数据? 让我知道您是否需要任何信息

堆栈跟踪:

Caused by: java.sql.SQLException: ORA-00936: missing expression

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:113)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:754)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:219)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:813)
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1051)
    at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:854)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1156)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3415)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3460)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)

3 个答案:

答案 0 :(得分:4)

如果您需要使用其嵌套子实体的集合来获取父实体,则可以使用带有@EntityGraph批注或带有join fetch的JPQL的简单方法:

@Entity
public class Parent {
    //...
    @OneToMany
    private List<Child> children;
}

@Entity
public class Child {
    //...
}

interface ParentRepo extends JpaRepository<Parent, Integer> {

    // with @EntityGraph
    @EntityGraph(attributePaths = "children")
    @Override
    List<Parent> findAll(); 

    // or manually
    @Query("select distinct p from Parent p left join fetch p.children")
    List<Parent> findWithQuery(); 
}

请注意在查询中使用distinct,以避免重复记录。

示例:duplicate-parent-entities

更多信息:DATAJPA-1299

答案 1 :(得分:2)

AFAIK,您不能使用带有Collection的构造函数表达式。

请参见JPA 2.2 Spec,第4.14 BNF节,了解有关构造函数表达式的信息:

constructor_expression ::=
    NEW constructor_name ( constructor_item {, constructor_item}* )
constructor_item ::=
    single_valued_path_expression |
    scalar_expression |
    aggregate_expression |
    identification_variable

答案 2 :(得分:1)

这是Blaze-Persistence Entity Views的完美用例。

我创建了该库,以允许在JPA模型和自定义接口定义的模型之间轻松映射,例如类固醇上的Spring Data Projections。这个想法是您以自己喜欢的方式定义目标结构,并通过JPQL表达式将属性(获取器)映射到实体模型。由于属性名称用作默认映射,因此大多数情况下您不需要显式映射,因为80%的用例都是将DTO作为实体模型的子集。

模型的映射看起来像下面的一样简单

@EntityView(DisabScreenRequest.class)
interface RequestSearchDto extends Serializable {
  @IdMapping
  long getRequestId();
  Long getCivilId();
  Set<DisabMaster> getDisabilities();
}

查询是将实体视图应用于查询的问题,最简单的方法就是按ID查询。

RequestSearchDtodto = entityViewManager.find(entityManager, RequestSearchDto.class, id);

但是Spring Data集成允许您像使用Spring Data Projections一样使用它:https://persistence.blazebit.com/documentation/1.4/entity-view/manual/en_US/#spring-data-features