fetch subclasses and their associations with one query

demo for simplicity and to avoid being sued :GnuTrolling: so the question is whether its possible to fetch all the associations to their respective classes when calling the find all method without eager fetching and avoiding n+1 and without extra queries for getting the associations
public interface ShapeRepository extends JpaRepository<Shape, Long> {
Page<Shape> findAll();
}
public interface ShapeRepository extends JpaRepository<Shape, Long> {
Page<Shape> findAll();
}
@DiscriminatorValue("SQUARE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Square extends Shape {
@OneToMany(mappedBy = "square", fetch = FetchType.LAZY)
private Set<Vertices>;
}
@DiscriminatorValue("SQUARE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Square extends Shape {
@OneToMany(mappedBy = "square", fetch = FetchType.LAZY)
private Set<Vertices>;
}
@DiscriminatorValue("TRIANGLE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Triangle extends Shape {
@OneToMany(mappedBy = "triangle", fetch = FetchType.LAZY)
private Set<Angles>;
}
@DiscriminatorValue("TRIANGLE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Triangle extends Shape {
@OneToMany(mappedBy = "triangle", fetch = FetchType.LAZY)
private Set<Angles>;
}
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public abstract class Shape {
@Id
private long id;

private String name;
}
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public abstract class Shape {
@Id
private long id;

private String name;
}
@DiscriminatorValue("RECTANGLE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Rectangle extends Shape {
@OneToMany(mappedBy = "rectangle", fetch = FetchType.LAZY)
private Set<Sides>;
}
@DiscriminatorValue("RECTANGLE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Rectangle extends Shape {
@OneToMany(mappedBy = "rectangle", fetch = FetchType.LAZY)
private Set<Sides>;
}
16 Replies
JavaBot
JavaBot2mo ago
This post has been reserved for your question.
Hey @ayylmao123xdd! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st2mo ago
Can you use a LEFT JOIN? something like SELECT FROM Shape s LEFT JOIN FETCH s.vertices ve LEFT JOIN FETCH s.angles an LEFT JOIN FETCH s.sides si
ayylmao123xdd
ayylmao123xddOP2mo ago
probably what in the case where i cant edit the repository but i want to add another class like a circle maybe but i cant change the repositorys code (stupid requirements)
dan1st
dan1st2mo ago
if you really want to, you could generate the code
ayylmao123xdd
ayylmao123xddOP2mo ago
hmmmmmmm
dan1st
dan1st2mo ago
but you probably don't want to (at build time)
ayylmao123xdd
ayylmao123xddOP2mo ago
ye that would need entity manager
dan1st
dan1st2mo ago
or make a custom repository I meant generating the repository interface at compile-time
ayylmao123xdd
ayylmao123xddOP2mo ago
yea that too my idea was to make a strategy for each class and basically loop over them all
dan1st
dan1st2mo ago
but yeah just using EntityManager is probably easier
ayylmao123xdd
ayylmao123xddOP2mo ago
and just add a query from each strategy and execute it
dan1st
dan1st2mo ago
I think Spring probably allows you to get a list of all entity classes so you could use that
ayylmao123xdd
ayylmao123xddOP2mo ago
or just grabbing a list of all the strategies square strategy etc etc into a map
dan1st
dan1st2mo ago
whatever you prefer
ayylmao123xdd
ayylmao123xddOP2mo ago
amazing ok gonna close
JavaBot
JavaBot2mo ago
Post Closed
This post has been closed by <@452882919325827074>.

Did you find this page helpful?