【问题标题】:could not execute query in Hibernate with MySQL无法使用 MySQL 在 Hibernate 中执行查询
【发布时间】:2014-06-23 11:21:32
【问题描述】:

DAO

public class TtmpHome {
private static final Log log = LogFactory.getLog(TtmpHome.class);
// Contexte de persistance
private EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
private EntityManager entityManager = emf.createEntityManager();

//staff

public Ttmp findByStationName(Integer stNumber) {
    String queryS = "select t from Ttmp t where t.stationNumber =?1";
    Ttmp t = null;
    try {
        Query query = entityManager.createQuery(queryS);
        query.setParameter(1, stNumber);
        List eqList = query.getResultList();
        if (eqList != null && eqList.size() > 0)
            t = (Ttmp) eqList.get(0);
        log.debug("get successful");
        return t;
    } catch (RuntimeException re) {
        log.error("get failed", re);
    }
}
}

实体

@Entity
@Table(name = "ttmp", catalog = "semap")
public class Ttmp implements java.io.Serializable {

    private int id;
    private String requestNumber;
    private String requestFrom;
    private Date requestDate;
    private Integer stationNumber;
    private String site;
    private String region;
    private String requestReceiver;
    private String descIssue;
    private String thirdParty;
    private String descResolutionAction;
    private String closerType;
    private Integer closeNum;
    private Date dateAction;
    private String agentAction;
    private String validation;
    private String comment;

    public Ttmp() {
    }

    public Ttmp(int id) {
        this.id = id;
    }

    public Ttmp(int id, String requestNumber, String requestFrom,
            Date requestDate, Integer stationNumber, String site,
            String region, String requestReceiver, String descIssue,
            String thirdParty, String descResolutionAction, String closerType,
            Integer closeNum, Date dateAction, String agentAction,
            String validation, String comment) {
        this.id = id;
        this.requestNumber = requestNumber;
        this.requestFrom = requestFrom;
        this.requestDate = requestDate;
        this.stationNumber = stationNumber;
        this.site = site;
        this.region = region;
        this.requestReceiver = requestReceiver;
        this.descIssue = descIssue;
        this.thirdParty = thirdParty;
        this.descResolutionAction = descResolutionAction;
        this.closerType = closerType;
        this.closeNum = closeNum;
        this.dateAction = dateAction;
        this.agentAction = agentAction;
        this.validation = validation;
        this.comment = comment;
    }

    @Id
    @Column(name = "id", unique = true, nullable = false)
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "request_number")
    public String getRequestNumber() {
        return this.requestNumber;
    }

    public void setRequestNumber(String requestNumber) {
        this.requestNumber = requestNumber;
    }

    @Column(name = "request_from")
    public String getRequestFrom() {
        return this.requestFrom;
    }

    public void setRequestFrom(String requestFrom) {
        this.requestFrom = requestFrom;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "request_date", length = 0)
    public Date getRequestDate() {
        return this.requestDate;
    }

    public void setRequestDate(Date requestDate) {
        this.requestDate = requestDate;
    }

    @Column(name = "station_number")
    public Integer getStationNumber() {
        return this.stationNumber;
    }

    public void setStationNumber(Integer stationNumber) {
        this.stationNumber = stationNumber;
    }

    @Column(name = "site")
    public String getSite() {
        return this.site;
    }

    public void setSite(String site) {
        this.site = site;
    }

    @Column(name = "region")
    public String getRegion() {
        return this.region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    @Column(name = "request _receiver")
    public String getRequestReceiver() {
        return this.requestReceiver;
    }

    public void setRequestReceiver(String requestReceiver) {
        this.requestReceiver = requestReceiver;
    }

    @Column(name = "desc_issue", length = 500)
    public String getDescIssue() {
        return this.descIssue;
    }

    public void setDescIssue(String descIssue) {
        this.descIssue = descIssue;
    }

    @Column(name = "third_party")
    public String getThirdParty() {
        return this.thirdParty;
    }

    public void setThirdParty(String thirdParty) {
        this.thirdParty = thirdParty;
    }

    @Column(name = "desc_resolution_action", length = 500)
    public String getDescResolutionAction() {
        return this.descResolutionAction;
    }

    public void setDescResolutionAction(String descResolutionAction) {
        this.descResolutionAction = descResolutionAction;
    }

    @Column(name = "closer_type")
    public String getCloserType() {
        return this.closerType;
    }

    public void setCloserType(String closerType) {
        this.closerType = closerType;
    }

    @Column(name = "close_num")
    public Integer getCloseNum() {
        return this.closeNum;
    }

    public void setCloseNum(Integer closeNum) {
        this.closeNum = closeNum;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "date_action", length = 0)
    public Date getDateAction() {
        return this.dateAction;
    }

    public void setDateAction(Date dateAction) {
        this.dateAction = dateAction;
    }

    @Column(name = "agent_action")
    public String getAgentAction() {
        return this.agentAction;
    }

    public void setAgentAction(String agentAction) {
        this.agentAction = agentAction;
    }

    @Column(name = "validation")
    public String getValidation() {
        return this.validation;
    }

    public void setValidation(String validation) {
        this.validation = validation;
    }

    @Column(name = "comment", length = 500)
    public String getComment() {
        return this.comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }
}

当我试图在我的代码的某些部分中获取数据时,我得到了那个错误:

Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:630)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:75)
    at sau.se.migration.dao.TtmpHome.findByStationName(TtmpHome.java:87)
    at sau.se.editor.dialog.StationInfo.createIncidentsTable(StationInfo.java:110)
    at sau.se.editor.dialog.StationInfo.<init>(StationInfo.java:750)
    at sau.se.editor.dialog.StationInfoAction.actionPerformed(StationInfoAction.java:19)
    at sau.se.editor.BasicGraphEditor$14.actionPerformed(BasicGraphEditor.java:1074)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$400(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2231)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
    at org.hibernate.loader.Loader.list(Loader.java:2120)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:66)
    ... 43 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as request13_21_, ttmp0_.site as site21_, ttmp0_.station_number as station15_21_' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1888)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1808)
    at org.hibernate.loader.Loader.doQuery(Loader.java:697)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2228)
    ... 51 more

我曾经在其他实体中遵循相同的程序,但这是我第一次遇到这个问题。

更新 使用 SpringLearner 提出的解决方案时:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as request13_21_, ttmp0_.site as site21_, ttmp0_.station_number as station15_21_' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.Util.getInstance(Util.java:381) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1031) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1837) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1961) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2543) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1737) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1888) at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208) at org.hibernate.loader.Loader.getResultSet(Loader.java:1808) at org.hibernate.loader.Loader.doQuery(Loader.java:697) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) at org.hibernate.loader.Loader.doList(Loader.java:2228) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125) at org.hibernate.loader.Loader.list(Loader.java:2120) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401) at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361) at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:66) at sau.se.migration.dao.TtmpHome.findByStationName(TtmpHome.java:87) at sau.se.editor.dialog.StationInfo.createIncidentsTable(StationInfo.java:110) at sau.se.editor.dialog.StationInfo.<init>(StationInfo.java:750) at sau.se.editor.dialog.StationInfoAction.actionPerformed(StationInfoAction.java:19) at sau.se.editor.BasicGraphEditor$14.actionPerformed(BasicGraphEditor.java:1074) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.AbstractButton.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source) at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 14:42:38.680 [AWT-EventQueue-0] WARN o.h.util.JDBCExceptionReporter - SQL Error: 1064, SQLState: 42000 14:42:38.680 [AWT-EventQueue-0] ERROR o.h.util.JDBCExceptionReporter - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as request13_21_, ttmp0_.site as site21_, ttmp0_.station_number as station15_21_' at line 1 14:42:38.681 [AWT-EventQueue-0] DEBUG org.hibernate.jdbc.ConnectionManager - aggressively releasing JDBC connection

【问题讨论】:

  • 也许如果您实际提供了正在调用的 SQL(在日志中),那么人们可以识别它的问题,并提出解决方法。

标签: java mysql sql hibernate jpa


【解决方案1】:

更改String queryS = "select t from Ttmp t where t.stationNumber =?1";String queryS = "select t from Ttmp t where t.stationNumber =:stnumber ";

query.setParameter(1, stNumber);

query.setParameter("stnumber", stNumber);

【讨论】:

  • @HoussemBdr 你能发布新的堆栈跟踪吗
  • @HoussemBdr 现在你有不同的错误不是相同的错误。你现在有 sql 语法错误
  • 这与您的原始问题不同。所以我建议您发布另一个问题
  • Hibernate导致的sql语法问题
【解决方案2】:

我想一定是(不带数字的问号):

"select t from Ttmp t where t.stationNumber =?";

【讨论】:

  • @HoussemBdr 我不这么认为,因为位置是从左到右。但我会使用named parameter 而不是positional parameter
  • JPQL 中的编号参数必须具有 OP 提供的编号(请参阅 JPA 规范)。这不是本机 (SQL) 查询。虽然使用命名更有意义
猜你喜欢
  • 2012-06-09
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 2021-09-09
  • 2010-11-22
  • 1970-01-01
  • 2011-12-07
  • 1970-01-01
相关资源
最近更新 更多