J'essaie une association bidirectionnelle ManyToOne utilisant SpringBoot, SpringDataJpa avec un test unitaire utilisant SpringBootTest. Cependant, le test échoue avec la trace de pile illustrée ci-dessous. Cependant, je suis incapable de trouver la raison. Tous les pointeurs seront utiles
En dessous de Spring Boot JUnit, le test pour ManyToOne Bi-Directional Association échoue.
@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);
}
Exception 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)
Entité propriétaire ManyToOne
@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;
}
}
* Entité non propriétaire OneToMany avec mappedBy *
@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);
}
}
Eh bien, j'ai trouvé la réponse de ce post https://stackoverflow.com/a/38690930/5266568
L'ajout de la configuration ci-dessous dans application.properties a résolu le problème:
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
Une autre solution qui a fonctionné pour moi, mais veuillez noter que j'ai encore besoin de comprendre comment cela a vraiment résolu le problème
@Transactional garantit que tous les appels de méthode de cette méthode de test se déroulent dans la même limite.
@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);
}