3

I have problem with nested embedded objects in Hibernate 6.1.4.Final (Java 17, Spring-Data 3.0.0-M5) which I stripped down to the following lines of code

@Entity
public class Foo{
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false)
    private Long id;
    
    @Embedded
    private Bar1 embeddedBar1;
//Getters and Setters omitted 
}
@Embeddable
public class Bar1 {
    
    @Embedded
    private Bar2 embeddedBar2;
//Getters and Setters omitted 
}
@Embeddable
public class Bar2 {
    
    private String bar;
    
    private String test;
//Getters and Setters omitted 
}

The corresponding test looks like

@DataJpaTest
public class FooRepositoryTest {
    
    @Autowired
    private TestEntityManager em;
    
    @Test
    public void myTest() throws Exception {
        Foo foo = em.persist(new Foo());
        assertThat(foo).isNotNull();
        em.merge(foo);
        
        TypedQuery<Foo> qry = em.getEntityManager().createQuery("SELECT f from Foo f where f.id=:id", Foo.class);
        qry.setParameter("id", foo.getId());
        EntityGraph<Foo> g = em.getEntityManager().createEntityGraph(Foo.class);
        //Setting the EntityGraph will lead to an error
        qry.setHint("jakarta.persistence.fetchgraph", g);
        List<Foo> ff = qry.getResultList();
        assertThat(ff).isNotEmpty();
    }
}

The resulting stacktrace will lead to an ArrayIndexOutOfBoundsException

Caused by: org.hibernate.HibernateException: Could not generate fetch : testfoo.Foo(f).embeddedBar1.embeddedBar2 -> test
    at app//org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.buildFetch(BaseSqmToSqlAstConverter.java:7207)
    at app//org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.addFetch(BaseSqmToSqlAstConverter.java:7059)
    at app//org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.lambda$visitFetches$130(BaseSqmToSqlAstConverter.java:7109)
    at java.base@17/java.util.ArrayList.forEach(ArrayList.java:1511)
    at app//org.hibernate.metamodel.mapping.internal.EmbeddableMappingTypeImpl.visitAttributeMappings(EmbeddableMappingTypeImpl.java:726)
    at app//org.hibernate.metamodel.mapping.internal.EmbeddableMappingTypeImpl.visitSubParts(EmbeddableMappingTypeImpl.java:736)
    at app//org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping.visitSubParts(EmbeddedAttributeMapping.java:292)
    at app//org.hibernate.sql.results.graph.FetchableContainer.visitFetchables(FetchableContainer.java:35)
    at app//org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.visitFetches(BaseSqmToSqlAstConverter.java:7109)
    at app//org.hibernate.sql.results.graph.AbstractFetchParent.afterInitialize(AbstractFetchParent.java:32)
    at app//org.hibernate.sql.results.graph.embeddable.internal.EmbeddableFetchImpl.<init>(EmbeddableFetchImpl.java:75)
    at app//org.hibernate.metamodel.mapping.internal.EmbeddedAttributeMapping.generateFetch(EmbeddedAttributeMapping.java:238)
    at app//org.hibernate.sql.results.graph.FetchParent.generateFetchableFetch(FetchParent.java:105)
    at app//org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.buildFetch(BaseSqmToSqlAstConverter.java:7148)
    ... 126 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
    at org.hibernate.metamodel.mapping.internal.BasicAttributeMapping.generateFetch(BasicAttributeMapping.java:310)
    at org.hibernate.sql.results.graph.FetchParent.generateFetchableFetch(FetchParent.java:105)
    at org.hibernate.query.sqm.sql.BaseSqmToSqlAstConverter.buildFetch(BaseSqmToSqlAstConverter.java:7148)
    ... 139 more

Thanks in advance for any help or contribution to this problem.

GroppP2010
  • 31
  • 1
  • I can't explain why but `qry.setHint(":jakarta.persistence.loadgraph", g);` should fix this issue. We have similar issue in our code but we use `@IdClass` with entities and `@EntityGraph` for queries. Looks like backward compatibility problem and we need someone from Hibernate team to take a look into this. – tjuchniewicz Dec 07 '22 at 13:36
  • 1
    Thanks for your reply. Yes, you are right "loadgraph" solves the problem though at the cost of a slight difference in business logic. In any case in my opinion, I think this is a bug in this version (6.1.4.Final) of Hibernate. – GroppP2010 Dec 09 '22 at 09:18
  • Did you try the latest version 6.1.7/6.2.0.CR2 yet? If so, and you still have the problem, please create an issue in the issue tracker(https://hibernate.atlassian.net) with a test case(https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) that reproduces the issue. – Christian Beikov Feb 07 '23 at 13:09
  • I tried version 6.1.7 and everything worked as expected. Problem solved! – GroppP2010 Feb 13 '23 at 11:14

0 Answers0