Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate mapping for complex joins

I have legacy code that I am trying to map to the new code.

OLD_PERSON
pid
sid
name
age

NEW_PERSON
pid
sid
fid
age

RESOLVE_PERSON
pid
fid
status

Java class

domain.Person {
 ID _id;
 String _name;
 Integer _age;
}

In the legacy world, there was just one table: OLD_TABLE. The hibernate mapping was simple, just one class and its columns. In the new world, I have to use the above 3 tables and produce one Entity where the name comes from OLD_PERSON and age from NEW_PERSON. So basically a SQL query is:

select op.name as name, np.age as age
from OLD_PERSON op
INNER JOIN RESOLVE_PERSON rp
on rp.pid = op.pid
INNER JOIN NEW_PERSON np 
on np.pid = rp.pid and np.fid = rp.fid and np.sid = op.sid
where rp.status = 'CURRENT'

Upon research/googling I found that I can use "Secondary tables" which are equivalent of "JOIN table" in hibernate xml. Note: I cannot use annotation as this code is old and I am still on hibernate3.5.6.

So I added a join table inside my mapping file:

<class name="domain.Person" table="OLD_PERSON">
        <composite-id name="_id" class="Id">
            <key-property access="property" name="key1" type="long" column="pid" />
            <key-property access="property" name="key2" type="int" column="sid" />

            <generator class="assigned" />
        </composite-id>

        <property name="_Name" type="String" column="name" />

        <join table="NEW_PERSON" fetch="join" inverse="false">
          <key>
            <column name="pid" />
            <column name="sid" />
          </key>
            <property name="_age" column="age" not-null="true" />
        </join>
</class>

But the join to NEW_PERSON table requires an inner join with RESOLVE_PERSON table. I tried using subselect but its not the correct thing to use. I am unable to plug in formula anywhere here.

Any pointers on how this can be achieved? What I am essentially asking for is how to apply a criteria/constraint check on the JOIN.

like image 209
imgr8 Avatar asked Mar 19 '17 22:03

imgr8


1 Answers

I had a similar situation as your problem. I have mapped the third table to an entity and use the DAO for this entity for getting something by its property.

Here is the code (since I have used annotation, so this is not something you can use directly, but hopefully it will give you some inspiration),

@Entity
@Table(name = "resolve_person")
public class ResolvePerson implements java.io.Serializable {
    private OldPerson old;
    private NewPerson new;

    ...

    @ManyToOne // you may change it to other relationships
    @JoinColumn(name = "pid", nullable = false)
    public OldPerson getOldPerson () {
        return this.old;
    }

    public void setOldPerson (OldPerson old) {
        this.old = old;
    }

    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator", strategy = "foreign",
            parameters = { @org.hibernate.annotations.Parameter(name = "property", value = "fid") })
    @Column(name = "fid", nullable = false, unique = true)
    public NewPerson getNewPerson () {
        return this.New;
    }

    public void setNewPerson (NewPerson new) {
        this.new = new;
    }
}

The DAO is quite normal,

public List findByProperty(String propertyName, Object value) {
        log.debug("finding ResolvePerson instance with property: "
                + propertyName + ", value: " + value);
        try {
            String queryString = "from ResolvePerson as model where model."
                    + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

Now you can query with the property "status" set to "current".

like image 133
Simon Avatar answered Sep 27 '22 19:09

Simon