Skip to main content
corrected spelling
Source Link
Stefan Haberl
  • 10.3k
  • 7
  • 76
  • 86

There's another option with still a lot of casts involved, but at least it doesn't need reflection, which will give you back compile time checking:

public Connection getConnection(final EntityManager em) {
  HibernateEntityManager hem = (HibernateEntityManager) em;
  SessionImplementor sim = (SessionImplementor) hem.getSession();
  return sim.connection();
}

You could of course make that even "prettier" with a viewfew instanceof checks, but the version above works for me.

There's another option with still a lot of casts involved, but at least it doesn't need reflection, which will give you back compile time checking:

public Connection getConnection(final EntityManager em) {
  HibernateEntityManager hem = (HibernateEntityManager) em;
  SessionImplementor sim = (SessionImplementor) hem.getSession();
  return sim.connection();
}

You could of course make that even "prettier" with a view instanceof checks, but the version above works for me.

There's another option with still a lot of casts involved, but at least it doesn't need reflection, which will give you back compile time checking:

public Connection getConnection(final EntityManager em) {
  HibernateEntityManager hem = (HibernateEntityManager) em;
  SessionImplementor sim = (SessionImplementor) hem.getSession();
  return sim.connection();
}

You could of course make that even "prettier" with a few instanceof checks, but the version above works for me.

Source Link
Stefan Haberl
  • 10.3k
  • 7
  • 76
  • 86

There's another option with still a lot of casts involved, but at least it doesn't need reflection, which will give you back compile time checking:

public Connection getConnection(final EntityManager em) {
  HibernateEntityManager hem = (HibernateEntityManager) em;
  SessionImplementor sim = (SessionImplementor) hem.getSession();
  return sim.connection();
}

You could of course make that even "prettier" with a view instanceof checks, but the version above works for me.