我得到以下异常:
Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:167)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:215)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:190)
at sei.persistence.wf.entities.Element_$$_jvstc68_47.getNote(Element_$$_jvstc68_47.java)
at JSON_to_XML.createBpmnRepresantation(JSON_to_XML.java:139)
at JSON_to_XML.main(JSON_to_XML.java:84)
Run Code Online (Sandbox Code Playgroud)
当我尝试从main调用以下行时:
Model subProcessModel = getModelByModelGroup(1112);
System.out.println(subProcessModel.getElement().getNote());
Run Code Online (Sandbox Code Playgroud)
我getModelByModelGroup(int modelgroupid)首先实现了这个方法,如下所示:
public static Model getModelByModelGroup(int modelGroupId, boolean openTransaction) {
Session session = SessionFactoryHelper.getSessionFactory().getCurrentSession();
Transaction tx = null;
if (openTransaction) {
tx = session.getTransaction();
}
String responseMessage = "";
try {
if (openTransaction) {
tx.begin();
}
Query query = session.createQuery("from Model where modelGroup.id = :modelGroupId");
query.setParameter("modelGroupId", modelGroupId); …Run Code Online (Sandbox Code Playgroud) 我有一个Person类:
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
@ManyToMany(fetch = FetchType.LAZY)
private List<Role> roles;
// etc
}
Run Code Online (Sandbox Code Playgroud)
与多对多的关系是懒惰的.
在我的控制器中我有
@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping("/get")
public @ResponseBody Person getPerson() {
Person person = personRepository.findOne(1L);
return person;
}
}
Run Code Online (Sandbox Code Playgroud)
PersonRepository就是这个代码,根据本指南编写
public interface PersonRepository extends JpaRepository<Person, Long> {
}
Run Code Online (Sandbox Code Playgroud)
但是,在这个控制器中,我实际上需要惰性数据.如何触发加载?
试图访问它将失败
未能懒惰地初始化角色集合:no.dusken.momus.model.Person.roles,无法初始化代理 - 没有会话
或其他例外取决于我尝试的内容.
我的xml描述,如果需要的话.
谢谢.
我正在尝试使用SpringBoot,SpringDataJpa以及使用SpringBootTest进行单元测试的ManyToOne双向关联.但是,测试失败,堆栈跟踪如下所示.但是我无法找到原因.任何指针都会有所帮助
Spring Boot JUnit Test下面的ManyToOne双向关联失败.
@Test
public void testFindByRegionIdEquals() {
Region region = regionService.findByRegionIdEquals(1L);
Region expectedRegion = new Region(1L, "Europe");
assertThat(region).isNotNull().isEqualTo(expectedRegion);
assertThat(region.getCountries()).isNotEmpty().doesNotContainNull().size().isEqualTo(8);
}
Run Code Online (Sandbox Code Playgroud)
异常StackTrace
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: zikzakjack.domain.Region.countries, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204)
at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148)
at org.hibernate.collection.internal.PersistentSet.isEmpty(PersistentSet.java:149)
at org.assertj.core.util.IterableUtil.isNullOrEmpty(IterableUtil.java:35)
at org.assertj.core.internal.Iterables.assertNotEmpty(Iterables.java:152)
at org.assertj.core.api.AbstractIterableAssert.isNotEmpty(AbstractIterableAssert.java:145)
at zikzakjack.service.RegionServiceTests.testFindByRegionIdEquals(RegionServiceTests.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Run Code Online (Sandbox Code Playgroud)
拥有实体ManyToOne
@Data
@Entity
@Table(name = "COUNTRIES")
public class Country implements Serializable, Comparable<Country> {
private static final long serialVersionUID = 1L; …Run Code Online (Sandbox Code Playgroud) 我有一个Spring 3 + JPA 2.0应用程序.在我@Controller需要一个初始化对象,但我有代理,我需要能够以编程方式初始化它.我需要类似的功能org.hibernate.Hibernate.initialize(Object).
有人可以帮忙吗 该对象用于AJAX操作.如果属性是代理,我不能将其作为JSON发送
我用spring-data-jpa用spring-boot(v2.0.0.RELEASE),只是写在MySQL凝乳演示,但它如下发生运行时异常,源代码如下:
源代码
User.java
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Integer id;
private String username;
private String password;
...getter&setter
}
Run Code Online (Sandbox Code Playgroud)
UserRepository.java
public interface UserRepository extends JpaRepository<User, Integer> {
}
Run Code Online (Sandbox Code Playgroud)
UserServiceTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserRepository userRepository;
@Test
public void getUserById() throws Exception{
userRepository.getOne(1);
}
}
Run Code Online (Sandbox Code Playgroud)
application.yml
spring:
datasource:
username: ***
password: ***
driver-class-name: com.mysql.jdbc.Driver
url: ********
thymeleaf:
cache: false
jpa:
show-sql: true
hibernate: …Run Code Online (Sandbox Code Playgroud) java ×4
hibernate ×3
jpa ×2
spring ×2
spring-boot ×2
spring-data ×2
lazy-loading ×1
orm ×1
session ×1
spring-json ×1