java - 无法通过 @Query 将集合传递给构造函数
问题描述
我收到一个 SQL 异常
java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as col_7_0_ from locales offerlocal0_ cross join offers offer2_ inner join offer' at line 1
在调用存储库方法时
@Query("SELECT DISTINCT new com.greenflamingo.staticplus.model.catalog.dto.OfferGet(ol.root.id,ol.title "
+ ",ol.description,dl.name,ol.root.price,ol.root.currency,ol.root.visible,ol.root.images) "
+ "FROM OfferLocale ol,DescriptorLocale dl "
+ "WHERE ol.root.webfront.id = (:webId) AND ol.culture.languageCode = (:langCode) "
+ "AND dl.culture.languageCode = (:langCode) "
+ "AND ol.root.category = dl.root")
Page<OfferGet> findAllWebfrontLocalized(@Param("webId")int webfrontId,@Param("langCode")String langCode,Pageable pageable );
我已将问题缩小到我试图传递给构造函数(ol.root.images)的集合。尝试使用 List (它给了我一个构造函数不匹配)和使用 Set (与此处显示的错误相同)这是我正在使用的 bean
public class OfferGet implements Serializable{
private static final long serialVersionUID = 6942049862208633335L;
private int id;
private String title;
private String shortDescription;
private String price;
private String category;
private boolean visible;
private List<Image> images;
public OfferGet(String title, String category) {
super();
..........
}
public OfferGet() {
super();
}
public OfferGet(int id, String title, String description
, BigDecimal price
,String currency,
boolean visible) {
.........
}
public OfferGet(int id, String title, String description,String category
, BigDecimal price
,String currency,
boolean visible,
Collection<Image> images) {
..........
}
}
我正在使用 java 11、mariaDb 和 Springboot 2.0.5 有谁知道为什么会发生这种情况以及是否有任何解决方法?任何帮助将不胜感激,小胡子感谢!:D
解决方案
无法使用将集合作为参数的构造函数表达式创建对象。
SQL 查询的结果始终是一个表。
原因是标识变量使它们代表实例,而不是集合。
此外,您不能返回 root.images,您必须加入 OneToMany 关系,然后您不再拥有集合,而是拥有每个属性。
查询的结果将是笛卡尔积。
这是一个正确的查询:
@Query("SELECT DISTINCT new com.greenflamingo.staticplus.model.catalog.dto.OfferGet(ol.root.id,ol.title "
+ ",ol.description,dl.name,ol.root.price,ol.root.currency,ol.root.visible, image) "
+ "FROM OfferLocale ol,DescriptorLocale dl "
+ "JOIN ol.root.images image
+ "WHERE ol.root.webfront.id = (:webId) AND ol.culture.languageCode = (:langCode) "
+ "AND dl.culture.languageCode = (:langCode) "
+ "AND ol.root.category = dl.root")
推荐阅读
- android - 有没有办法限制特定应用程序使用互联网?
- javascript - 如何在 javascript 中找到执行上下文对象名称?
- java - 谷歌的 Spring Boot oauth 回调
- java - Rest Api 对成功和失败有不同的响应
- javascript - setTimeout 是如何工作的?
- java - https 在生产模式下运行需要很长时间
- datasource - Redash如何更改仪表板数据源?
- typescript - 我如何在 Typescript 中编写静态 Json 对象
- go - 试图将数据解组到golang中的接口中
- android - RecyclerView 使用 ConstraintSet 扩展项目 - 扩展错误的项目/行