12

I am trying to do a simple select count statement from a method which works on my other part of program but here it gives me 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; } 

The 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 does not work

1
  • Please post an extract your class SlaveSubscribers (member declaration and getter/setter for sourcename) and the hibernate mapping file for this class (if exists). The error must be there. Commented Mar 15, 2012 at 7:54

2 Answers 2

18

Just to clarify the above answer, because I would hate for anyone to miss it.

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

As a result if you have a model class like this:

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

You will have to do a query on studentId not studentid.

Sign up to request clarification or add additional context in comments.

1 Comment

Great note, this helped me a lot. I'd like to add that the case of the variable as it is defined in the class does not matter. For example, you could have also defined the Long variable in the class as studentid instead of studentId, and as long as the getter had the I capitalized, you would still need to capitalize the I in your query, as well.
8

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.