Cách fix lỗi could not initialize proxy no session

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(); } }

Chủ đề