Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JPARepository中获取聚合查询结果_Java_Spring_Jpa - Fatal编程技术网

Java 在JPARepository中获取聚合查询结果

Java 在JPARepository中获取聚合查询结果,java,spring,jpa,Java,Spring,Jpa,我正在尝试从我的应用程序中的JPARepository获取聚合数据。SQL类比如下: SELECT c.sex as Sex, count(c.sex) as Count FROM customer c GROUP BY c.sex 该实体为: @Entity(name = "customer") public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) private L

我正在尝试从我的应用程序中的JPARepository获取聚合数据。SQL类比如下:

SELECT c.sex as Sex, count(c.sex) as Count 
FROM customer c
GROUP BY c.sex
该实体为:

@Entity(name = "customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Person.Sex sex;
    ...
}
我的假设是:

public interface CustomerRepository extends JpaRepository<Customer, Long> {

    @Query(value = "SELECT c.sex as Sex, count(c.sex) as Count FROM customer c")
    List<Object[]> countBySex();
}
公共接口CustomerRepository扩展了JpaRepository{
@查询(value=“选择c.sex作为性别,将(c.sex)作为客户c的计数”)
List countBySex();
}
SQL方法不返回任何结果,为什么不返回,还有非SQL方法吗

我使用的是Spring1.4.0.RELEASE

提前谢谢


编辑:当我为JPA添加persistence.xml配置以及相关类(Customer.class)的映射时,SQL方法起了作用。

要从域或表中获取自定义记录,我们需要遵循其他方法。 我们可以使用jdbcTemplate获得结果,并使用行映射器类将其与dto绑定


有关更多详细信息,请浏览

当我为JPA添加persistence.xml配置并映射相关类(Customer.class)时,SQL方法起到了作用。否则,应用程序无法从查询中识别表“Customer”

persistence.xml代码如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="jpa.sample.plain">
    <class>net.datamanager.application.Customer</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
        <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:spring" />
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
        <property name="hibernate.connection.username" value="sa" />
        <property name="hibernate.connection.password" value="" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 
    </properties>
</persistence-unit>

net.datamanager.application.Customer