简体   繁体   English

HQL查询以从多个表中获取数据

[英]HQL query to get data from multiple tables

I have 2 tables - A and B. And each has a corresponding hbm.xml and pojo. 我有2个表-A和B。每个表都有一个对应的hbm.xml和pojo。
Class A{
String aId;
String col;
}
Class B{
String bId;
String aId;//as reference to class A
A aObj;
}
B.hbm.xml contains the following entry to map A to B:
<many-to-one name="aId" column="AID" not-null="true" class="com.pojo.A" unique="true"/>
My requirement is that, whenever an object of B is created, it should push data into table A as well. 我的要求是,无论何时创建对象B,它都应将数据也推送到表A中。 Similarly when I read from B, it should give me data from A as well based on aId match. 同样,当我从B读取数据时,它也应该根据aId匹配从A提供数据。 It is a one-one mapping. 这是一对一的映射。 I want to pass the aId value as input to the query. 我想将aId值作为输入传递给查询。 And get the data from both the tables corresponding to that matched row. 并从与该匹配行相对应的两个表中获取数据。 Can some one tell me how to use HQL queries to achieve this? 有人可以告诉我如何使用HQL查询来实现这一目标吗? And is the structure that I have now is correct for my requirement? 我现在拥有的结构是否符合我的要求?

You classes are incorrect. 您的课程不正确。 The B class should have a field of type A, but not have an aId field. B类应具有类型A的字段,但不能具有aId字段。

Read the Hibernate documentation on unidirectional OneToOne associations , since this is what you have. 请阅读有关单向OneToOne关联的Hibernate文档,因为这就是您所拥有的。

Your requirements indicate that you should cascade all operations from B to A, and that this association should be eagerly fetched (with <many-to-one lazy="false"...> ). 您的需求表明,您应该将所有操作从B到A进行级联 ,并且应该热切地获取此关联(使用<many-to-one lazy="false"...> )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM