@Entity
@Table(name="User")
public class User implements Serializable{
private String username; //Key
// Getters and setters
}
@Entity
@Table(name="POITable")
public class POI implements Serializable{
private Integer id; //Key
private String name;
// Getters and setters
}
@Entity
@Table(name="VisitedPlaceTable")
public class VisitedPlace implements Serializable{
private Integer id; //Key
private User user; //Many TO One relation
private POI poi; //Many TO One relation
// Getters and setters
}
POITable
===================
| id | name |
ــــــــــــ+ـــــ+
| 1 | poi name 1 |
-----+------------+
| 2 | poi name 2 |
===================
VisitedPlaceTable
=================================
| id | user_username | poi_id |
ــــــــ+ـــــــــــــــــ+ـــــ+
| 3 | xxxx@gmail.com | 1 |
-----+-----------------+--------+
| 4 | xxxx@gmail.com | 2 |
=================================
此原生查询
SELECT CONCAT('"',model.user_username,'","',**ARRAY_TO_STRING(ARRAY(SELECT DISTINCT visitedPlace.poi_id FROM VisitedPlaceTable visitedPlace WHERE visitedPlace.user_username = model.user_username),'|')**,'"','') FROM TourismPOIVisited model INNER join POITable poi on poi.id = model.poi_id WHERE model.user_username='xxxx@gmail.com' GROUP BY model.user_username
返回下一个结果(TRUE和EXPECTED结果:))
HQL中“xxxx@gmail.com”, “1 | 2”
但
SELECT CONCAT('\"',model.user.username,'\",\"',ARRAY_TO_STRING(ARRAY(SELECT DISTINCT visitedPlace.poi.id FROM VisitedPlace visitedPlace WHERE visitedPlace.user.username = model.user.username),'|'),'\"','\n') FROM VisitedPlace model WHERE model.user.username='xxxx@gmail.com' GROUP BY model.user.username
它开火
第1:70行:意外令牌: SELECT
那么如何使用 ARRAY_TO_STRING(ARRAY())或任何其他等效函数在HQL中执行此操作?