[英]How do I specify hibernate join mappings?
说我有以下两个数据库:
Employee
|Name|Age|Sex|
Table
|Employee_Name|Location|Brand|
我希望执行以下查询形式:
select * from table,employee where table.employee_name=employee.name;
现在,我为这两个表都指定了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" />
然后编写java-mappings文件,并为每个文件创建getter和setter?
或者...我是否只是将列添加到现有的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.
现在,在这种情况下,我只需要员工信息时会发生什么? 请帮我。 非常感谢!
注意 :以下说明仅在由于某些原因要使用本机SQL时适用。
由于您将不保留返回的值,因此不应创建任何映射文件( .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));
如此处所述: http : //docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#d5e8394
如果只需要Employee
,则可以执行以下操作:
sess.createSQLQuery("select employee.* from from table, employee where table.employee_name=employee.name").addEntity(Employee.class);
此处对此进行了说明: http : //docs.jboss.org/hibernate/orm/4.1/manual/zh-CN/html_single/#d5e8271
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.