I need a jpql request for my Spring repository interface method to get all messages for a given semester.
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(cascade = CascadeType.MERGE)
@JoinTable
(
name = "semester_post",
joinColumns = {@JoinColumn(name = "semester_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "post_id", referencedColumnName = "id", unique = true)}
)
private List<PostEntity<?>> posts = new ArrayList<>();
PostEntity does not have a semester reference, and I do not want to add it because I plan to use this PostEntity for other things than the semester. Maybe I will have another class (let a group) that will also have OneToMany of PostEntity (e.g. in Semester)
So, how to write this SQL query as JPQL code?
select * from posts join semester_post on semester_post.post_id = posts.id where semester_post.semester_id = 1;
My repository
public interface PostRepository extends JpaRepository<PostEntity, Long> {
String QUERY = "SELECT p FROM PostEntity p ... where semester = :semesterId";
@Query(MY_QUERY)
public List<PostEntity> findBySemesterOrderByModifiedDateDesc(@Param("semesterId") Long semesterId);
source
share