EntityGraph does not fetch 4-level data after migrating to Hibernate 6

Hi,
I migrated hibernate 5.6.14.Final to 6.1.7.Final and I noticed a problem with entityGraph.

I have entities:

@Entity
public class Company {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Factory factory;
}
@Entity
public class Factory {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private CarFactory carFactory;
}
public class CarFactory {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "carFactory", cascade = CascadeType.ALL, orphanRemoval = true)
    Set<Car> cars = new HashSet<>();
}
@Entity
public class Car {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(mappedBy = "car", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    private Set<Feature> features = new HashSet<>();

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private CarFactory carFactory;

    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Info info;
}
@Entity
public class Feature {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    private Car car;

    @OneToOne(fetch = FetchType.LAZY)
    private Factory factory;
}
@Entity
public class Info {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

When I want to fetch all these entities in one query using @EntityGraph :

@Query("select c from Company c where c.id = :id")
    @EntityGraph(attributePaths = {"factory", "factory.carFactory", "factory.carFactory.cars",
            "factory.carFactory.cars.features","factory.carFactory.cars.info"
    })
    Optional<Company> findByIdCustomEntityGraph(long id);

or using fetchGraph hint:

@Transactional(readOnly = true)
    public Company getFetchGraphHint(long id) {
        var companyGraph = entityManager.createEntityGraph(Company.class);
        var factoryGraph = companyGraph.addSubgraph("factory");
        var carFactoryGraph = factoryGraph.addSubgraph("carFactory");
        var carsGraph = carFactoryGraph.addSubgraph("cars");
        carsGraph.addAttributeNodes("features", "info");
        return entityManager.createQuery("select c from Company c where c.id = :id", Company.class)
                .setParameter("id", id)
                .setHint(SpecHints.HINT_SPEC_FETCH_GRAPH, companyGraph)
                .getSingleResult();
    }

The features and info data are not fetched. But with Hibernate 5.6.14 everything works. This is a blocker for migrating to hibernate 6 in my company.

I created a bug: [HHH-16204] - Hibernate JIRA, but for more than a month nothing happened, no comment, no question. So I don’t know if I should wait patiently or am I doing something wrong and it’s not a bug?

It seems most people decided to wait very long until they gave Hibernate 6 a try and now we have a lot of bug reports. You didn’t hear anything yet because we have limited resources. We will look into the report eventually, but if this is so important to you right now, consider contributing.

1 Like

Solution:
set property:
spring.jpa.properties.hibernate.max_fetch_depth

because the default value is 2.