我在spring-jpa-project中有一个多对多关系,但是我找不到一种方法来让我的Query执行它应该做的事情。也许任何人都可以帮忙...
我有三个实体:
用户:
@Entity
public class User {
@Id private String userName;
}
UserSettings
@Entity
public class UserSettings {
@Id private String userName;
private EMailDeliveryType emailDelivery;
}
和事件
@Entity
public class Event {
@Id
private Long id;
private Long dailyMailSent;
@ManyToMany(fetch = EAGER)
private Set<User> recipients;
}
我的存储库如下:
@Repository
public interface StateChangeEventRepository extends JpaRepository<Event, Long> {
@Modifying
@Query(
"UPDATE Event s SET s.dailyMailSent = extract(EPOCH FROM CURRENT_TIMESTAMP) "
+ "WHERE s.dailyMailSent IS NULL "
+ "AND ("
+ " s.recipients IS EMPTY OR "
+ " (SELECT user FROM User user JOIN UserSettings settings ON "
+ " settings.userName = user.userName WHERE settings.emailDelivery = 25) "
+ " NOT MEMBER OF s.recipients"
+ " )")
void markUnprocessableDailyEventsAsFinished();
}
所以我要做的是标记查找所有具有DailyMailSent = null的事件,或者它们没有接收者,或者没有任何接收者的userSetting具有特定的emailDelivery。 此查询不起作用,因为子查询返回了多个用户,然后我遇到错误:
ERROR: more than one row returned by a subquery used as an expression
有人知道如何解决此问题吗?我尝试了几种选择,但是没有一个起作用。
提前感谢,马蒂亚斯