Ork*_*zen 3 jpql spring-data-jpa spring-boot
我米试图写使用查询select max及where与@Query
以下不起作用,我该如何解决?
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
interface IntermediateInvoiceRepository extends JpaRepository<Invoice, String> {
@Query("SELECT max(i.sequence) " +
"FROM Invoice as i " +
"WHERE i.fleetId = :fleetId" +
" AND i.sequence IS NOT NULL")
Long findMaxSequence(@Param("fleetId") String fleetId);
}
Run Code Online (Sandbox Code Playgroud)
我遇到了另一个答案,但它明确地使用了实体管理器,它不一样
如何在 JPA 2.0 中使用 where 子句编写 MAX 查询?
错误是:
2018-09-14T09:27:57,180Z [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.data.mapping.PropertyReferenceException: No property findMaxSequence found for type Invoice!
Run Code Online (Sandbox Code Playgroud)
发票类(为简洁起见简化):
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "invoices", indexes = {
@Index(name = "IDX_FLEET", columnList = "fleetId", unique = false)
,
@Index(name = "IDX_USERSSS", columnList = "userId", unique = false)
,
@Index(name = "IDX_TIME", columnList = "invoiceDate", unique = false)
,
@Index(name = "IDX_SEQUENCE", columnList = "sequence", unique = false)
})
@JsonIgnoreProperties(ignoreUnknown = true)
public class Invoice implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(columnDefinition = "CHAR(36)")
@Id
private String id;
@Column
private long sequence;
Run Code Online (Sandbox Code Playgroud)
...
更新:
也许可以使用 findOne 在序列列上使用 DESC 排序来解决?
@Query("SELECT i.sequence " + "FROM Invoice as i " + "WHERE i.fleetId = :fleetId " + "ORDER BY i.sequence DESC ") Long getMaxSequence(@Param("fleetId") String fleetId);
但我需要以某种方式将结果集限制为 1
更新 2:
修复了import org.springframework.data.jpa.repository.Query;仍然出错的问题
由于您使用的是 JPA 存储库,请使用:
org.springframework.data.jpa.repository.Query
Run Code Online (Sandbox Code Playgroud)
注释而不是
org.springframework.data.mongodb.repository.Query
Run Code Online (Sandbox Code Playgroud)
您可以创建查询方法,而无需使用 @Query 批注,例如:
发票 findFirstByFleetIdOrderBySequenceDesc(String fleetId);
返回您需要的发票。
| 归档时间: |
|
| 查看次数: |
11417 次 |
| 最近记录: |