2016-03-01 44 views
0

將項目切換到maven,使用最新版本的Hibernate。我被困在試圖解決這個問題,確切的代碼之前工作。在我看來,不知何故休眠無法找到映射文件或什麼?我在資源文件夾中擁有所有的xml文件。使用Intellij。org.hibernate.QueryException:意外的令牌:如

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) 

TicketDAO:

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

門票:

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", [email protected](name="Serial", unique=true, nullable=false, length=12)), 
    @AttributeOverride(name="gameTemplatesPartNum", [email protected](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; 
} 
} 

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

}

+0

你是否嘗試過't',從任何地方移除't'? –

+2

或者直接刪除'AS'並重試 –

+1

Hibernate的哪個版本?請添加'HibernateUtil'代碼。 –

回答

0

不能配置Hibernate 5這種方式(這是僅適用於休眠4)

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); 

Configuration失去所有的映射信息,當你做buildSessionFactory(serviceRegistry)

只是用它來建立一個SessionFactory

sessionFactory = new Configuration().configure().buildSessionFactory(); 

Hibernate 5 :- org.hibernate.MappingException: Unknown entity

而你需要提交事務,關閉會話。在其他情況下也不要忘記回滾。

+0

謝謝,爲我提供了足夠的信息來解決它。查閱5.1文檔,發現幾乎相同的代碼,並且與以前一樣工作。 – Switcher

+0

@ Switcher05不客氣。 Hibernate有一個不太好的文檔。例如,一個不適用於Hibernate 5的例子[1.1.6。啓動和幫助](http://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch01.html#tutorial-firstapp-helpers) –

0

它基本上取決於數據庫。一些數據庫支持作爲關鍵字和一些不會。從查詢,以便去除然後嘗試

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();