0

I have 2 classes

Collections

@Entity
@NamedEntityGraph(
        name = "cars-graph",
        attributeNodes = @NamedAttributeNode("cars"))
public class Collections {
    @Id
    @ReturnInsert(returnOnly=true)
    private Long id;

    @ManyToMany(cascade = ALL)
    @JoinTable(name="CarsCollections",
            joinColumns= @JoinColumn(name="COLLECTIONS_ID", referencedColumnName="ID"),
            inverseJoinColumns=
                    {@JoinColumn(name = "MODELSRANGE_ID", referencedColumnName = "MODELSRANGE_ID"),
                            @JoinColumn(name = "MODELS_ID", referencedColumnName = "MODELS_ID"),
                            @JoinColumn(name = "TYPES_ID", referencedColumnName = "TYPES_ID")
                    }
    )
    public List<Cars> cars;

and Cars

@Entity(name = "Cars")
public class Cars {

    @EmbeddedId
    private CarsId id;


    @ManyToOne(fetch = FetchType.EAGER)
    @MapsId("modelsRangeid")
    @JoinColumn(name = "MODELSRANGE_ID")
    private ModelsRange modelsrange;

    @ManyToOne(fetch = FetchType.EAGER)
    @MapsId("modelsid")
    @JoinColumn(name = "MODELS_ID")
    private Models models;

    @ManyToOne(fetch = FetchType.EAGER)
    @MapsId("typesid")
    @JoinColumn(name = "TYPES_ID")
    private Types types;

    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
    @JoinColumn(name = "MAKE")
    private Makes makes;

When i try to get Collections with Cars

EntityGraph entityGraph = em.getEntityGraph("cars-graph");
        Collections collections = em.createQuery("select r from Collections r where r.id = :id", Collections.class)
                .setParameter("id", 1L)
                .setHint("javax.persistence.fetchgraph", entityGraph)
                .getSingleResult();
        System.out.println(collections.getCars());

I get {IndirectList: not instantiated}. Does not give errors, but Cars does not load.

If you add to the Collections entity for the Cars field "EAGER"

...
@ManyToMany(cascade = ALL, fetch = FetchType.EAGER)
    @JoinTable(name="CarsCollections",
...

It works. But then Cars will always load.

I use Jakarta EE9 and Eclipselink

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Have you read in this Collections instance prior to executing the query - if so, do you see different behaviour when executing the query if the app just starts up the first time? Otherwise - do you see other attributes with Collections being fetched? You only have 2 shown, but you might check by adding other basics and verifying if they are fetched. – Chris Jan 13 '22 at 15:16

1 Answers1

0

Problem in javax name. In Jakarta EE need to use:

...
.setHint("jakarta.persistence.loadgraph", entityGraph)
...