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

更多相关文章

  1. 使用Maven时,不要将JAR添加到Google Cloud上的暂存文件夹中
  2. Java jasypt对.properties配置文件密码加密
  3. Java调用gc机制强制删除文件
  4. 将日期保存到序列化文件
  5. 新手求教RandomAccessFile读写文件到底写入的是什么?
  6. Java逐行读写TXT文件
  7. 帮忙解析一下 java解析普通的文本文件,提取信息到execl文件中
  8. 如何从java获取spring配置文件名称
  9. [疯狂Java]NIO.2:walkFileTree、FileVisitor(遍历文件/目录)

随机推荐

  1. 你一定不能错过的pandas 1.0.0四大新特性
  2. 用Python编写小工具下载OSM路网数据
  3. 使用Python检测新冠肺炎疫情拐点,抗疫成果
  4. 对容器镜像的思考和讨论
  5. 用python爬取3万多条评论,看韩国人如何评
  6. 是技术也是艺术 使用geopandas玩转地图可
  7. conda+jupyter玩转数据科学环境搭建
  8. 1 简介 在上一篇文章中我们对geopandas
  9. 简单几步,教你使用scikit-learn做分类和回
  10. Android常用小功能实例