如何指定休眠连接映射?
Say I have the below two databases:
说我有以下两个数据库:
Employee
|Name|Age|Sex|
Table
|Employee_Name|Location|Brand|
I wish to execute following form of query:
我希望执行以下形式的查询:
select * from table,employee where table.employee_name=employee.name;
Now, I have specified the hibernate .hbm.xml files for both these tables. But in this case, the return value from the query is a join. Hence, how can I specify the data structure for it? Do I make a separate file, as in:
现在,我已经为这两个表指定了hibernate .hbm.xml文件。但在这种情况下,查询的返回值是一个连接。因此,我该如何为它指定数据结构?我是否制作单独的文件,如:
<id name="name" type="string" column="name">
<generator class="native">
</generator>
</id>
<property name="age"
type="string"
column="age" />
<property name="sex"
type="string"
column="sex" />
<property name="location"
type="string"
column="location" />
<property name="brand"
type="string"
column="brand" />
and then write the java-mappings file and make getters and setters for each one?
然后编写java-mappings文件并为每个文件制作getter和setter?
OR... do I simply add the columns to an existing java file without modifying any XML?
或者...我只是将列添加到现有的java文件而不修改任何XML?
public class EmployeeModel // THIS IS THE JAVA MAPPING FILE FOR EMPLOYEE
{ private String name;
private String age;
private String sex;
private String location;
private String brand;
// Getters and Setters for the above values.
Now, what happens when I want only Employee information in this case? Please do help me out guys. Thanks a lot!
现在,当我在这种情况下只需要员工信息时会发生什么?请帮帮我们。非常感谢!
1 个解决方案
#1
0
Note: The below explanation only applies if you want to use native SQL for some reason.
注意:以下说明仅适用于您出于某种原因要使用本机SQL的情况。
Since you are not going to persist the returned values, you shouldn't create any mapping file (.hbm.xml
). Hibernate can return a non-managed object. Just create an EmployeeModel
class and use this:
由于您不会保留返回的值,因此不应创建任何映射文件(.hbm.xml)。 Hibernate可以返回一个非托管对象。只需创建一个EmployeeModel类并使用它:
session.createSQLQuery("select employee.Name, employee.Age, employee.Sex, table.Location, table.Brand from table, employee where table.employee_name=employee.name")
.setResultTransformer(Transformers.aliasToBean(EmployeeModel.class));
as explained here: http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#d5e8394
如下所述:http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#d5e8394
If you want just the Employee
, you can do this:
如果您只想要员工,您可以这样做:
sess.createSQLQuery("select employee.* from from table, employee where table.employee_name=employee.name").addEntity(Employee.class);
This is explained here: http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#d5e8271
这在这里解释:http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#d5e8271
更多相关文章
- 使用Maven时,不要将JAR添加到Google Cloud上的暂存文件夹中
- Java jasypt对.properties配置文件密码加密
- Java调用gc机制强制删除文件
- 将日期保存到序列化文件
- 新手求教RandomAccessFile读写文件到底写入的是什么?
- Java逐行读写TXT文件
- 帮忙解析一下 java解析普通的文本文件,提取信息到execl文件中
- 如何从java获取spring配置文件名称
- [疯狂Java]NIO.2:walkFileTree、FileVisitor(遍历文件/目录)