直接向DTO查询结果

时间:2019-02-17 07:42:06

标签: java spring-boot spring-data

当我尝试直接从spring数据查询结果到DTO类时,出现此错误

  

原因:java.lang.IllegalArgumentException:org.hibernate.QueryException:无法解析属性:test.dao.bean.Car的owner_name [选择新的test.dto.bean.CarOwners(c.owner_name,c。 owner_nid,c.owner_phone,c.driver_name,c.driver_nid,c.driver_phone)(来自test.dao.bean.Car c)       在org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]       在org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]       在org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]       在org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:670)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]       在org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final]       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_152]       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_152]       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_152]       在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_152]       在org.springframework.orm.jpa.ExtendedEntityManagerCreator $ ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350)〜[spring-orm-5.0.7.RELEASE.jar:5.0.7.RELEASE]       在com.sun.proxy。$ Proxy123.createQuery(未知来源)〜[na:na]       在org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:87)〜[spring-data-jpa-2.0.8.RELEASE.jar:2.0.8.RELEASE]       ...省略了76个通用框架

这是我的查询

@Query("SELECT new test.dto.bean.CarOwners(c.owner_name, c.owner_nid, c.owner_phone, c.driver_name, c.driver_nid, c.driver_phone) FROM car c")
public List<CarOwners> findAllCarOwners(); 

这是DTO类:

test.dto.bean;

public class CarOwners{

private String owner_name;
private Long owner_nid;
private Integer owner_phone;
private String driver_name;
private Long driver_nid;
private Integer driver_phone;

 public CarOwners(String owner_name, Long owner_nid, Integer owner_phone, String driver_name, Long driver_nid, Integer driver_phone) {
        this.owner_name = owner_name;
        this.owner_nid = owner_nid;
        this.owner_phone = owner_phone;
        this.driver_name = driver_name;
        this.driver_nid = driver_nid;
        this.driver_phone = driver_phone;

    }

public String getOwner_name() {
    return owner_name;
}


public void setOwner_name(String owner_name) {
    this.owner_name = owner_name;
}


public Long getOwner_nid() {
    return owner_nid;
}


public void setOwner_nid(Long owner_nid) {
    this.owner_nid = owner_nid;
}


public Integer getOwner_phone() {
    return owner_phone;
}


public void setOwner_phone(Integer owner_phone) {
    this.owner_phone = owner_phone;
}


public String getDriver_name() {
    return driver_name;
}


public void setDriver_name(String driver_name) {
    this.driver_name = driver_name;
}


public Long getDriver_nid() {
    return driver_nid;
}


public void setDriver_nid(Long driver_nid) {
    this.driver_nid = driver_nid;
}


public Integer getDriver_phone() {
    return driver_phone;
}


public void setDriver_phone(Integer driver_phone) {
    this.driver_phone = driver_phone;
}
}

请帮助??

1 个答案:

答案 0 :(得分:0)

  

由于:java.lang.IllegalArgumentException:   org.hibernate.QueryException:无法解析属性:owner_name   来自:test.dao.bean.Car

显然,select子句中的字段应引用映射实体(Car)的 Java属性名称,而不是DTO中的属性名称。

所以假设汽车是:

@Entity
@Table(name="car")
public class Car{


    @Column(name="owner_name")
    private String ownerName;

}

那你应该写:

@Query("SELECT new test.dto.bean.CarOwners(c.ownerName) FROM car c") 

实际上,DTO字段的名称与如何编写@Query字符串无关。它仅与DTO构造函数参数和SELECT NEW子句中的字段之间的顺序匹配。您可以使用任何DTO名称,只要它们的顺序和数据类型匹配即可。