当前位置: 首页 > 知识库问答 >
问题:

Hibernate java.lang.ClassCastException[Ljava.lang.Object;不能强制转换为我的类

羊时铭
2023-03-14

我的班级

 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;
    }
}

我的Hibernate类

 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的方法中处理这些参数。我以前能够用ajax将数据作为json来处理,但我更喜欢用会话来做,并且改变了方法。这部分以前没有给出一个错误,现在给出了一个错误。原因何在?

共有1个答案

钱安和
2023-03-14

我找到了解决办法。您的Hibernate设置也是设置。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 ();

问题是它不支持Hibernate上的SQL查询偏移量。也许有人有用,谢谢大家。

 类似资料: