How to search the Session in Hibernate 5.2.3.Final

1

Hello! I am changing my Hibernate from version 4.3.8.Final to 5.2.3.Final. Now I am not able to fetch the Hibernate Session as it did before, like this:

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

Give Cast Error

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

Does anyone have any ideas? From what I saw in the documentation it looks like it should be the same way.   link

    
asked by anonymous 13.10.2016 / 22:06

2 answers

2

My problem was with the CDI. I have a producer method of entityManager that was annotated with @RequestScoped. Even in the earlier version of Hibernate everything works, deta. After searching a lot I found something like talking to change to @Dependent and it is not that resolved.

@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

    
14.10.2016 / 22:12
1

If you have not resolved ... I solved it like this:

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

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

If you look at the hibernate Session interface, you will see that it is now also implementing EntityManager, hence underneath the tilt cloths in the Weld. Actually the Algaworks Tiago that went deeper into it, I just used the same idea of it

public interface Session extends SharedSessionContract, EntityManager, HibernateEntityManager, AutoCloseable
    
17.12.2016 / 18:57