Java org.hibernate.QueryException: 无法解析属性:文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21478178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:54:13  来源:igfitidea点击:

org.hibernate.QueryException: could not resolve property: filename

javahibernateoracle-sqldeveloperhibernate-criteriahibernateexception

提问by Ankit Lamba

I am using Hibernate Criteriato get values from column filenamein my table contaque_recording_log.

我正在使用 HibernateCriteriafilename我的表中的列中获取值contaque_recording_log

But when I'm getting the result, it throws an exception

但是当我得到结果时,它抛出了一个异常

org.hibernate.QueryException: could not resolve property: filename of: com.contaque.hibernateTableMappings.contaque_recording_log

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

My table bean is:

我的餐桌豆是:

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 class from where I'm getting hibernate-session:

我从那里获得休眠会话的 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();
    }
}

My method where I'm getting the data from table:

我从表中获取数据的方法:

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

If I print the criteria.toString(), the O/P is:

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

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

where am I going wrong, how do I get the data from my Table?

我哪里出错了,如何从我的表中获取数据?

采纳答案by Xavi López

Hibernate queries are case sensitive with property names (because they end up relying on getter/setter methods on the @Entity).

Hibernate 查询对属性名称区分大小写(因为它们最终依赖于 上的 getter/setter 方法@Entity)。

Make sure you refer to the property as fileNamein the Criteria query, not filename.

确保您fileName在 Criteria 查询中引用该属性,而不是filename.

Specifically, Hibernate will call the getter method of the filenameproperty when executing that Criteria query, so it will look for a method called getFilename(). But the property is called FileNameand the getter getFileName().

具体来说,Hibernate 将filename在执行该 Criteria 查询时调用属性的 getter 方法,因此它将查找名为 的方法getFilename()。但是该属性被称为FileNamegetter getFileName()

So, change the projection like so:

所以,像这样改变投影:

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