0

I have two tables table1 and table2 which are exactly same in regard to the Schema and the Composite IDs. The only difference is the data which is present in them. The first table has 4 months of data and the other table has 21 months of data. In my application I need the who data of both the tables. Here we cannot have association between this two tables as the data dont fall in any scenario.

I have tried to join the two tables 2 different pojos using an FULL JOIN in HQL query but its asked for a path to join which mean there should be some association between the entities to join in hibernate.

I also tried a single pojo for both the tables as they are exactly similar except the data in them. For this I have used the hibernate feature entity-name. But its giving me an error as below;

**persistent class not known: MainClass**

This is the HBM which I m using to map the table to the entity:

<?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>

Please suggest me which would be the best approach for getting all the data without duplicates from the both identical tables.

Thanks. Sujith G

Sujith G
  • 1
  • 1

1 Answers1

0

I can think of two approaches

  1. create a database view and map it to a entity class. This will avoid duplication.
  2. Create a database Union query. Take a look here
Community
  • 1
  • 1
Rohit
  • 2,132
  • 1
  • 15
  • 24
  • Thanks a ton for the suggestions rohit. And so mapping the two tables for the same entity by entity-name isn't a right approach for my problem. Is there any wrong I am doing in the hbm file which inturn throws the error "Persistent Class Not Known". – Sujith G Nov 07 '14 at 18:26
  • Check if there is any typo in class `name` in your original mapping file. or Check in your deployable archive if the class is correctly build in the mentioned package. – Rohit Nov 07 '14 at 19:14
  • Thanks Rohit. I have cross checked if there are any typos, but dont have any. Is there any mistake that I am doing in the hbm. – Sujith G Nov 08 '14 at 19:08
  • This hbm does look correct. Are you still persisting with your original solution for this problem? – Rohit Nov 09 '14 at 18:10
  • Yes, actually in this [link](http://stackoverflow.com/questions/13767125/hibernate-query-with-entity-name) he is doing the same. The thing is I have total of 4 tables Table 1, Table 2, Table 3, Table 4. In which Table 1,Table 2 and Table 3, Table 4 are exactly Identical but they differ in data which they contain. One contains 4 months data and the other contain 21 months data. I am joining Table 1 and Table 3 to get 4 months of data which has one-one relationship. Even Table 2 and Table 4 has one-to-one relation ship as both have 21 months data. – Sujith G Nov 10 '14 at 17:08
  • Okay, I got it working Rohit. In the above hbm files, I was missing the entity-name in one-to-one association to which they getting mapped to. – Sujith G Nov 10 '14 at 20:05
  • Good to hear that. Wouldn't in this approach you require two queries for data range overlapping the two periods? – Rohit Nov 11 '14 at 05:40
  • No we dont need two quires. When I create a criteria for the first table pojo, it brings the distinct data from both the table1 and table2 all the 24 months data. – Sujith G Nov 11 '14 at 06:02