简体   繁体   中英

How to get / initialize / unproxy data from session.load(E.class, id) in Hibernate?

How to get / initialize / unproxy data from session.load(E.class, id) in Hibernate?? No (eg @OneToMany or @ManyToMany ...)

Hibernate.initialize(el) return new property "hibernateLazyInitializer": {}


try (Session session = sessionFactory.openSession()) {

    session.beginTransaction();

    final Employee el = session.load(E.class, id);//Could not write JSON: could not initialize proxy

    Hibernate.initialize(el);
    Employee el2 =  Hibernate.unproxy(el, E.class); // "hibernateLazyInitializer": {}

    session.getTransaction().commit();

    return el; //"hibernateLazyInitializer": {}
}

if no "unproxy" you will get error

"message": "Could not write JSON: could not initialize proxy [ *] - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy [****] - no Session (through reference chain: com.* .E$HibernateProxy$Zq7mcdJl[\\"firstName\\"])",

Need use:

E el =  Hibernate.unproxy(session.load(E.class, id), E.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM