I have database structure with which is possible to have one object which to point another object. The same idea like the Symbolic Link in Linux/UNIX. This is possible because all complex objects are with global unique identifier and for each identifier I have some additional information part of which is linked object. Everything is working fine except that retrieving the linked object. At the moment I do that manually with the following code:
public void populate(EntityManager em, DataObjectBean dob)
{
List<String> properties = manySidedProperties.get(dob.getClass().getName());
if(properties != null && properties.size() > 0)
{
for(String property : properties)
{
try
{
Class cls = dob.getClass();
String methodName = "get" + property + "Id";
Method method = cls.getMethod(methodName, noParameterTypes);
BigInteger id = (BigInteger)method.invoke(dob, noParameters);
if(id != null)
{
DataObject dataObject = em.find(DataObject.class, id);
if(dataObject == null)
continue;
DataObjectTypeLocal dotLocal = getDataObjectTypeLocal();
DataObjectType dot = dotLocal.getDataObjectType(dataObject.getDataObjectTypeId());
Class valueClass = Class.forName(dot.getDataObjectType());
Object entity = em.find(valueClass, id);
if(entity == null)
continue;
methodName = "set" + property;
method = cls.getMethod(methodName, new Class[] {entity.getClass()});
method.invoke(dob, new Object[] {entity});
}
}
catch(Exception ex)
{
throw new RuntimeException(ex);
}
}
}
}
private static TreeMap<String, List<String>> manySidedProperties;
static
{
manySidedProperties = new TreeMap<String, List<String>>();
properties = Arrays.asList("Recipient", "ShippingAgent");
manySidedProperties.put(Invoice.class.getName(), properties);
}
My question is: Is it possible this to be done automatically with some special select statement or inheritance or something like that?
The working environment is the last version of JPA Hibernate.
Miro.
_________________ Regards,
Miro
|