休眠 java.lang.ClassCastException [Ljava.lang.Object;无法投射到我的 class

Hibernate java.lang.ClassCastException [Ljava.lang.Object; cannot be cast to my class

我的Class

 package com.pizzaweb.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.*;
@Entity
@Table(name="Urunler")
public class Urunler {
     @Id
     @GeneratedValue(strategy=GenerationType.IDENTITY)
     @Column(name="ID")//1
     protected  int ID;
     @Column(name="PersonelID")//1tm
     protected  int PersonelID;
     @Column(name="KategoriID")
     protected  int KategoriID;
     @Column(name="UrunAd")
     protected  String UrunAd;
     @Column(name="UrunFiyat")
     protected  float UrunFiyat;
     @Column(name="UrunStok")
     protected  int UrunStok;
     @Column(name="UrunResim")
     protected  String UrunResim;
     @Column(name="UrunDetay")
     protected  String UrunDetay;
     @Column(name="EklenmeTarih")
     protected Date EklenmeTarih;
     @Column(name="UrunPuan")
     protected int UrunPuan;
     @Column(name="UrunOncelik")
     protected int UrunOncelik;
     @Column(name="EskiFiyat")
     protected float EskiFiyat;
         public Urunler() {
             
         }
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public int getPersonelID() {
        return PersonelID;
    }
    public void setPersonelID(int personelID) {
        PersonelID = personelID;
    }
    public int getKategoriID() {
        return KategoriID;
    }
    public void setKategoriID(int kategoriID) {
        KategoriID = kategoriID;
    }
    public String getUrunAd() {
        return UrunAd;
    }
    public void setUrunAd(String urunAd) {
        UrunAd = urunAd;
    }
    public float getUrunFiyat() {
        return UrunFiyat;
    }
    public void setUrunFiyat(float urunFiyat) {
        UrunFiyat = urunFiyat;
    }
    public int getUrunStok() {
        return UrunStok;
    }
    public void setUrunStok(int urunStok) {
        UrunStok = urunStok;
    }
    public String getUrunResim() {
        return UrunResim;
    }
    public void setUrunResim(String urunResim) {
        UrunResim = urunResim;
    }
    public String getUrunDetay() {
        return UrunDetay;
    }
    public void setUrunDetay(String urunDetay) {
        UrunDetay = urunDetay;
    }
    public Date getEklenmeTarih() {
        return EklenmeTarih;
    }
    public void setEklenmeTarih(Date eklenmeTarih) {
        EklenmeTarih = eklenmeTarih;
    }
    public int getUrunPuan() {
        return UrunPuan;
    }
    public void setUrunPuan(int urunPuan) {
        UrunPuan = urunPuan;
    }
    public int getUrunOncelik() {
        return UrunOncelik;
    }
    public void setUrunOncelik(int urunOncelik) {
        UrunOncelik = urunOncelik;
    }
    public float getEskiFiyat() {
        return EskiFiyat;
    }
    public void setEskiFiyat(float eskiFiyat) {
        EskiFiyat = eskiFiyat;
    }
}

我的休眠 class

 package com.pizzaweb.util;
import java.sql.SQLException;
import java.util.Properties;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;

import com.pizzaweb.model.Urunler;
public class PizzaHiber {
     private static SessionFactory sessionFactory;
     public static SessionFactory getSessionFactory() throws SQLException{
          if (sessionFactory == null) {
           try {
            Configuration configuration = new Configuration();
        Properties settings = new Properties();
        settings.put(Environment.DRIVER, "com.microsoft.sqlserver.jdbc.SQLServerDriver");
        settings.put(Environment.URL, "jdbc:sqlserver://localhost:1433;DatabaseName=Pizza");
        settings.put(Environment.USER, "maya");
        settings.put(Environment.PASS, "123");
        settings.put(Environment.DIALECT, "org.hibernate.dialect.SQLServerDialect");

        settings.put(Environment.SHOW_SQL, "true");

        settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");

        settings.put(Environment.HBM2DDL_AUTO, "none");

        configuration.setProperties(settings);
        configuration.addAnnotatedClass(Urunler.class); //sınıflar bu şekilde eklenecek.

        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
          .applySettings(configuration.getProperties()).build();
        System.out.println("Hibernate Java Config serviceRegistry created");
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        return sessionFactory;

       } catch (Exception e) {
        e.printStackTrace();
        
       }
      }
      return sessionFactory;
     }
}

此处例外

@SuppressWarnings("unchecked")
private List<UrunList> getUrun(int Page,int KategoriID){
    int offset = (Page - 1)*8;
    String query = "SELECT * FROM Urunler where UrunStok > 0 AND KategoriID = "+KategoriID+" ORDER BY ID OFFSET "+offset+" ROWS FETCH NEXT 8 ROWS ONLY";
    try {
        Session session = PizzaHiber.getSessionFactory().openSession();
        List<Urunler> urunler =  (List<Urunler>) session.createSQLQuery(query).list(); // exception here (List<Urunler>) cast
        return urunler;
    } catch (HibernateException | SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

异常详情

Nis 26, 2021 4:56:37 ÖS org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.pizzaweb.controller.urun.Urun] in context with path [/PizzaWeb] threw exception
java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class com.pizzaweb.model.Urunler ([Ljava.lang.Object; is in module java.base of loader 'bootstrap'; com.pizzaweb.model.Urunler is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @2b936b04)
    at com.pizzaweb.controller.urun.Urun.getUrun(Urun.java:58)
    at com.pizzaweb.controller.urun.Urun.doPost(Urun.java:39)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:689)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:543)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:353)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:870)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1696)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:832)

我通过 JSP 处理参数并在 servlet 的方法中处理这些参数。我之前能够将数据处理为 json 和 ajax,但我更喜欢使用会话来处理数据并更改了方法。这部分之前没有报错,现在报错了。这是什么原因?

我找到了解决方案。您的休眠设置也 settings.put (Environment.USE_LEGACY_LIMIT_HANDLERS, "true"); 需要补充。此外,应该在 sessionFactory 上使用 createQuery 而不是 createSqlQuery。例如;

List <Products> products = (List <Products>) session.createQuery("PRODUCTS ProductStock> 0 AND CategoryID = 1").SetFirstResult(8).setMaxResults(8).list ();

问题是它不支持 SQL 休眠时的查询偏移量。或许有人有用,谢谢大家