我正在尝试使用Hibernate Criteria API运行SELECT查询,该API在下面的代码中定义。我检查了控制台,它似乎是 查询运行正常。以下是我在SQL Query的控制台中获得的内容:
public class Book
{
public Book()
{
}
[Key]
public int IdBook { get; set; }
public string Title { get; set; }
[ForeignKey("Author")]
public int IdAuthor{ get; set; }
public Author Author { get; set; }
public ICollection<Ticket> Tickets { get; set; }
}
但是在控制台的上面的SQL下面,我看到了错误:
public class Ticket
{
public Ticket()
{
}
[Key]
public int IdTicket { get; set; }
public DateTime BorrowingDate { get; set; }
public DateTime EstimatedLandingDate { get; set; }
public DateTime? LandingTime { get; set; }
[ForeignKey("Book")]
public int IdBook { get; set; }
public Book Book { get; set; }
}
以下方法中第50行是以下行:
package main
import (
"context"
"log"
"os"
"sync"
"time"
)
type worker struct {
wg *sync.WaitGroup
in chan job
quit context.Context
}
type job struct {
message int
}
func main() {
numberOfJobs := 50
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
w := worker{
wg: &sync.WaitGroup{},
in: make(chan job),
quit: ctx,
}
for i := 0; i < numberOfJobs; i++ {
go func(i int) {
w.in <- job{message: i}
}(i)
}
counter := 0
for {
select {
case j := <-w.in:
counter++
log.Printf("Received job %+v\n", j)
// DO SOMETHING WITH THE RECEIVED JOB
// WORKING ON IT
x := j.message * j.message
log.Printf("job processed, result %d", x)
case <-w.quit.Done():
log.Printf("Recieved quit, timeout reached. Number of jobs queued: %d, Number of jobs complete: %d\n", numberOfJobs, counter)
os.Exit(0)
default:
// TODO
}
}
}
以下方法在EmployeeDaoImpl java类中定义。
Hibernate:
select
this_.VALUE_EMP_ID as y0_
from
EMPLOYEE_TOP_METADATA this_
where
this_.TESTING_ID=?
and this_.COMPANY_EMP_ID=?
这是我的实体班java.lang.ClassCastException: java.lang.Integer cannot be cast to abc.def.myproject.orm.EmployeeTopMetaData
at abc.def.myproject.orm.dao.impl.EmpDaoImpl.insertEmployeeDetails(EmployeeDaoImpl.java:50)
:
(EmployeeTopMetaData) session.createCriteria(EmployeeTopMetaData.class)
答案 0 :(得分:2)
您的查询仅返回&#34; this_.VALUE_EMP_ID&#34;一个int值。
如果要返回EmployeeTopMetaData,则必须更改查询:
Hibernate:
select
this_
from
EMPLOYEE_TOP_METADATA this_
where
this_.TESTING_ID=?
and this_.COMPANY_EMP_ID=?
但我建议如果你只需要VALUE_EMP_ID,那么只更改变量会更好。
Integer empMetaData =
(Integer) session.createCriteria(EmployeeTopMetaData.class) // This is the line #50
.setProjection(Projections.property("valueEmpId"))
.add(Restrictions.eq("testingId", 1234))
.add(Restrictions.eq("company_employee_id", 3345))
.uniqueResult();