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