Results 1 to 3 of 3

Thread: Downcast EntityManager

  1. #1
    Join Date
    Jul 2005
    Location
    Rüdesheim, Germany
    Posts
    29

    Default Downcast EntityManager

    Hi,

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

    @PersistenceContext
    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,
    Sebastian

  2. #2

    Default

    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. #3
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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).
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •