java hibernate 无法解析属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9707141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java hibernate could not resolve property
提问by user1217609
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.
我正在尝试从一个适用于我程序的其他部分的方法中执行一个简单的 select count 语句,但在这里它给了我错误。
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
我不知道为什么这不起作用
采纳答案by Mikko Maunu
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).
您在 SlaveSubscribers 实体中没有持久属性(字段)SOURCENAME。最有可能的 SOURCENAME 是数据库列的名称。在 HQL 中,您需要使用字段名称(区分大小写)。
回答by Aelexe
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.
Hibernate 使用类中的变量属性进行查询,而不是数据库中的列名。
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 studentIdnot studentid.
你将不得不做一个查询studentId不studentid。