views:

58

answers:

2

Hello,

My project uses Spring, JPA, and Hibernate, so that all EntityManager's are injected by Spring.

I need to access persistent classes' meta data such as column lengths and optionality. The only way I know to obtain this information is through Hibernate's Configuration.getClassMapping(String className). In a pure Hibernate project without JPA and Spring, you can keep the Configuration after creating a SessionFactory(i.e. new Configuration().configure().buildSessionFactory()), but since Spring uses LocalEntityManagerFactoryBean to create a SessionFactory in the form of EntityManagerFactory, I'm not sure where to get the Configuration.

If it is not possible to retrieve the Configuration from Spring, is there any other way to access a persistent class' meta data?

Thanks.

+2  A: 

I have not used JPA and spring but only hibernate directly with spring. But I guess the architecture is similar. When using hibernate directly a LocalSessionFactoryBean is used instead of a LocalEntityManagerFactoryBean.

The way to access the hibernate configuration is described in this thread from the Spring forum. Maybe you can adapt this approach to work with LocalEntityManagerFactoryBean also.

Wolfgang
No, this won't work, a `LocalEntityManagerFactoryBean` doesn't expose any Hibernate specific stuff (and the `Configuration` isn't exposed by the `HibernatePersistence` anyway).
Pascal Thivent
+1  A: 

It turns out that it is not possible. Spring creates an EntityManagerFactory in a standard way, using a PersistenceProvider implementation specified in the persistence.xml file. HibernatePersistence, which is Hibernate's implementation of PersistenceProvider, does not keep the Configuration object after use. In order to expose the Configuration, you'll need to implement PersistenceProvider on your own, which isn't a big deal anyways.

Tom Tucker