我的班级
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来处理,但我更喜欢用会话来做,并且改变了方法。这部分以前没有给出一个错误,现在给出了一个错误。原因何在?
我找到了解决办法。您的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查询偏移量。也许有人有用,谢谢大家。
问题内容: 码: CrbtSubMasterDemo 与数据库一起映射。当我尝试运行它时,它显示以下异常: 问题是query.list()返回pojo类的对象列表。那为什么是这个异常。我是Hibernate的新手,很抱歉这是一个愚蠢的问题。 问题答案: 先生,许多用户都面临这种需求。Hibernate具有ResultTransformer来在Object中转换hql / sql。 它将您查询转换为
问题内容: 我想在hibernate状态查询两个表。用户实体中的featch 3表(User-Role-Profile)。用hql查询: 并运行查询: userentity类:此类是geteer和seter: userEntity.hbm.xml的hibernate映射 和类hibernateutil创建会话: 问题答案: 由于使用的是多选投影,因此实际上是在获取对象数组,因此需要将查询结果处理逻
查询 在这里被困了一个多小时。如有任何帮助或建议,将不胜感激。
group.java 这就是我的方法。 类型异常报告 消息请求处理失败;嵌套异常为java.lang.ClassCastException:java.base/[Ljava.lang.Object;不能强制转换为com.keating.model.group 描述服务器遇到意外情况,无法完成请求。
我在我的xhtml页面中显示一个带有链接的项目列表。当我从该链接中单击“返回上一页”按钮时,我基本上会重置bean中的标志,以向xhtml指示要显示的页面,并从数据库中重新加载链接列表。 当我在这个新页面中单击同一个链接(或任何其他链接)时,我会得到这个错误: 如果我刷新页面并重试,则不会出现问题。有人知道我做错了什么吗? ----编辑---- 以防其他使用liferay的人有这个问题,我只是在我