Hibernate version: 3.1.1
Hi all,
I'm new to hibernate and have a perculiar situation. I am creating a database that sites to the side of another system without maintaining a direct link. Therefore the datastructures within it have fields which are effectively foreign keys to this other database.
In my POJOs and Hibernate I am trying to figure out how to load the POJOs with data from two different databases. In other words a POJO may contain fields from two different tables which reside in different databases with the id field being the only common data. The best I have come up with so far is to query one for a list of items, then loop through them and use .refresh(Object) to load the second block of data from the second database. Here's some sample code:
Code:
public void load() {
//Start transactions.
this.lpisession.beginTransaction();
this.commissionsession.beginTransaction();
//Set a calendar to be the start of the year.
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(2006,1,1,0,0,0);
// get some data from the database to prove that hibernate is working.
List<Order> results =
(List <Order>) this.lpisession.createCriteria(Order.class)
.add(Restrictions.ge("entryDt", cal.getTime()))
.addOrder(org.hibernate.criterion.Order.asc("orderId"))
.list();
System.out.println("List class is an instance of " + results.getClass().getName());
for (Order o : results) {
System.out.println(o.getAccountId() + " => " + o.getOrderId());
try {
//Now load the rest of the object fields from the other database.
this.commissionsession.refresh(o);
}
catch (UnresolvableObjectException uro) {
//Do nothing - we are not concerned about missing rows in
//the commission database as these are normal.
}
System.out.println("\tOrderId = " + o.getOrderId());
System.out.println("\tAccountId = " + o.getAccountId());
System.out.println("\tCurrency = " + o.getCurrency());
System.out.println("\tStatus = " + o.getStatusId());
}
this.lpisession.getTransaction().rollback();
this.commissionsession.getTransaction().rollback();
}
The following is the lpi hbm file for Order:
Code:
<hibernate-mapping>
<class name="com.lp.commissions.Order" table="order_header" mutable="false">
<cache usage="read-only" />
<id name="orderId" column="order_id">
<generator class="native" />
</id>
<property name="accountId" type="int" column="account_id" />
<property name="currency" type="string" column="currency" />
<property name="entryDt" type="timestamp" column="entry_dt" />
</class>
</hibernate-mapping>
And here is the mapping for the commissions Orders table:
Code:
<hibernate-mapping>
<class name="com.lp.commissions.Order" table="orders">
<id name="orderId" column="lpi_order_id">
<generator class="native" />
</id>
<property name="statusId" type="int" column="status_id" />
</class>
</hibernate-mapping>
The only thing that I would like to change is to be able to bulk load based on a collection of objects. I cannot see anyway to do this other than what I have done.
Have I missed something or is there a better way to do this ?
Thanks
Derek