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
JavaBot7d 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
dan1st7d 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
ayylmao123xddOP7d 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
dan1st7d ago
if you really want to, you could generate the code
ayylmao123xdd
ayylmao123xddOP7d ago
hmmmmmmm
dan1st
dan1st7d ago
but you probably don't want to (at build time)
ayylmao123xdd
ayylmao123xddOP7d ago
ye that would need entity manager
dan1st
dan1st7d ago
or make a custom repository I meant generating the repository interface at compile-time
ayylmao123xdd
ayylmao123xddOP7d ago
yea that too my idea was to make a strategy for each class and basically loop over them all
dan1st
dan1st7d ago
but yeah just using EntityManager is probably easier
ayylmao123xdd
ayylmao123xddOP7d ago
and just add a query from each strategy and execute it
dan1st
dan1st7d ago
I think Spring probably allows you to get a list of all entity classes so you could use that
ayylmao123xdd
ayylmao123xddOP7d ago
or just grabbing a list of all the strategies square strategy etc etc into a map
dan1st
dan1st7d ago
whatever you prefer
ayylmao123xdd
ayylmao123xddOP7d ago
amazing ok gonna close
JavaBot
JavaBot7d ago
Post Closed
This post has been closed by <@452882919325827074>.

Did you find this page helpful?