Announcement Announcement Module
No announcement yet.
Downcast EntityManager Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Downcast EntityManager


    I'm using Spring 2.0rc1 with JPA (Hibernate impl). The EntityManager is injected using the @PersistenceContext annotation in my service layer:

    private EntityManager em;

    In some methods I have to use the Criteria API, so I need a reference to the current Hibernate Session. My idea was to downcast:

    Session sess = ((HibernateEntityManager) em).getSession();

    but this results in a ClassCastException:

    java.lang.ClassCastException: $Proxy23

    Any ideas how to get a reference to the underlying Session using the (very cool!) @PersistenceContext-approach?

    Thank you,

  • #2
    I just run into this problem right now, there is a method in the AbstractEntityManagerFactory called getEntityManagerInterface where the documentation states this is used to determine the interface of the returned EntityManager(Proxy). It also states that the JpaVendorDialect should be used to determine the interface but the getter method is never called. So I guess this is a bug or is there another way to get around this problem?


    • #3
      Make sure that you are using the latest 2.0 RC (or if you wait a bit the final release) since some significant changes with respect to JPA have occured.
      In order to do proper casting make sure you specify the proper jpa dialect and vendor adapter (in your case HibernateJpaDialect and HibernateJpaVendorAdapter).