问题描述
在hibernate中,我似乎遇到了一些困难。 选择*从产品p INNER JOIN仓库w ON p.wid = w。产品表:
> id |名称| wid |价格|库存.....
仓库表:
id |名称|城市| lat | long .....
连接结果:
id |名称| wid |价格|股票| id |名称|城市| lat | long .....
当我运行查询时。
Session.createSQLQuery(this.query)
.addEntity(p,Product.class)
.addEntity(w, Warehouse.class).LIST();
因此,对于每个结果,我都会得到一个包含 Product对象的对象
和一个仓库对象
。
这是预期的。问题在于hibernate将产品的ID和名称分配给仓库对象ID和名称属性。它就像创建仓库项目时连接结果中的前两列一样。产品对象始终包含正确的数据。
任何有关如何解决此问题的建议,都会非常感谢表示正确仓库数据的id和name列。 / p>
预先感谢。
解决方案使用{}表单来避免列名重复出现问题:
SELECT {p。*},{w。*} FROM product p INNER JOIN仓库w ON p.wid = w.id
来自,第18.1.4节。返回多个实体:
sess.createSQLQuery(SELECT c。*,m。* FROM CATS c,CATS m WHERE c.MOTHER_ID = c.ID)
.addEntity(cat,Cat.class)
。 addEntity(mother,Cat.class)
I seem to be having some difficulty with a query in hibernate. I am performing an inner join on two tables.
SELECT * FROM product p INNER JOIN warehouse w ON p.wid = w.id
Product Table:
id | name | wid | price | stock .....
Warehouse Table:
id | name | city | lat | long .....
The join result:
id | name | wid | price | stock | id | name | city | lat | long .....
When I run the query..
Session.createSQLQuery(this.query)
.addEntity("p", Product.class)
.addEntity("w", Warehouse.class).list();
So for every result I get an object containing a Product object
and a Warehouse object
.
This is expected. The issue is hibernate assigns the id and name of the product to the warehouse objects id and name property. Its as if the first two columns in the join result are over riding when it comes to creating the Warehouse project. The Product object always contains the correct data.
Any suggestion on finding a way around this issue so the id and name columns representing the correct Warehouse data would be much appreciated.
Thanks in advance.
解决方案 Use the {} form to avoid problems with column name duplication:
SELECT {p.*}, {w.*} FROM product p INNER JOIN warehouse w ON p.wid = w.id
From Hibernate Reference Documentation, section 18.1.4. Returning multiple entities:
sess.createSQLQuery("SELECT c.*, m.* FROM CATS c, CATS m WHERE c.MOTHER_ID = c.ID")
.addEntity("cat", Cat.class)
.addEntity("mother", Cat.class)
sess.createSQLQuery("SELECT {cat.*}, {mother.*} FROM CATS c, CATS m WHERE c.MOTHER_ID = c.ID")
.addEntity("cat", Cat.class)
.addEntity("mother", Cat.class)
这篇关于使用内部连接使用hibernate返回多个对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!