【问题标题】:Joining two Identical tables using Hibernate without Association使用没有关联的 Hibernate 连接两个相同的表
【发布时间】:2014-11-07 16:19:35
【问题描述】:

我有两个表 table1 和 table2,它们在架构和复合 ID 方面完全相同。唯一的区别是它们中存在的数据。第一个表有 4 个月的数据,另一个表有 21 个月的数据。在我的应用程序中,我需要两个表的 who 数据。这里我们不能在这两个表之间建立关联,因为数据不属于任何场景。

我尝试在 HQL 查询中使用 FULL JOIN 将两个表加入两个不同的 pojo,但它要求加入路径,这意味着实体之间应该有一些关联才能在休眠中加入。

我还为这两个表尝试了一个 pojo,因为它们完全相似,除了其中的数据。为此,我使用了休眠功能实体名称。但它给了我如下错误;

**persistent class not known: MainClass**

这是我用来将表映射到实体的 HBM:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Nov 2, 2014 7:23:43 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
        <class name="pojos.MainClass"
        table="table1" entity-name="table1">
        <composite-id class="pojos.CompositeID"
            name="compositeID">
            <key-property name="*****" type="string">
                <column name="*****" length="5" />
            </key-property>
            <key-property name="*****" type="string">
                <column name="*****" length="3" />
            </key-property>
            <key-property name="*****" type="string">
                <column name="******" length="3" />
            </key-property>
            <key-property name="****" type="string">
                <column name="****" length="7" />
            </key-property>
        </composite-id>
        <property name="****" type="string">
            <column name="****" length="7" />
        </property>
        <one-to-one name="otherClass"
            class="pojos.OtherClass"></one-to-one>
    </class>
    <class name="pojos.MainClass"
        table="table2" entity-name="table2">
        <composite-id class="pojos.CompositeID"
            name="compositeID">
            <key-property name="*****" type="string">
                <column name="*****" length="5" />
            </key-property>
            <key-property name="*****" type="string">
                <column name="*****" length="3" />
            </key-property>
            <key-property name="*****" type="string">
                <column name="******" length="3" />
            </key-property>
            <key-property name="****" type="string">
                <column name="****" length="7" />
            </key-property>
        </composite-id>
        <property name="****" type="string">
            <column name="****" length="7" />
        </property>
        <one-to-one name="otherClass"
            class="pojos.OtherClass"></one-to-one>
    </class>
</hibernate-mapping>

请建议我从两个相同的表中获取所有数据而不重复的最佳方法。

谢谢。 苏吉斯·G

【问题讨论】:

    标签: spring hibernate join db2


    【解决方案1】:

    我可以想到两种方法

    1. 创建数据库视图并将其映射到实体类。这样可以避免重复。
    2. 创建数据库联合查询。看看here

    【讨论】:

    • 非常感谢 rohit 的建议。因此,通过实体名称为同一个实体映射两个表并不是解决我的问题的正确方法。我在 hbm 文件中做错了什么,进而引发错误“Persistent Class Not Known”。
    • 检查您的原始映射文件中name 类中是否有任何拼写错误。或者如果在提到的包中正确构建了类,请检查您的可部署存档。
    • 谢谢罗希特。我已经交叉检查是否有任何错别字,但没有任何错别字。我在 hbm 中是否有任何错误。
    • 这个 hbm 看起来确实正确。对于这个问题,您还在坚持原来的解决方案吗?
    • 是的,实际上在这个link 中他也在做同样的事情。问题是我总共有 4 个表表 1、表 2、表 3、表 4。其中表 1、表 2 和表 3、表 4 完全相同,但它们包含的数据不同。一个包含 4 个月的数据,另一个包含 21 个月的数据。我加入表 1 和表 3 以获得具有一对一关系的 4 个月数据。甚至表 2 和表 4 也具有一对一的关系,因为它们都有 21 个月的数据。
    猜你喜欢
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多