gpt4 book ai didi

java - Spring 数据JPA : Check Exists Entity ID with @Query Annotation

转载 作者:行者123 更新时间:2023-11-30 07:21:50 26 4
gpt4 key购买 nike

我正在尝试制作一个简单的 JPQL,用于检查是否存在实体 ID。

我的 PlanDetail 实体:

Entity
@Table(name="T_PLAN_DETAIL")
public class PlanDetail extends AbstractProcessEntity {

@ManyToOne
@JoinColumn(name = "TERRITORY_ID")
private Territory territory;
...
}

我想使用断言来检查它:

Assert.isTrue(planDetailRepository.existsByTerritory(territoryId), "This territory exist with ID = " + territoryId);

我的 PlanDetail 实体:

public interface PlanDetailRepository extends CrudRepository<PlanDetail, Long> {

@Query("SELECT COUNT(e)>0 FROM PlanDetail e WHERE e.terrritoryId=?1")
boolean existsByTerritory(Long territoryId);
}

错误日志:

Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: terrritoryId of: test.domain.model.plan.PlanDetail [SELECT COUNT(e)>0 FROM test.domain.model.plan.PlanDetail e WHERE e.terrritoryId=?1]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:331) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]

感谢任何帮助。您能给我任何 JPQL 文档吗?谢谢。

最佳答案

假设您的领土实体有一个名为“id”的字段。

将您的查询更改为:

@Query("SELECT COUNT(e)>0 FROM PlanDetail e WHERE e.territory.id=?1")

关于java - Spring 数据JPA : Check Exists Entity ID with @Query Annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37435455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com