question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

See original GitHub issue

I want to use to hibernate level2 cache as flow config.

@Getter @Entity public class Personnel{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private String name; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; @ManyToOne(fetch = FetchType.LAZY) private Post post; }

Now for get object at the first time, all things are ok! (that means I want to load ‘post’ from personnel) But when I call this function for a second time, in my personnel object the ‘post’ is null and I get an error as below:

Caused by: java.lang.reflect.InvocationTargetException: null at sun.reflect.GeneratedMethodAccessor167.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.dozer.util.ReflectionUtils.invoke(ReflectionUtils.java:323) ... 132 common frames omitted Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:146) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:259) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73) at org.model.core.Post_$$_jvstb54_2a.getId(Post_$$_jvstb54_2a.java)

Hibernate 5.2 Radisson 3.12.5 spring-boot 2.1.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
siedmohsen2commented, Jun 13, 2020

thanks It works very fine

0reactions
mrnikocommented, Jun 8, 2020

Unable to reproduce, below is my test code:

public class LazeTest extends BaseCoreFunctionalTestCase {

    @Entity
    public static class A implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;

      private String firstName;
      private String lastName;
      private String code;
      private String fatherName;
      @Enumerated
      private PersonelKind kind;
      @ManyToOne(fetch = FetchType.LAZY)
      private B post;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getFatherName() {
            return fatherName;
        }

        public void setFatherName(String fatherName) {
            this.fatherName = fatherName;
        }

        public PersonelKind getKind() {
            return kind;
        }

        public void setKind(PersonelKind kind) {
            this.kind = kind;
        }

        public B getPost() {
            return post;
        }

        public void setPost(B post) {
            this.post = post;
        }
    }

    public enum PersonelKind {
        A, B;
    }

    @Entity
    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
    public static class B implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;

      private String firstName;
      private String lastName;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    }

    @Override
    protected Class<?>[] getAnnotatedClasses() {
        return new Class[] { A.class, B.class };
    }

    @Override
    protected void configure(Configuration cfg) {
        super.configure(cfg);
        cfg.setProperty(Environment.DRIVER, org.h2.Driver.class.getName());
        cfg.setProperty(Environment.URL, "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;");
        cfg.setProperty(Environment.USER, "sa");
        cfg.setProperty(Environment.PASS, "");
        cfg.setProperty(Environment.CACHE_REGION_PREFIX, "");
        cfg.setProperty(Environment.GENERATE_STATISTICS, "true");

        cfg.setProperty(Environment.SHOW_SQL, "true");
        cfg.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
        cfg.setProperty(Environment.USE_QUERY_CACHE, "true");
        cfg.setProperty(Environment.CACHE_REGION_FACTORY, RedissonRegionFactory.class.getName());
    }
    
    @Before
    public void before() {
        sessionFactory().getCache().evictEntityRegions();
        sessionFactory().getStatistics().clear();
    }

    @Test
    public void test() {
        Session s = openSession();
        s.beginTransaction();

        A a = new A();
        a.setFatherName("nameA");
        B b = new B();
        b.setFirstName("nameB");
        a.setPost(b);
        s.save(b);
        Serializable id = s.save(a);
        s.flush();
        s.getTransaction().commit();

        s = openSession();
        s.beginTransaction();
        A a1 = s.load(A.class, id);
        assertThat(a1.getPost().getFirstName()).isEqualTo("nameB");
        s.getTransaction().commit();
    }
    
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hibernate could not initialize proxy – no Session - Baeldung
This error occurs when we try to fetch a lazy-loaded object from the database by using a proxy object, but the Hibernate session...
Read more >
How to fix org.hibernate.LazyInitializationException - could not ...
hibernate.LazyInitializationException - could not initialize proxy - no Session" exception. Changing that Set from "fetch=FetchType.LAZY" to "fetch=FetchType.
Read more >
could not initialize proxy - no Session in Hibernate Java
hibernate.LazyInitializationException: could not initialize proxy - no Session" it is not a good solution because you are throwing away the Lazy Initialization ...
Read more >
could not initialize proxy – no Session | SpringBoot - YouTube
In this video you will learn how to solve LazyInitializationException error when working with JPA and Hibernate in Spring Boot and Spring ...
Read more >
LazyInitializationException - What it is and the best way to fix it
Hibernate throws the LazyInitializationException when it needs to initialize a lazily fetched association to another entity without an active ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found