Hi Kum,
I'm back to it again. The code I would use is a follows:
Suggested Mapping (simplified!):
Code:
<class name="Product" table="PRODUCT">
<id name="customerNumber" type="string" column="CUST_NBR" >
<generator class="assigned" />
</id>
<property name="pCode" column="PCODE" />
</class>
<class name="Client" table="CLIENT">
<id name="clientNumber" type="string" column="CLIENT_NBR">
</id>
<property name="clientReferenceNumber" type="string" column="CR_NBR"/>
</class>
The classes (also simplified):
Code:
public class Client {
private String clientNumber;
private Product product;
private String clientReferenceNumber;
// Add getter and setters + constructor(s)
}
public class Product {
private String customerNumber;
private String pCode;
private Set clients = new HashSet();
// Add getter and setters + constructor(s)
}
In order to get a set of products with their "associated" clients (note that the association between client and product is also populated):
Code:
Query query = session.createQuery("SELECT c, p "
+ "FROM Product p, Client c WHERE c.clientReferenceNumber = (p.customerNumber || p.pCode)");
List list = query.list();
Iterator it = list.iterator();
Set productSet = new HashSet();
while(it.hasNext()) {
Object[] objects = (Object[]) it.next();
Client c = (Client) objects[0];
Product p = (Product) objects[1];
c.setProduct(p);
p.getClients().add(c);
productSet.add(p);
}
return productSet;
I must admit that it's a bit "ugly" - but as I said, I didn't find a "nicer" way. And in the meantime I don't think that using an association is the right way.
Note: I didn't test the code above a lot - so there may be a few mistakes left. Analyse it - and decide whether it is useful or not...
Erik