Hibernate Where子句中的SQLSyntaxErrorException

时间:2019-05-13 21:27:03

标签: java hibernate

我正在尝试使用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

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 )") 是那些列的名称。