我收到一个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)的Collection中。尝试过使用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 有谁知道为什么会这样,是否有办法解决?任何帮助将不胜感激,小胡子gracias! :D
答案 0 :(得分:1)
无法使用以集合作为参数的构造函数表达式创建对象。
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")