简体   繁体   English

Hibernate Criteria Restriction字段命名混淆

[英]Hibernate Criteria Restriction field naming confusion

I'm confused with the criteria Restriction field naming on Hibernate when we apply a condition. 当我们应用条件时,我对Hibernate上的限制字段命名标准感到困惑。 I have a database table called "User" and it contains a field called " status_id ". 我有一个名为“User”的数据库表,它包含一个名为“ 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. 我已经使用eclipse hibernate工具生成POJO并且它创建了User类,并且类中的属性名称是“ statusId ”,它没有“_”符号。

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. 我的困惑在于是否使用列中的“ status_id ”或“ statusId ”来检查标准。 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 在使用本机sql时,应该使用DEPT_NAME

DEPTNAME is used when using HQL and during criteria . 在使用HQL时标准期间使用DEPTNAME

Use statusId. 使用statusId。 You always use field names from the mapped POJOS and not the table column names unless you are trying to write a SQL. 除非您尝试编写SQL,否则始终使用映射的POJOS中的字段名称而不是表列名称。

You use the bean property name. 您使用bean属性名称。 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 如果您的列是status_id并且您的类中有int statusId ,则条件的属性名称是bean属性的名称: statusId

This generates SQL and will figure out your query needs to be status_id 这会生成SQL并将确定您的查询需要是status_id

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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