AssertionError running SpringBoot unit test using Repository Spring JPA for entities with EntityGraph annotation

73 Views Asked by At

after upgraded from Java11 to Java17 some unit test has becoming failing. I'm using Java 17 locally existing VM temurin-17.jdk with Springboot 3.0.2 and Junit 5.9.2.

This is the unit test failing now:

public class MyServiceTests {

    @Autowired
private MyService myService;

save some data into test database

    @Test
public void doSomethingTest() {
RequestBody body = new RequestBody("...");

B b = myService.doSomething(body);

assertEquals(...);
    }

}

This is the service:

//....

@Override
public B doSomething(RequestBody request) {

     //Status Ok
B b = new B();

List<UUID> aList = request.getAelements();

Check A elements on the database
Map<UUID, A> aMaps = aRepo.findByUuidIn(aList)
             .stream()
             .collect(Collectors.toMap(A::getUuid, a -> a));

b.setAMaps(aMaps);
     //....
return b;

}

This is the repository:

@EntityGraph(value = "a-with-b")
List<A> findByUuidIn(List<UUID> uuids);

This is the entity 1:

@Entity
@NamedEntityGraph(
name = "a-with-b",
attributeNodes = { @NamedAttributeNode(value = "b") }
)
@Table( name = "a", uniqueConstraints = @UniqueConstraint(columnNames = {"someColumnUnique"}))
@AllArgsConstructor @NoArgsConstructor
@Getter @Setter
public class A {

    //...

    @OneToMany(mappedBy = "a", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
    @MapKey(name = "id")
private Map<UUID, B> bMaps;

    //...
}

This is the entity 2:

@Entity
@Table( name = "b")
@NoArgsConstructor
@Getter @Setter
public class B {

    // ...

    @ManyToOne(fetch = FetchType.EAGER)
    @NotNull(message = "A element must not be null")
    @JoinColumn(name = "a_id", insertable = false, updatable = false)
private A a;

    // ...
}

And finally this is the error:

2023-08-30 10:44:21.406  INFO  StatisticalLoggingSessionEventListener-end():261 - \[ Session Metrics {
0 nanoseconds spent acquiring 0 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
0 nanoseconds spent preparing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections);
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)
} \]

java.lang.AssertionError
at org.hibernate.sql.results.internal.StandardEntityGraphTraversalStateImpl.traverse(StandardEntityGraphTraversalStateImpl.java:71)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.addFetch(BaseSqmToSqlAstConverter.java:6937)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitFetches(BaseSqmToSqlAstConverter.java:7099)
at org.hibernate.sql.results.graph.AbstractFetchParent.afterInitialize(AbstractFetchParent.java:32)
at org.hibernate.sql.results.graph.entity.AbstractEntityResultGraphNode.afterInitialize(AbstractEntityResultGraphNode.java:100)
at org.hibernate.persister.entity.AbstractEntityPersister.createDomainResult(AbstractEntityPersister.java:1300)
at org.hibernate.query.sqm.sql.internal.AbstractSqmPathInterpretation.createDomainResult(AbstractSqmPathInterpretation.java:55)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.lambda$visitSelection$25(BaseSqmToSqlAstConverter.java:2053)
at java.base/java.util.Collections$SingletonList.forEach(Collections.java:4966)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelection(BaseSqmToSqlAstConverter.java:2048)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitSelectClause(BaseSqmToSqlAstConverter.java:1951)
at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:1819)
...

Thanks

0

There are 0 best solutions below