How to find Session in Hibernate 5.2.3.Final

Asked

Viewed 813 times

1

hello! Guys I’m changing my Hibernate from version 4.3.8.Final to 5.2.3.Final. Now I’m not getting the Hibernate Session like I used to, like this:

(Session) manager.unwrap(Session.class);

Gives Cast error

:java.lang.ClassCastException: org.jboss.weldx.persistence.EntityManager$1993463486$Proxy$_$$_WeldClientProxy cannot be cast to org.hibernate.Session

Does anyone have any idea? From what I saw in the documentation it seems that it should be the same way. https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#pc-unwrap

  • 1

    As far as I can tell, it has something to do with the CDI I’m using, because ending some tests on another program without and it works...

3 answers

2

My problem was with CDI. I have a production method of Nager entityManager that was annotated with @Requestscoped. Even in the previous version of Hibernate everything worked, in form. After searching a lot I found something similar talking to switch to @Dependent and it is not that solved.

@Produces @Dependent //@RequestScoped estava assim até alterar a versão do Hibernate
    public EntityManager createEntityManager() {
        return factory.createEntityManager();
    }

    public void closeEntityManager(@Disposes EntityManager manager) {
        manager.close();
    }

Solution

  • 1

    I was just running some tests on this one @Dependent and did not like, it no longer closes the Entitymanager method with the @Disposes And still passes several times in the @Produces instead of a single request. It’s strange...I still don’t understand why it doesn’t work with the @RequestScoped, if anyone knows tell me.

1

In case you haven’t solved... I resolved so:

@RequestScoped
public Session createEntityManager() {
    return (Session) this.factory.createEntityManager();
}

public void closeEntityManager(@Disposes Session manager) {
    manager.close();
}

If you look at the Session interface of Hibernate, you will see that it is now also implements Entitymanager, from below the screens gives "tilt" in Weld. Actually Tiago from Algaworks who went deeper into it, I just used the same idea of him

public interface Session extends SharedSessionContract, EntityManager, HibernateEntityManager, AutoCloseable

0

I had a similar Issue and Solved it like this:

change

    Session session = em.unwrap(Session.class);

to

    TargetInstanceProxy<?> proxy = 
               (TargetInstanceProxy<?>) em.unwrap(Session.class);
    Session session = (Session) proxy.weld_getTargetInstance();

Session is

     org.hibernate.Session

Targetinstanceproxy is

     org.jboss.weld.interceptor.util.proxy.TargetInstanceProxy
  • 1

    Translate into English, please.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.