Java Spring数据未执行查询
我对查询执行有问题 以下是我的结构: 产品价格Java Spring数据未执行查询,java,spring,Java,Spring,我对查询执行有问题 以下是我的结构: 产品价格 @Data @Entity @Table(name="\"Product_price\"") public class ProductPrice { @Id @SequenceGenerator(name="product_price_id_seq", sequenceName="product_price_id_seq", allocationSize=1) @Generat
@Data
@Entity
@Table(name="\"Product_price\"")
public class ProductPrice {
@Id
@SequenceGenerator(name="product_price_id_seq",
sequenceName="product_price_id_seq",
allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator="product_price_id_seq")
private long id;
private Float price;
private Date date_from;
@ManyToOne
private Product product;
}
这是我的产品价格表
@Data
public class ProductPriceDto implements Serializable {
public Float price;
public Date date;
}
这是我的价格存储库
public interface PriceRepository extends CrudRepository<ProductPrice, Long> {
public static final String FIND_LAST_PRICE = "SELECT p.price, MAX(p.date_from) FROM ProductPrice p WHERE p.product = ?1 GROUP BY p.product";
@Query(FIND_LAST_PRICE)
ProductPriceDto findPrice(@Param(value = "product") Long product);
@Override
List<ProductPrice> findAll();
}
您的查询方法需要
Long
作为方法参数,但在JPQL查询中,您需要产品
将查询方法参数更改为
产品
您的查询方法需要长
作为方法参数,但在JPQL查询中,您需要产品
将查询方法参数更改为
产品
产品价格
模型类具有产品
属性,而不是长产品
,因此存在该错误
您应该在下面更改您的价格存储库
public interface PriceRepository extends CrudRepository<ProductPrice, Long> {
public static final String FIND_LAST_PRICE = "SELECT p.price, MAX(p.date_from) FROM ProductPrice p WHERE p.product = ?1 GROUP BY p.product";
@Query(FIND_LAST_PRICE)
List<ProductPrice> findPrice(@Param(value = "product") Product product);
@Override
List<ProductPrice> findAll();
}
public interface PriceRepository扩展了crudepository{
公共静态最终字符串FIND_LAST_PRICE=“从ProductPrice p中选择p.PRICE,MAX(p.date_from),其中p.product=?按p.product分组”;
@查询(查找上次价格)
列出findPrice(@Param(value=“product”)产品);
@凌驾
列出findAll();
}
ProductPrice
模型类有一个Product-Product
属性,而不是Long-Product
,因此您会出现该错误
您应该在下面更改您的价格存储库
public interface PriceRepository extends CrudRepository<ProductPrice, Long> {
public static final String FIND_LAST_PRICE = "SELECT p.price, MAX(p.date_from) FROM ProductPrice p WHERE p.product = ?1 GROUP BY p.product";
@Query(FIND_LAST_PRICE)
List<ProductPrice> findPrice(@Param(value = "product") Product product);
@Override
List<ProductPrice> findAll();
}
public interface PriceRepository扩展了crudepository{
公共静态最终字符串FIND_LAST_PRICE=“从ProductPrice p中选择p.PRICE,MAX(p.date_from),其中p.product=?按p.product分组”;
@查询(查找上次价格)
列出findPrice(@Param(value=“product”)产品);
@凌驾
列出findAll();
}
这很好,但导致了另一个问题:SQLGrammarException:无法提取ResultSet],根本原因为org.postgresql.util.PSQLException:错误:“productpri0.price”列必须出现在GROUP BY子句中或用于聚合函数:(@uksz您可能想问一个单独的问题。我想这是因为您对该属性有一个@ManyToOne
关系,因此这意味着您对单个产品有许多价格
结果,我知道。有没有其他方法可以通过max(date\u from)获得单个产品的单一价格?@uksz,你最好为此再问一个问题,正如Wim deblauwet所建议的那样。这个问题解决得很好,但会导致另一个问题:SQLGrammarException:无法提取结果集]和根本原因org.postgresql.util.PSQLException:错误:列“productpri0.price”必须出现在GROUP by子句中或用于聚合函数:(@uksz您可能想问一个单独的问题。我想这是因为您对该属性有一个@ManyToOne
关系,因此这意味着您对单个产品有许多价格
结果,我知道。有没有其他方法可以通过max(date\u from)获得单个产品的单一价格“?@uksz,你最好再问一个问题,就像维姆·德布劳韦建议的那样。”