I'm trying to integrate Eclipse Texo into my existing Hibernate project. I have modeled my domain model in ECore and generated both EMF and POJO code from there using Texo and the regular EMF code generation.
Fetching entities (POJOs) stored in the database works without problems, now I would like to use Texo's ModelEMFConverter
to transform the Hibernate-mapped data model into the corresponding EMF model. However, this attempt fails due to the entities returned by Hibernate being transparently proxied. Texo's ModelResolver
is unable to look up a model descriptor for these proxied entities, since it compares the class of the entity (which is the proxy class) to the mapped classes and fails with an exception in my case:
Exception in thread "main" java.lang.IllegalStateException: The class class foobar.Entity_$$_jvst4f2_5 is not managed by this ModelResolver at org.eclipse.emf.texo.utils.Check.isNotNull(Check.java:66) at org.eclipse.emf.texo.model.ModelResolver.getModelDescriptor(ModelResolver.java:366) at org.eclipse.emf.texo.model.ModelResolver.getModelObject(ModelResolver.java:298) at org.eclipse.emf.texo.resolver.DefaultObjectResolver.toUri(DefaultObjectResolver.java:188) at org.eclipse.emf.texo.resolver.DefaultObjectResolver.resolveToEObject(DefaultObjectResolver.java:98) at org.eclipse.emf.texo.converter.ModelEMFConverter.createTarget(ModelEMFConverter.java:146) at org.eclipse.emf.texo.converter.ModelEMFConverter.convertSingleEReference(ModelEMFConverter.java:265) at org.eclipse.emf.texo.converter.ModelEMFConverter.convertContent(ModelEMFConverter.java:189) at org.eclipse.emf.texo.converter.ModelEMFConverter.convert(ModelEMFConverter.java:107) [...]
The relevant code bits from ModelResolver
:
public ModelObject<?> getModelObject(final Object target) {
/* ... snip ... */
final ModelDescriptor modelDescriptor = getModelDescriptor(target.getClass(), true);
return modelDescriptor.createAdapter(target);
}
I tried manually unwrapping the proxied entities before passing them to the model converter using the following code:
final List<Object> objects = entities
.stream()
.map(o ->
o instanceof HibernateProxy ?
(Entity) ((HibernateProxy) o).getHibernateLazyInitializer().getImplementation() : o)
.collect(Collectors.toList());
final ModelEMFConverter converter = new ModelEMFConverter();
final Collection<EObject> eObjects = converter.convert(objects);
In theory this approach seems to work (I checked by single-stepping through the conversion code), however it fails for entities referenced by associations in my data model, that are not contained in the original entities
list. I would like to avoid having to traverse the entire object graph by hand in order to get rid of the proxies.
Is there a way to retrieve unproxied entities from Hibernate? Or does anyone maybe have a suggestion as to how I could approach this model transformation from a different angle?
Thanks for your help in advance!