作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 SpringBoot、SpringDataJpa 以及使用 SpringBootTest 进行单元测试的多对一双向关联。但是测试失败,堆栈跟踪如下所示。但是我无法找到原因。任何指针都会有所帮助
ManyToOne双向关联的Spring Boot JUnit测试失败。
@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);
}
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)
@Data
@Entity
@Table(name = "COUNTRIES")
public class Country implements Serializable, Comparable<Country> {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "COUNTRY_ID")
private String countryId;
@Column(name = "COUNTRY_NAME")
private String countryName;
@ManyToOne
@JoinColumn(name = "REGION_ID")
private Region region;
public Country() {
}
public Country(String countryId, String countryName, Region region) {
super();
this.countryId = countryId;
this.countryName = countryName;
this.region = region;
}
}
@Data
@Entity
@Table(name = "REGIONS")
public class Region implements Serializable, Comparable<Region> {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "REGION_ID")
private Long regionId;
@Column(name = "REGION_NAME")
private String regionName;
@OneToMany(mappedBy = "region", fetch = FetchType.LAZY)
private Set<Country> countries = new HashSet<Country>();
public Region() {
}
public Region(Long regionId, String regionName) {
this.regionId = regionId;
this.regionName = regionName;
}
public Region(String regionName) {
this.regionName = regionName;
}
public void addCountry(Country country) {
countries.add(country);
country.setRegion(this);
}
}
最佳答案
好吧,我从这篇文章中找到了答案 https://stackoverflow.com/a/38690930/5266568
在 application.properties 中添加以下配置修复了问题:
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
@Test
@Transactional
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);
}
关于spring-boot - org.hibernate.LazyInitializationException : failed to lazily initialize a collection of role: FQPropretyName, 无法初始化代理 - 没有 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46259878/
我是一名优秀的程序员,十分优秀!