21

我使用Hibernate Criteria從我的表contaque_recording_log的表filename中獲取值。org.hibernate.QueryException:無法解析屬性:文件名

但是,當我得到的結果,它拋出一個異常

org.hibernate.QueryException:無法解析屬性:文件名:com.contaque.hibernateTableMappings.contaque_recording_log

我的表bean是:

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


@Entity 
@Table(name="contaque_recording_log") 
public class contaque_recording_log implements java.io.Serializable{ 
private static final long serialVersionUID = 1111222233334404L; 
@Id 
@Column(name="logId", insertable=true, updatable=true, unique=false) 
private Integer logId; 

@Column(name="userName", insertable=true, updatable=true, unique=false) 
private String userName; 

@Column(name="ext", insertable=true, updatable=true, unique=false) 
private String ext; 

@Column(name="phoneNumber", insertable=true, updatable=true, unique=false) 
private String phoneNumber; 

@Column(name="callerId", insertable=true, updatable=true, unique=false) 
private String callerId; 

@Column(name="fileName", insertable=true, updatable=true, unique=false) 
private String fileName; 


@Column(name="campName", insertable=true, updatable=true, unique=false) 
private String campName; 

@Temporal(javax.persistence.TemporalType.TIMESTAMP) 
@Column(name="eventDate", insertable=true, updatable=true, unique=false) 
private Date eventDate; 

@Column(name="disposition", insertable=true, updatable=true, unique=false) 
private String disposition; 

@Column(name="duration", insertable=true, updatable=true, unique=false) 
private String duration; 

@Column(name="calltype", insertable=true, updatable=true, unique=false) 
private String calltype; 

public Date getEventDate() { 
    return eventDate; 
} 

public void setEventDate(Date eventDate) { 
    this.eventDate = eventDate; 
} 

public String getCallerId() { 
    return callerId; 
} 

public void setCallerId(String callerId) { 
    this.callerId = callerId; 
} 

public String getExt() { 
    return ext; 
} 

public void setExt(String ext) { 
    this.ext = ext; 
} 

public String getFileName() { 
    return fileName; 
} 

public void setFileName(String fileName) { 
    this.fileName = fileName; 
} 

public String getPhoneNumber() { 
    return phoneNumber; 
} 

public void setPhoneNumber(String phoneNumber) { 
    this.phoneNumber = phoneNumber; 
} 

public String getUserName() { 
    return userName; 
} 

public void setUserName(String userName) { 
    this.userName = userName; 
} 

public Integer getLogId() { 
    return logId; 
} 

public void setLogId(Integer logId) { 
    this.logId = logId; 
} 

public String getCampName() { 
    return campName; 
} 

public void setCampName(String campName) { 
    this.campName = campName; 
} 

public String getDisposition() { 
    return disposition; 
} 

public void setDisposition(String disposition) { 
    this.disposition = disposition; 
} 

public String getDuration() { 
    return duration; 
} 

public void setDuration(String duration) { 
    this.duration = duration; 
} 

public String getCalltype() { 
    return calltype; 
} 

public void setCalltype(String calltype) { 
    this.calltype = calltype; 
} 

}

從那裏我得到休眠會話MY HibernateUtil類:

public enum HibernateUtilSpice { 
INSTANCE; 
public static SessionFactory sessionFactory = null; 

private synchronized SessionFactory getSessionFactory(){ 

    if(sessionFactory == null){ 
     Configuration config = new Configuration(); 
     config.addAnnotatedClass(contaque_recording_log.class); 
     config.addAnnotatedClass(contaque_servers.class); 
     config.configure(); 

     //get the properties from Hibernate configuration file 
     Properties configProperties = config.getProperties(); 
     ServiceRegistryBuilder serviceRegisteryBuilder = new ServiceRegistryBuilder(); 
     ServiceRegistry serviceRegistry = serviceRegisteryBuilder.applySettings(configProperties).buildServiceRegistry(); 
     sessionFactory = config.buildSessionFactory(serviceRegistry); 
    } 
    return sessionFactory; 
} 

public Session getSession(){ 
    return getSessionFactory().openSession(); 
} 
} 

我的方法在那裏我從表中獲取數據:

public String getFileName() { 

    try{ 
     hibernateSession = HibernateUtilSpice.INSTANCE.getSession(); 
     Criteria criteria = hibernateSession.createCriteria(contaque_recording_log.class); 
     criteria.add(Restrictions.eq("campname", "spice")); 
     criteria.add(Restrictions.eq("disposition", "WN")); 
     criteria.setProjection(Projections.property("filename")); 
     List list = criteria.list(); 
     for (Object object : list) { 
      System.out.println("List obj: " + object); 
     } 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } finally { 
     hibernateSession.flush(); 
     hibernateSession.close(); 
    } 
    return filename; 
} 

如果我打印criteria.toString()中,O/P是:

CriteriaImpl(com.contaque.hibernateTableMappings.contaque_recording_log:this[][campname=spice, disposition=WN]filename) 

我在哪裏出錯了,我如何從我的表中獲取數據?

回答

51

Hibernate查詢區分屬性名稱(因爲它們最終依靠@Entity上的getter/setter方法)。

請確保您在標準查詢中引用屬性fileName,而不是filename

具體而言,Hibernate在執行該條件查詢時將調用filename屬性的getter方法,因此它將查找名爲getFilename()的方法。但該物業被稱爲FileName和獲取者getFileName()

所以,改變投影像這樣:

criteria.setProjection(Projections.property("fileName")); 
+4

有這個問題。我的表有一個名爲'fk_mycolumn'的列,我使用'name =「fk_mycolumn」'映射它,但在我的DAO中,我試圖查詢它'fk_myColumn',大寫字母「C」。 – George

+1

我有同樣的問題和問題是既吸氣劑和二傳手丟失! – Cjo

相關問題