简体   繁体   中英

could not execute query in Hibernate with MySQL

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

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

When I tried to get data in some part of my code, I got that error:

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

I used to follow the same procedure in other entities but it is the first time I got that problem.

UPDATE When using solution proposed by 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 ohutil.JDBCExceptionReporter - SQL Error: 1064, SQLState: 42000 14:42:38.680 [AWT-EventQueue-0] ERROR ohutil.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

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

and

query.setParameter(1, stNumber);

to query.setParameter("stnumber", stNumber);

我认为它一定是(没有数字的问号):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM