0

我是hibernate的初学者并且已经阅读了很多,但我被困在这一点上。在我正在实现休眠的 JSF 应用程序中,我有这个在我的数据库中工作的 SQL 查询:

SELECT *
FROM CourseProduct
INNER JOIN Course
ON CourseProduct.number=Course.number
inner join Product
on CourseProduct.product=Product.product;

我正在尝试对我的 JSF 应用程序使用 hibernate 做同样的事情。到目前为止,我想出了:

List results = session.createCriteria(Course.class)
     .setFetchMode("product", FetchMode.JOIN)
     .setFetchMode("number", FetchMode.JOIN)
     .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
     .list();

这是正确的还是完全错误的?另外,我如何访问结果中的字段(如果我什至必须这样做,因为 hibernate 会为我填充类)?我得到的结果似乎只是课程表,即产品中主键的值,而不是表产品中的其他 2 个字段。

编辑

我想我解决了我自己的问题。看起来上面的代码似乎是正确的,我只是没有意识到为了访问 Product 类,我必须从 Course 类的 Set 中访问它!我只是使用了一个迭代器来获取我在课程类中产品集的 get 方法中需要的数据。

4

1 回答 1

0

I guess I solved my own problem. It looks as though the above code is correct, I just didn't realize that in order to access the class Product I had to access it from the Set in the Course class! I just used an iterator to get the data I need in the get method for the set of Products in the Course class.

Update: I really solved the problem. I just got rid of hibernate. The sql query works fine, got my data using a perpared Statement and the result set using the regular old way (java.sql.DriverManager). For some reason the hibernate driver didn't even like using my statement as a native SQL (kept giving me an exception trying to convert an Integer). I googled the problem and they say it's a bug in hibernate!

于 2013-06-11T14:38:42.453 回答