简体   繁体   中英

Hibernate Criteria Restriction field naming confusion

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?

@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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM