Java org.hibernate.QueryException:意外标记:as
将项目切换到maven,使用最新版本的Hibernate。我被困在试图解决这个问题,确切的代码工作过。我觉得hibernate找不到映射文件之类的东西?我的所有xml文件都在resources文件夹中。使用IntellijJava org.hibernate.QueryException:意外标记:as,java,hibernate,Java,Hibernate,将项目切换到maven,使用最新版本的Hibernate。我被困在试图解决这个问题,确切的代码工作过。我觉得hibernate找不到映射文件之类的东西?我的所有xml文件都在resources文件夹中。使用Intellij org.hibernate.QueryException: unexpected token: as [from Tickets as t where t.inplay = true and t.bin < 30 ORDER BY t.bin ASC] at org.h
org.hibernate.QueryException: unexpected token: as [from Tickets as t where t.inplay = true and t.bin < 30 ORDER BY t.bin ASC]
at org.hibernate.QueryException.generateQueryException(QueryException.java:120)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:233)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:193)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:150)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:298)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1836)
at dao.TicketDAO.get30Tickets(TicketDAO.java:88)
at gui.SellingMain.reloadGameButtons(SellingMain.java:98)
at gui.SellingMain.<init>(SellingMain.java:78)
at gui.SellingMain$39.run(SellingMain.java:2030)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
HibernateUtil:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main.resources;
/**
*
* @author Switcher
*/
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static Session currentSession;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
// loads configuration and mappings
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// builds a session factory from the service registry
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
}
/**
* Handle sessions and close them at end of HTTP transaction instead of
* after tx.commit()
*
* @return the session
*/
public static Session getSession() {
if (currentSession == null) {
currentSession = sessionFactory.openSession();
}
return currentSession;
}
/**
* Close open session.
*/
public static void closeSession() {
if (currentSession != null) {
currentSession.close();
currentSession = null;
}
}
}您不能以这种方式配置Hibernate 5(它仅对Hibernate 4有效)
Configuration
在执行buildSessionFactory(serviceRegistry)
操作时,会丢失所有映射信息
只需使用它来构建一个会话工厂
sessionFactory = new Configuration().configure().buildSessionFactory();
您需要提交一个事务并关闭会话。在其他情况下也不要忘记回滚。这基本上取决于数据库。有些数据库支持关键字,有些则不支持。所以从查询中删除as,然后重试
ts = session.createQuery("from Tickets where inplay =: inplayValue and bin < :binValule ORDER BY bin ASC";
ts.setParameter("inplayValue ", Boolean.valueOf("true");
ts.setParameter("binValule", 30);
ts.list();
ts=session.createQuery(“来自票证,其中inplay=:inplayValue和bin<:binvalueorderbybinasc”;
ts.setParameter(“inplayValue”,Boolean.valueOf(“true”);
ts.setParameter(“binValule”,30);
ts.list();
我不知道您使用的是什么ddbb,也许这可以帮助您:
我在用oracle booleans映射java booleans时遇到了很多问题您是否尝试过不使用“t”,从任何地方删除“t”或只是删除“AS”并再次尝试哪个版本的Hibernate?请添加
HibernateUtil
code。听起来您切换到了Hibernate版本,该版本对HQL使用了稍微不同的标准。@v.ladynev 5.1。0.Final,在HibernateUtil中编辑。谢谢,为我提供了足够的信息来修复它。查阅了5.1文档,发现了与以前几乎相同的代码,工作原理与以前相同。@Switcher05不客气。Hibernate有一个不太好的文档。例如,一个不适用于Hibernate 5的示例
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry
= new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
// builds a session factory from the service registry
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
sessionFactory = new Configuration().configure().buildSessionFactory();
ts = session.createQuery("from Tickets where inplay =: inplayValue and bin < :binValule ORDER BY bin ASC";
ts.setParameter("inplayValue ", Boolean.valueOf("true");
ts.setParameter("binValule", 30);
ts.list();