Java hibernate could not resolve property

I am trying to make a simple counter statement from a method that works with my other part of the program, but here it gives me an error.

public Long validateSub(String source, String tbl){ Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Query q = session.createQuery("SELECT count(s) from SlaveSubscribers s where s.SOURCENAME = :sourcename AND s.TBL = :tbl"); q.setParameter("sourcename", source); q.setParameter("tbl", tbl); Long result = (Long) q.list().get(0); session.getTransaction().commit(); return result; } 

Error message:

 Exception in thread "Thread-3" org.hibernate.QueryException: could not resolve property: SOURCENAME of: com.datadistributor.main.SlaveSubscribers [SELECT count(s) from com.datadistributor.main.SlaveSubscribers s where s.SOURCENAME = :sourcename AND s.TBL = :tbl] 

I have no idea why this is not working.

+6
source share
2 answers

You do not have a persistent SOURCENAME attribute (field) in the SlaveSubscribers entity. Most likely SOURCENAME is the name of the database column. In HQL, you need to use the field name (case sensitive).

+5
source

Just to clarify the above answer, because I hate anyone to skip it.

Hibernate uses the variable property in the class to query, not the column name in the database.

As a result, if you have a model class as follows:

 private Long studentId; @Id @GeneratedValue @Column(name="studentid") public Long getStudentId() { return studentId; } 

You will need to fulfill the request for studentId not studentid .

+6
source

Source: https://habr.com/ru/post/910731/


All Articles