我有两个表 website_availability 和 status_codes。它们之间有外键关系。status_codes 是父表。我正在使用 hibernate。加入后我需要这些表中的值的“列表”。我正在遵循此代码。
List<WebsiteAvailability>list=new ArrayList<WebsiteAvailability>
String selquery="select w.statusCode,w.updateTime,w.statusCodes.statusCodeValue from WebsiteAvailability w,StatusCodes s where w.statusCodes.statusCode=s.statusCode and w.url=?";
//here hibernate generates the POJO classes and these are having foriegn key relation so WebsiteAvailability is having private StatusCodes statusCodes.So I am accessing statuscodevalue of statuscodes table using w.statusCodes.statusCodeValue.
PreparedStatement ps=con.prepareStatement(selquery);
ps.setString(1,selUrl);
rs=ps.executeQuery();
while(rs.next())
{
list.add(new WebsiteAvailability(rs.getString("statusCode"),rs.getTimestamp("updateTime"),rs.getString("statusCodeValue")));
}
return list;
}
首先,我可以将结果集与 hibernate 一起使用。有没有其他选择。因为我正在使用?占位符 我应该使用 setString() 的准备语句。并执行Query() 来获取列表。我需要值列表,如何获取。正在获取空列表。错误是什么?
org.hibernate.QueryException:无法解析-----WebsiteAvailability---的属性statusCode
在 hibernate 映射文件中,我检查了大小写敏感性。仍然出现无法解决属性异常
最佳答案
您正在尝试使用 JDBC 语句将 HQL 查询作为 SQL 查询来处理 Hibernate 实体。这没有道理。 HQL 查询由 Hibernate Session 执行。不是通过 JDBC。如果您使用 Hibernate,则不再需要 JDBC(除非在某些极端情况下您需要原始 JDBC 性能,例如批处理)。
阅读有关 HQL query execution 的文档。您还必须修正您的查询,因为它看起来不正确。它包含 w.statusCode
和 w.statusCodes
。它还使用相等语句进行连接并从两个实体中进行选择,而不是简单地使用隐式或显式连接。 the documentation 中也对此进行了解释。 .
关于java - 使用 hibernate 从数据库访问值时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16689346/