首先,以下是使用的技术:hibernate v5.1.0.Final,javaee-api 7.0,Java 8,Postgres 9.6.1 给出的是 postgres db:
CREATE TABLE employee (
id SERIAL PRIMARY KEY,
name varchar NOT NULL ,
last_name varchar NOT NULL ,
hired_date date
);
CREATE TABLE ticket (
id SERIAL PRIMARY KEY,
parent_ticket_id bigint REFERENCES ticket (id),
code varchar,
description varchar
);
CREATE TYPE ticket_status AS ENUM ('OPENED', 'RESOLVED', 'CLOSED');
CREATE TABLE tickets_assigned (
id SERIAL PRIMARY KEY,
employee_id bigint REFERENCES employee (id) NOT NULL ,
ticket_id bigint REFERENCES ticket (id) NOT NULL ,
status ticket_status,
assigned_date timestamp,
resolved_date timestamp,
closed_date timestamp,
UNIQUE (employee_id, ticket_id)
);
我在数据库中插入了一些值。例如。我插入埃尔维斯普雷斯利所有的东西
关闭1张门票。
然后我在psql中创建一个查询(考虑ROW_NUMBER() OVER, COUNT and GROUP BY
)来查看Elvis的统计数据:
SELECT e.id,
e.name,
e.last_name,
e.hired_date,
ROW_NUMBER() OVER(ORDER BY e.id ASC),
COUNT(a.id)
FROM employee e
LEFT JOIN assigned_tickets a ON e.id=a.employee_id
AND a.status='CLOSED'
GROUP BY e.id
这对我来说非常有效,像:
...
2 elvis presley 1967-10-10 2 1
...
然后我使用
Entitymanager.createNativeQuery(...)
在我的EJB项目中使用Hibernate v5.1.0进行SAME SQL QUERY,这就是我得到的:
COUNT()
GROUP BY
在任何情况下都会返回0,无视输入。ROW_NUMBER() OVER(e.id ASC)
感叹,让我写出像ROW_NUMBER() OVER()
这样的表达式,这会导致我没有预料到的行为(例如2 guys with #3
和1 guy with #2
list.length=3
}})。 猫王变得像:2 elvis presley 1967-10-10 3 0
我如何解决这两个问题? 我请求你的帮助。如果需要,可以提供所有其他的东西。
UPD。
entityManager.createNativeQuery("SELECT e.id as ID, e.name AS NAME," +
" e.last_name AS LAST_NAME, e.hired_date AS HIRED_DATE," +
" ROW_NUMBER() OVER(ORDER BY e.id ASC) AS ROW_NUM, COUNT(a.id) AS NUM_CLOSED FROM employee e" +
" LEFT JOIN assigned_tickets a ON e.id=a.employee_id AND a.status='CLOSED' GROUP BY e.id",
"EmpDtoMapping")
.getResultList()
Hibernate哀叹:
Caused by: org.h2.jdbc.JdbcSQLException: ?????????????? ?????? ? ????????? SQL "SELECT E.ID AS ID, E.NAME AS NAME, E.LAST_NAME AS LAST_NAME, E.HIRED_DATE AS HIRED_DATE, ROW_NUMBER() OVER(ORDER[*] BY E.ID ASC) AS ROW_NUM, COUNT(A.ID) AS NUM_CLOSED FROM EMPLOYEE E LEFT JOIN ASSIGNED_TICKETS A ON E.ID=A.EMPLOYEE_ID AND A.STATUS='CLOSED' GROUP BY E.ID "; ????????? ")" Syntax error in SQL statement "SELECT E.ID AS ID, E.NAME AS NAME, E.LAST_NAME AS LAST_NAME, E.HIRED_DATE AS HIRED_DATE, ROW_NUMBER() OVER(ORDER[*] BY E.ID ASC) AS ROW_NUM, COUNT(A.ID) AS NUM_CLOSED FROM EMPLOYEE E LEFT JOIN ASSIGNED_TICKETS A ON E.ID=A.EMPLOYEE_ID AND A.STATUS='CLOSED' GROUP BY E.ID "; expected ")"; SQL statement: SELECT e.id as ID, e.name AS NAME, e.last_name AS LAST_NAME, e.hired_date AS HIRED_DATE, ROW_NUMBER() OVER(ORDER BY e.id ASC) as ROW_NUM, COUNT(a.id) as NUM_CLOSED FROM employee e LEFT JOIN assigned_tickets a ON e.id=a.employee_id AND a.status='CLOSED' GROUP BY e.id [42001-173]
期望获得:
id name surname date row# num of closed tkts
1 elvis presley 1967-10-10 1 1
如果试图修复我写:
ROW_NUMBER() OVER()
代替ROW_NUMBER() OVER(ORDER BY e.id ASC)
我得到了:
1 elvis presley 1967-10-10 2 0
使用COUNT(a.id) ... GROUP BY e.id
ResultSet sql映射(仅适用于一般订单):
@SqlResultSetMapping(
name="EmpDtoMapping",
classes={
@ConstructorResult(
targetClass=com.alexchulkin.dto.EmpDto.class,
columns={
@ColumnResult(name="ID", type = Long.class),
@ColumnResult(name="NAME", type = String.class),
@ColumnResult(name="LAST_NAME", type = String.class),
@ColumnResult(name="HIRED_DATE", type = Date.class),
@ColumnResult(name="ROW_NUM", type = Integer.class),
@ColumnResult(name="NUM_CLOSED", type = BigInteger.class)
})})
在JBOSS启动期间UPD2 部署了h2和postgres驱动程序:
20:24:52,004 INFO [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 33) WFLYJCA0004: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
20:24:52,022 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) WFLYJCA0018: Started Driver service with driver-name = h2
20:25:05,786 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) WFLYJCA0005: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 9.3)
20:25:08,716 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-3) WFLYJCA0018: Started Driver service with driver-name = DI-project-1.0-SNAPSHOT.war_org.postgresql.Driver_9_3
的persistence.xml:
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/persistence"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="entityManager">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<!-- Annotated entity classes -->
<class>com.alexchulkin.domain.Emp</class>
<class>com.alexchulkin.domain.Tkt</class>
<class>com.alexchulkin.domain.AssignedTkt</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost/di_project" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true" />
<property name="format_sql" value="true" />
<property name="use_sql_comments" value="true" />
<!--<property name="hibernate.hbm2ddl.import_files" value="/schema.sql"/>-->
</properties>
</persistence-unit>