[hibernate-dev] [ORM] Do we have a way to check if an object is an entity?
Hi, In Hibernate Validator, we have a TraversableResolver which avoids to validate the uninitialized properties of an entity. This is done in https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/resolver/JPATraversableResolver.java#L35 and, as you can see, we execute Persistence.getPersistenceUtil().isLoaded( traversableObject, traversableProperty.getName() ) even if the traversableObject has nothing to do with Hibernate ORM. I'm looking for an API that could tell me if an object is a class potentially managed by ORM (be it an entity, an embeddable or whatever: any class potentially containing a lazy field). I was thinking that maybe injecting an EntityManagerFactory (it would require CDI though) and using the Metamodel could somehow work... but the PersistenceUtil API we currently use is capable of dealing with several persistence providers and I don't think the injected EntityManagerFactory approach will fly in this case. Is there something I could use to do that, that would be portable and cover the cases currently (somehow) taken care of? Any ideas welcome. Thanks! -- Guillaume ___ hibernate-dev mailing list hibernate-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/hibernate-dev
Re: [hibernate-dev] [ORM] Do we have a way to check if an object is an entity?
EMF + Metamodel are standard JPA contracts: try { emf.getMetamodel().managedType( theClass ); } catch ( IllegalArgumentException e ) { // JPA defined exception if the passed class is not a managed type } Again, that will (should) work on any provider On Thu, Apr 25, 2019 at 1:01 PM Guillaume Smet wrote: > Hi, > > In Hibernate Validator, we have a TraversableResolver which avoids to > validate the uninitialized properties of an entity. > > This is done in > > https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/resolver/JPATraversableResolver.java#L35 > and, as you can see, we execute Persistence.getPersistenceUtil().isLoaded( > traversableObject, traversableProperty.getName() ) even if the > traversableObject has nothing to do with Hibernate ORM. > > I'm looking for an API that could tell me if an object is a class > potentially managed by ORM (be it an entity, an embeddable or whatever: any > class potentially containing a lazy field). > > I was thinking that maybe injecting an EntityManagerFactory (it would > require CDI though) and using the Metamodel could somehow work... but the > PersistenceUtil API we currently use is capable of dealing with several > persistence providers and I don't think the injected EntityManagerFactory > approach will fly in this case. > > Is there something I could use to do that, that would be portable and cover > the cases currently (somehow) taken care of? > > Any ideas welcome. > > Thanks! > > -- > Guillaume > ___ > hibernate-dev mailing list > hibernate-dev@lists.jboss.org > https://lists.jboss.org/mailman/listinfo/hibernate-dev > ___ hibernate-dev mailing list hibernate-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/hibernate-dev
Re: [hibernate-dev] [ORM] Do we have a way to check if an object is an entity?
On Thu, Apr 25, 2019 at 8:05 PM Steve Ebersole wrote: > EMF + Metamodel are standard JPA contracts: > > > try { > emf.getMetamodel().managedType( theClass ); > } > catch ( IllegalArgumentException e ) { > // JPA defined exception if the passed class is not a managed type > } > > Again, that will (should) work on any provider > Thanks. So I was pretty sure it would work with any provider. My question was more: how to make it work with multiple providers? Right now, the current implementation is very low level and doesn't use anything but JPA to get the EMF. But I didn't see any way to get all the potentially created EMF. I suppose I could try to get all the EMF from CDI - that would make this feature CDI-compatible only but I suppose it's acceptable. I will probably will need to do that after everything is initialized or I will have a chicken and egg problem as the EMF needs to get the ValidatorFactory. The thing is that, in this case, all the EMF should be created via CDI or it won't work very well. Collecting once and for all all the managed types via emf.getMetamodel().getManagedTypes() should work. @Gunnar Morling does it make sense to you too? -- Guillaume ___ hibernate-dev mailing list hibernate-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/hibernate-dev
Re: [hibernate-dev] [ORM] Do we have a way to check if an object is an entity?
That's going to depend on which "bootstrap" they use. JPA defines 2 which it terms "SE" and "EE". Not sure this will work in all EE bootstrap environments, but you have: PersistenceProviderResolver resolver = PersistenceProviderResolverHolder.getPersistenceProviderResolver(); List providers = resolver.getPersistenceProviders(); On Thu, Apr 25, 2019 at 1:25 PM Guillaume Smet wrote: > On Thu, Apr 25, 2019 at 8:05 PM Steve Ebersole > wrote: > >> EMF + Metamodel are standard JPA contracts: >> >> >> try { >> emf.getMetamodel().managedType( theClass ); >> } >> catch ( IllegalArgumentException e ) { >> // JPA defined exception if the passed class is not a managed type >> } >> >> Again, that will (should) work on any provider >> > > Thanks. > > So I was pretty sure it would work with any provider. My question was > more: how to make it work with multiple providers? Right now, the current > implementation is very low level and doesn't use anything but JPA to get > the EMF. But I didn't see any way to get all the potentially created EMF. > > I suppose I could try to get all the EMF from CDI - that would make this > feature CDI-compatible only but I suppose it's acceptable. I will probably > will need to do that after everything is initialized or I will have a > chicken and egg problem as the EMF needs to get the ValidatorFactory. The > thing is that, in this case, all the EMF should be created via CDI or it > won't work very well. > > Collecting once and for all all the managed types via > emf.getMetamodel().getManagedTypes() should work. > > @Gunnar Morling does it make sense to you too? > > -- > Guillaume > > > ___ hibernate-dev mailing list hibernate-dev@lists.jboss.org https://lists.jboss.org/mailman/listinfo/hibernate-dev