[java] org.hibernate.QueryException: could not resolve property: filename

I am using Hibernate Criteria to get values from column filename in my table 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

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:

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:

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?

The answer is


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

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

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

So, change the projection like so:

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

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to hibernate

Hibernate Error executing DDL via JDBC Statement How does spring.jpa.hibernate.ddl-auto property exactly work in Spring? Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed JPA Hibernate Persistence exception [PersistenceUnit: default] Unable to build Hibernate SessionFactory Disable all Database related auto configuration in Spring Boot Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] HikariCP - connection is not available Hibernate-sequence doesn't exist How to find distinct rows with field in list using JPA and Spring? Spring Data JPA and Exists query

Examples related to oracle-sqldeveloper

how to modify the size of a column How to create a blank/empty column with SELECT query in oracle? Extract number from string with Oracle function How to run .sql file in Oracle SQL developer tool to import database? SQL Developer with JDK (64 bit) cannot find JVM How do I view the Explain Plan in Oracle Sql developer? NLS_NUMERIC_CHARACTERS setting for decimal copy from one database to another using oracle sql developer - connection failed Oracle SqlDeveloper JDK path Oracle SQL Developer: Failure - Test failed: The Network Adapter could not establish the connection?

Examples related to hibernate-criteria

org.hibernate.QueryException: could not resolve property: filename Hibernate Criteria Query to get specific columns Hibernate Group by Criteria Object Hibernate Criteria Restrictions AND / OR combination JPA and Hibernate - Criteria vs. JPQL or HQL

Examples related to hibernateexception

org.hibernate.QueryException: could not resolve property: filename Hibernate: How to fix "identifier of an instance altered from X to Y"?