使用内部连接使用hibernate返回多个对象类型 [英] Returning multiple object types using hibernate using an inner join

查看:148
本文介绍了使用内部连接使用hibernate返回多个对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 

来自 Hibernate参考文档,第18.1.4节。返回多个实体:


到目前为止,假设结果集列名与
中指定的列名相同映射文件。对于连接多个表的SQL查询,这可能是
问题,因为相同的
列名可能出现在多个表中。



列别名在以下查询中需要注入(其中大部分
可能会失败):



  sess.createSQLQuery(SELECT c。*,m。* FROM CATS c,CATS m WHERE c.MOTHER_ID = c.ID)
.addEntity(cat,Cat.class)
。 addEntity(mother,Cat.class)




每行返回两个Cat实例:一个cat和
它的母亲。但是,查询会失败,因为名称冲突
;这些实例被映射到相同的列名称。另外,在
上有些数据库的返回的列别名很可能是
格式的c.ID,c.NAME等,它们不等于指定的列
在映射中(ID和NAME)。



以下表单不容易出现列名重复:



  sess.createSQLQuery(SELECT {cat。*},{mother。*} FROM CATS c,CATS m WHERE c.MOTHER_ID = c .ID)
.addEntity(cat,Cat.class)
.addEntity(mother,Cat.class)




这个查询指定了:



SQL查询字符串,用Hibernate的占位符来注入列
别名查询返回的实体 {cat。*}
{mother。*} 符号是所有属性的缩写。



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:

Until now, the result set column names are assumed to be the same as the column names specified in the mapping document. This can be problematic for SQL queries that join multiple tables, since the same column names can appear in more than one table.

Column alias injection is needed in the following query (which most likely will fail):

sess.createSQLQuery("SELECT c.*, m.*  FROM CATS c, CATS m WHERE c.MOTHER_ID = c.ID")
 .addEntity("cat", Cat.class)
 .addEntity("mother", Cat.class)

The query was intended to return two Cat instances per row: a cat and its mother. The query will, however, fail because there is a conflict of names; the instances are mapped to the same column names. Also, on some databases the returned column aliases will most likely be on the form "c.ID", "c.NAME", etc. which are not equal to the columns specified in the mappings ("ID" and "NAME").

The following form is not vulnerable to column name duplication:

sess.createSQLQuery("SELECT {cat.*}, {mother.*}  FROM CATS c, CATS m WHERE c.MOTHER_ID = c.ID")
 .addEntity("cat", Cat.class)
 .addEntity("mother", Cat.class)

This query specified:

the SQL query string, with placeholders for Hibernate to inject column aliases the entities returned by the query The {cat.*} and {mother.*} notation used above is a shorthand for "all properties".

这篇关于使用内部连接使用hibernate返回多个对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆