I have an app which is using hibernate and jpa. I need to find out which db it is connected to so that some native sql query i execute based on db say for eg. oracle and postgres. If I am using pure jdbc then it is straight forward to get the db metadata but dont know how to get from entity manager
Thanks
The DatabaseMetaData interface provides methods to get information about the database you have connected with like, database name, database driver version, maximum column length etc... Following are some methods of DatabaseMetaData class. Method. Description. getDriverName()
You can use the @PersistenceContext annotation to inject an EntityManager in an EJB 3.0 client (such as a stateful or stateless session bean, message-driven bean, or servlet). You can use @PersistenceContext attribute unitName to specify a persistence unit by name, as Example 29-13 shows.
Hibernate ORM Tool uses Metadata Mapping Pattern to specify the mapping between classes and tables either using XML or annotations in code. Metadata Mapping specifies the mapping between classes and tables so that we could treat a table of any database like a Java class.
In Hibernate 4, you can get the database infos from the entity manager with that code:
org.hibernate.engine.spi.SessionImplementor sessionImp =
(org.hibernate.engine.spi.SessionImplementor) eManager.getDelegate();
DatabaseMetaData metadata = sessionImp.connection().getMetaData();
//do whatever you need with the metadata object...
metadata.getDatabaseProductName();
Cheers
Emmanuel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With