切换到maven后,Hibernate查询停止工作

时间:2015-12-14 06:36:48

标签: java hibernate maven

我想要一种更简单的方法来管理库,所以我决定将maven添加到我的项目中。基本上将代码复制到文件夹中,将所有cfg和hbm文件添加到resources文件夹中。我的项目在maven之前按照预期使用相同的代码。我甚至从Hibernate 5.0恢复到4.2以尝试解决问题。它似乎是一个映射问题,我在启动时遇到以下错误:

01:21:49.184 [AWT-EventQueue-0] ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 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 'from where (inplay=true )and(bin<30 ) order by  bin ASC' at line 1
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2116)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1899)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1875)
at org.hibernate.loader.Loader.doQuery(Loader.java:919)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doList(Loader.java:2611)
at org.hibernate.loader.Loader.doList(Loader.java:2594)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423)
at org.hibernate.loader.Loader.list(Loader.java:2418)
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.list(QueryTranslatorImpl.java:957)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:226)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1268)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:87)
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:2029)
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)

代码:

public List<Tickets> get30Tickets(){
    List ts = new ArrayList<Tickets>();
    session = HibernateUtil.getSessionFactory().openSession();
    try{
        trns = session.beginTransaction();
        ts = session.createQuery("from Tickets as t where t.inplay = true and t.bin < 30 ORDER BY t.bin ASC").list();

    } catch (RuntimeException e){
        e.printStackTrace();
    } finally {
        releaseResources();
    }
    return ts;
}

票证实体:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 11, 2015 7:57:00 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
    <class name="entity.Tickets" table="tickets" catalog="fire_tickets" optimistic-lock="version">
        <composite-id name="id" class="entity.TicketsId">
            <key-property name="serial" type="string">
                <column name="Serial" length="12" />
            </key-property>
            <key-property name="gameTemplatesPartNum" type="string">
                <column name="game_templates_part_num" length="12" />
            </key-property>
        </composite-id>
        <property name="datePlaced" type="date">
            <column name="Date_placed" length="10" />
        </property>
        <property name="dateRemoved" type="date">
            <column name="Date_removed" length="10" />
        </property>
        <property name="unsoldAmt" type="java.lang.Integer">
            <column name="Unsold_amt" />
        </property>
        <property name="actualGross" type="java.lang.Integer">
            <column name="Actual_gross" />
        </property>
        <property name="actualPrizes" type="java.lang.Integer">
            <column name="Actual_prizes" />
        </property>
        <property name="actualNet" type="java.lang.Integer">
            <column name="Actual_net" />
        </property>
        <property name="bin" type="java.lang.Integer">
            <column name="Bin" />
        </property>
        <property name="inplay" type="java.lang.Boolean">
            <column name="Inplay" />
        </property>
        <property name="closed" type="java.lang.Integer">
            <column name="Closed" />
        </property>
        <property name="unsoldTickets" type="java.lang.Integer">
            <column name="Unsold_tickets" />
        </property>
        <property name="lastSaleRem" type="java.lang.Byte">
            <column name="Last_sale_rem" />
        </property>
        <property name="prizeRem1" type="java.lang.Integer">
            <column name="Prize_rem1" />
        </property>
        <property name="prizeRem2" type="java.lang.Integer">
            <column name="Prize_rem2" />
        </property>
        <property name="prizeRem3" type="java.lang.Integer">
            <column name="Prize_rem3" />
        </property>
        <property name="prizeRem4" type="java.lang.Integer">
            <column name="Prize_rem4" />
        </property>
        <property name="prizeRem5" type="java.lang.Integer">
            <column name="Prize_rem5" />
        </property>
        <property name="prizeRem6" type="java.lang.Integer">
            <column name="Prize_rem6" />
        </property>
        <property name="prizeRem7" type="java.lang.Integer">
            <column name="Prize_rem7" />
        </property>
        <property name="prizeRem8" type="java.lang.Integer">
            <column name="Prize_rem8" />
        </property>
        <property name="prizeRem9" type="java.lang.Integer">
            <column name="Prize_rem9" />
        </property>
        <property name="prizeRem10" type="java.lang.Integer">
            <column name="Prize_rem10" />
        </property>
        <property name="prizeRem11" type="java.lang.Integer">
            <column name="Prize_rem11" />
        </property>
        <property name="prizeRem12" type="java.lang.Integer">
            <column name="Prize_rem12" />
        </property>
        <property name="prizeRem13" type="java.lang.Integer">
            <column name="Prize_rem13" />
        </property>
        <property name="prizeRem14" type="java.lang.Integer">
            <column name="Prize_rem14" />
        </property>
        <property name="prizeRem15" type="java.lang.Integer">
            <column name="Prize_rem15" />
        </property>
        <property name="datePurch" type="date">
            <column name="date_purch" length="10" />
        </property>
        <property name="invoiceNum" type="string">
            <column name="invoice_num" length="10" />
        </property>
        <property name="type" type="java.lang.Integer">
            <column name="type" />
        </property>
    </class>
</hibernate-mapping>

的hibernate.cfg.xml

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://192.168.1.2:3306/fire_tickets</property>
    <property name="hibernate.connection.username">-</property>
    <property name="hibernate.connection.password">-</property>
    <property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>

    <property name="show_sql">false</property>
    <property name="hibernate.jdbc.batch_size">20</property>
    <mapping resource="entity/Users.hbm.xml"/>
    <mapping resource="entity/SaleSessions.hbm.xml"/>
    <mapping resource="entity/GameTemplates.hbm.xml"/>
    <mapping resource="entity/MfgId.hbm.xml"/>
    <mapping resource="entity/Customers.hbm.xml"/>
    <mapping resource="entity/BigWinners.hbm.xml"/>
    <mapping resource="entity/Locations.hbm.xml"/>
    <mapping resource="entity/Sessions.hbm.xml"/>
    <mapping resource="entity/TillTape.hbm.xml"/>
    <mapping resource="entity/DistsId.hbm.xml"/>
    <mapping resource="entity/Tickets.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

Tickets.java

    package entity;
// Generated Oct 11, 2015 7:56:58 PM by Hibernate Tools 4.3.1


import javax.persistence.*;
import java.util.Date;

/**
 * Tickets generated by hbm2java
 */
@Entity
@Table(name="tickets"
    ,catalog="fire_tickets"
    , uniqueConstraints = @UniqueConstraint(columnNames="Serial") 
)
public class Tickets  implements java.io.Serializable {


     private TicketsId id;
     private Date datePlaced;
     private Date dateRemoved;
     private Integer unsoldAmt;
     private Integer actualGross;
     private Integer actualPrizes;
     private Integer actualNet;
     private Integer bin;
     private Boolean inplay;
     private Integer closed;
     private Integer unsoldTickets;
     private Byte lastSaleRem;
     private Integer prizeRem1;
     private Integer prizeRem2;
     private Integer prizeRem3;
     private Integer prizeRem4;
     private Integer prizeRem5;
     private Integer prizeRem6;
     private Integer prizeRem7;
     private Integer prizeRem8;
     private Integer prizeRem9;
     private Integer prizeRem10;
     private Integer prizeRem11;
     private Integer prizeRem12;
     private Integer prizeRem13;
     private Integer prizeRem14;
     private Integer prizeRem15;
     private Date datePurch;
     private String invoiceNum;
     private Integer type;

    public Tickets() {
    }


    public Tickets(TicketsId id) {
        this.id = id;
    }
    public Tickets(TicketsId id, Date datePlaced, Date dateRemoved, Integer unsoldAmt, Integer actualGross, Integer actualPrizes, Integer actualNet, Integer bin, Boolean inplay, Integer closed, Integer unsoldTickets, Byte lastSaleRem, Integer prizeRem1, Integer prizeRem2, Integer prizeRem3, Integer prizeRem4, Integer prizeRem5, Integer prizeRem6, Integer prizeRem7, Integer prizeRem8, Integer prizeRem9, Integer prizeRem10, Integer prizeRem11, Integer prizeRem12, Integer prizeRem13, Integer prizeRem14, Integer prizeRem15, Date datePurch, String invoiceNum, Integer type) {
       this.id = id;
       this.datePlaced = datePlaced;
       this.dateRemoved = dateRemoved;
       this.unsoldAmt = unsoldAmt;
       this.actualGross = actualGross;
       this.actualPrizes = actualPrizes;
       this.actualNet = actualNet;
       this.bin = bin;
       this.inplay = inplay;
       this.closed = closed;
       this.unsoldTickets = unsoldTickets;
       this.lastSaleRem = lastSaleRem;
       this.prizeRem1 = prizeRem1;
       this.prizeRem2 = prizeRem2;
       this.prizeRem3 = prizeRem3;
       this.prizeRem4 = prizeRem4;
       this.prizeRem5 = prizeRem5;
       this.prizeRem6 = prizeRem6;
       this.prizeRem7 = prizeRem7;
       this.prizeRem8 = prizeRem8;
       this.prizeRem9 = prizeRem9;
       this.prizeRem10 = prizeRem10;
       this.prizeRem11 = prizeRem11;
       this.prizeRem12 = prizeRem12;
       this.prizeRem13 = prizeRem13;
       this.prizeRem14 = prizeRem14;
       this.prizeRem15 = prizeRem15;
       this.datePurch = datePurch;
       this.invoiceNum = invoiceNum;
       this.type = type;
    }

     @EmbeddedId


    @AttributeOverrides( {
        @AttributeOverride(name="serial", column=@Column(name="Serial", unique=true, nullable=false, length=12) ), 
        @AttributeOverride(name="gameTemplatesPartNum", column=@Column(name="game_templates_part_num", nullable=false, length=12) ) } )
    public TicketsId getId() {
        return this.id;
    }

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

    @Temporal(TemporalType.DATE)
    @Column(name="Date_placed", length=10)
    public Date getDatePlaced() {
        return this.datePlaced;
    }

    public void setDatePlaced(Date datePlaced) {
        this.datePlaced = datePlaced;
    }

    @Temporal(TemporalType.DATE)
    @Column(name="Date_removed", length=10)
    public Date getDateRemoved() {
        return this.dateRemoved;
    }

    public void setDateRemoved(Date dateRemoved) {
        this.dateRemoved = dateRemoved;
    }


    @Column(name="Unsold_amt")
    public Integer getUnsoldAmt() {
        return this.unsoldAmt;
    }

    public void setUnsoldAmt(Integer unsoldAmt) {
        this.unsoldAmt = unsoldAmt;
    }


    @Column(name="Actual_gross")
    public Integer getActualGross() {
        return this.actualGross;
    }

    public void setActualGross(Integer actualGross) {
        this.actualGross = actualGross;
    }


    @Column(name="Actual_prizes")
    public Integer getActualPrizes() {
        return this.actualPrizes;
    }

    public void setActualPrizes(Integer actualPrizes) {
        this.actualPrizes = actualPrizes;
    }


    @Column(name="Actual_net")
    public Integer getActualNet() {
        return this.actualNet;
    }

    public void setActualNet(Integer actualNet) {
        this.actualNet = actualNet;
    }


    @Column(name="Bin")
    public Integer getBin() {
        return this.bin;
    }

    public void setBin(Integer bin) {
        this.bin = bin;
    }


    @Column(name="Inplay")
    public Boolean getInplay() {
        return this.inplay;
    }

    public void setInplay(Boolean inplay) {
        this.inplay = inplay;
    }


    @Column(name="Closed")
    public Integer getClosed() {
        return this.closed;
    }

    public void setClosed(Integer closed) {
        this.closed = closed;
    }


    @Column(name="Unsold_tickets")
    public Integer getUnsoldTickets() {
        return this.unsoldTickets;
    }

    public void setUnsoldTickets(Integer unsoldTickets) {
        this.unsoldTickets = unsoldTickets;
    }


    @Column(name="Last_sale_rem")
    public Byte getLastSaleRem() {
        return this.lastSaleRem;
    }

    public void setLastSaleRem(Byte lastSaleRem) {
        this.lastSaleRem = lastSaleRem;
    }


    @Column(name="Prize_rem1")
    public Integer getPrizeRem1() {
        return this.prizeRem1;
    }

    public void setPrizeRem1(Integer prizeRem1) {
        this.prizeRem1 = prizeRem1;
    }


    @Column(name="Prize_rem2")
    public Integer getPrizeRem2() {
        return this.prizeRem2;
    }

    public void setPrizeRem2(Integer prizeRem2) {
        this.prizeRem2 = prizeRem2;
    }


    @Column(name="Prize_rem3")
    public Integer getPrizeRem3() {
        return this.prizeRem3;
    }

    public void setPrizeRem3(Integer prizeRem3) {
        this.prizeRem3 = prizeRem3;
    }


    @Column(name="Prize_rem4")
    public Integer getPrizeRem4() {
        return this.prizeRem4;
    }

    public void setPrizeRem4(Integer prizeRem4) {
        this.prizeRem4 = prizeRem4;
    }


    @Column(name="Prize_rem5")
    public Integer getPrizeRem5() {
        return this.prizeRem5;
    }

    public void setPrizeRem5(Integer prizeRem5) {
        this.prizeRem5 = prizeRem5;
    }


    @Column(name="Prize_rem6")
    public Integer getPrizeRem6() {
        return this.prizeRem6;
    }

    public void setPrizeRem6(Integer prizeRem6) {
        this.prizeRem6 = prizeRem6;
    }


    @Column(name="Prize_rem7")
    public Integer getPrizeRem7() {
        return this.prizeRem7;
    }

    public void setPrizeRem7(Integer prizeRem7) {
        this.prizeRem7 = prizeRem7;
    }


    @Column(name="Prize_rem8")
    public Integer getPrizeRem8() {
        return this.prizeRem8;
    }

    public void setPrizeRem8(Integer prizeRem8) {
        this.prizeRem8 = prizeRem8;
    }


    @Column(name="Prize_rem9")
    public Integer getPrizeRem9() {
        return this.prizeRem9;
    }

    public void setPrizeRem9(Integer prizeRem9) {
        this.prizeRem9 = prizeRem9;
    }


    @Column(name="Prize_rem10")
    public Integer getPrizeRem10() {
        return this.prizeRem10;
    }

    public void setPrizeRem10(Integer prizeRem10) {
        this.prizeRem10 = prizeRem10;
    }


    @Column(name="Prize_rem11")
    public Integer getPrizeRem11() {
        return this.prizeRem11;
    }

    public void setPrizeRem11(Integer prizeRem11) {
        this.prizeRem11 = prizeRem11;
    }


    @Column(name="Prize_rem12")
    public Integer getPrizeRem12() {
        return this.prizeRem12;
    }

    public void setPrizeRem12(Integer prizeRem12) {
        this.prizeRem12 = prizeRem12;
    }


    @Column(name="Prize_rem13")
    public Integer getPrizeRem13() {
        return this.prizeRem13;
    }

    public void setPrizeRem13(Integer prizeRem13) {
        this.prizeRem13 = prizeRem13;
    }


    @Column(name="Prize_rem14")
    public Integer getPrizeRem14() {
        return this.prizeRem14;
    }

    public void setPrizeRem14(Integer prizeRem14) {
        this.prizeRem14 = prizeRem14;
    }


    @Column(name="Prize_rem15")
    public Integer getPrizeRem15() {
        return this.prizeRem15;
    }

    public void setPrizeRem15(Integer prizeRem15) {
        this.prizeRem15 = prizeRem15;
    }

    @Temporal(TemporalType.DATE)
    @Column(name="date_purch", length=10)
    public Date getDatePurch() {
        return this.datePurch;
    }

    public void setDatePurch(Date datePurch) {
        this.datePurch = datePurch;
    }


    @Column(name="invoice_num", length=10)
    public String getInvoiceNum() {
        return this.invoiceNum;
    }

    public void setInvoiceNum(String invoiceNum) {
        this.invoiceNum = invoiceNum;
    }


    @Column(name="type")
    public Integer getType() {
        return this.type;
    }

    public void setType(Integer type) {
        this.type = type;
    }




}

1 个答案:

答案 0 :(得分:0)

存在语法错误的查询在日志中看起来与在代码中看起来不同。

有可能因为你切换到maven你的项目没有编译,当你启动服务器时,Eclipse(我假设)只是部署最后一个成功构建“.class”

检查你的pom(esclamation mark)是否没有错误并尝试清理所有内容。