Question

I'm confused with the criteria Restriction field naming on Hibernate when we apply a condition. I have a database table called "User" and it contains a field called "status_id". I have used the eclipse hibernate tools to generate the POJOs and it created the User class and the property name is "statusId" within the class, which does not have the "_" sign.

Now I want to know in the following code, which name I have to use for the Restriction criteria.

Criteria criteria = session.createCriteria(User.class)
                 .add(Restrictions.eq(<column to check>, new Int(1)));

My confusion is whether to use the "status_id" or the "statusId" in the column to check for the criteria. I searched for the explanation and couldn't find similar problem. Can someone help me out in this issue?

Was it helpful?

Solution 2

You use the bean property name. If your column is status_id and you have int statusId in your class, the property name for the criteria is the name of the bean property: statusId

This generates SQL and will figure out your query needs to be status_id

http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/criterion/Restrictions.html

OTHER TIPS

@Column(name = "DEPT_NAME", length = 100)

private String DEPTNAME;

Here DEPT_NAME is supposed to be used when using native sql

DEPTNAME is used when using HQL and during criteria.

Use statusId. You always use field names from the mapped POJOS and not the table column names unless you are trying to write a SQL.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top