我正在尝试使用Where子句过滤掉空的实体。
即
@Where(clause = "size(userTracking) > 0 OR size(userResults) > 0")
在
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "user_id", insertable = false, updatable = false)
@LazyCollection(LazyCollectionOption.FALSE)
@Where(clause = "size(userTracking) > 0 OR size(userResults) > 0")
private List<UserSession> userSessions = new ArrayList<UserSession>();
在UserSessions中
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "user_session_id", updatable = false)
@LazyCollection(LazyCollectionOption.FALSE)
private List<UserResult> userResults = new ArrayList<UserResult>();
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "user_session_id", updatable = false)
@LazyCollection(LazyCollectionOption.FALSE)
private List<UserTracking> userTracking = new ArrayList<UserTracking>();
但是我遇到的错误是
Caused by: java.sql.SQLSyntaxErrorException: FUNCTION database.size does not exist
是否可以通过这种方式使用@Where子句?
它不是重复的
HQL Query to check if size of collection is 0 or empty
我明确地说.size()在这里不起作用。
.size也不起作用
Java.sql.SQLSyntaxErrorException: Unknown column 'userTracking.size' in 'where clause'
我也尝试过'不为空'
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'usersessio0_.EMPTY OR usersessio0_.userResults IS usersessio0_.EMPTY) and users' at line 1
答案 0 :(得分:1)
DbSet<TitanHistoryRow>
定义了 SQL WHERE子句,而不是HQL WHERE子句,因此您需要编写:
@Where
@Where(clause = "user_session_id IN ( SELECT xxx FROM UserTracking ) OR user_session_id IN ( SELECT xxx FROM UserResult )")
是那些列的名称。