Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get database metadata from entity manager

Tags:

hibernate

jpa

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

like image 833
user509755 Avatar asked May 15 '12 19:05

user509755


People also ask

What is database metadata in JDBC?

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()

What is @PersistenceContext annotation used for?

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.

What is metadata in Hibernate?

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.


1 Answers

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

like image 59
Emmanuel Avatar answered Sep 28 '22 08:09

Emmanuel