Derby SQL查询不起作用

时间:2018-07-15 14:19:44

标签: java jpa-2.0 jpql derby

im正在尝试运行derby sql查询,但是它不起作用。请谁能告诉我这是怎么了?

这是JPA实体类

员工类别

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Integer id;

@Column(name = "FIRST_NAME")
private String firstName;

@Column(name = "NAME")
private String name;

@Column(name = "START_DATE")
@Temporal(TemporalType.DATE)
private Date startDate;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "employeeId")
private Set<Leave> leaveSet;

离开课程

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Integer id;

@Column(name = "NUMBER_OF_DATES")
private int numberOfDates;

@Column(name = "LEAVE_TYPE")
private String leaveType;

@Column(name = "NUMBER_OF_DAYS_OF_TYPE")
private int numberOfDaysOfType;

@JoinColumn(name = "EMPLOYEE_ID", referencedColumnName = "ID")
@ManyToOne(optional = false)
private Employee employeeId;

这是我尝试运行的德比查询,

select 
    SUM(l.NUMBER_OF_DATES), 
    (l.NUMBER_OF_DAYS_OF_TYPE - SUM(l.NUMBER_OF_DATES)), 
    l.LEAVE_TYPE, 
    l.FIRST_NAME
from 
    LEAVE_TS l, 
    employee e
where 
    l.EMPLOYEE_ID= e.ID
group by  
    l.LEAVE_TYPE

这给了我以下例外,

java.sql.SQLSyntaxErrorException: Column reference 'L.NUMBER_OF_DAYS_OF_TYPE' is invalid, or is part of an invalid expression.  For a SELECT list with a GROUP BY, the columns and expressions being selected may only contain valid grouping expressions and valid aggregate expressions.

相当于JP-QL query

SELECT 
    SUM(l.numberOfDates), 
    (l.numberOfDaysOfType - SUM(l.numberOfDates)), 
    l.leaveType, 
    e.firstName 
FROM 
    Employee e, 
    Leave l 
where 
    l.employeeId = e.id 
GROUP BY 
    l.leaveType

这给了我以下例外,

Internal Exception: java.sql.SQLSyntaxErrorException: Column 'T1.ID.T1' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE  statement then 'T1.ID.T1' is not a column in the target table.
Error Code: -20001

Call: SELECT SUM(t0.NUMBER_OF_DATES), (t0.NUMBER_OF_DAYS_OF_TYPE - SUM(t0.NUMBER_OF_DATES)), t0.LEAVE_TYPE, t1.FIRST_NAME FROM leave_ts t0, employee t1 WHERE (t0.EMPLOYEE_ID = t1.ID.t1.ID) GROUP BY t0.LEAVE_TYPE
Query: ReportQuery(referenceClass=Employee sql="SELECT SUM(t0.NUMBER_OF_DATES), (t0.NUMBER_OF_DAYS_OF_TYPE - SUM(t0.NUMBER_OF_DATES)), t0.LEAVE_TYPE, t1.FIRST_NAME FROM leave_ts t0, employee t1 WHERE (t0.EMPLOYEE_ID = t1.ID.t1.ID) GROUP BY t0.LEAVE_TYPE")
    at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:377)
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
    at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:468)
    at com.ejb.emptrackr.bean.LeaveRequestSessionBean.getLeaveRequestsSummeryByEmployee(LeaveRequestSessionBean.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

我做错了什么?

0 个答案:

没有答案