How to find a way to get the DataSource object from the EntityManager in Hibernate

Solution Verified - Updated -

Issue

  • We don't have a custom mapping capability in the JPA specification.
  • We need help to build a Spring JdbcTemplate object when only have a reference to the entity manager.
  • In other words, if we have a reference to an EntityManager for my persistence unit, how to get the data source (even use JBoss specific APIs) so that we can create a JdbcTemplate object.

  • In otherwords imagine:

EntityManager em = ...
JdbcTemplate springTempalte = new JdbcTemplate( em.getDatasource() );

  • We would be able to use the spring template.
  • The problem is that we cannot find a way to get to the data source from the entity manager.
  • We don't want to reference the data source directly, since there can be multiple persistence units linked to different data-sources and we want that configuration all in one place.
  • What API calls (even JBoss specific) can we make to get the data source used in a given EntityManager in JBoss EAP 6.3.
  • We need to know how to get to the data-source, but lets know what SqlResultSetMapping is and whether we can have a custom implementation that does the mapping instead of using the annotation class?
  • Can please provide us JBoss specific or Hibernate APIs that help us go from the JBoss EntityManager implementation to the underlying data source ?
  • We are okay to cast that object to a JBoss specific or hibernate specific implementation that we can guarantee exists in EAP 6.4+.

Environment

  • Red Hat JBoss Enterprise Application Platform(EAP)
    • 6.3.3
    • Hibernate/JPA

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content