java Spring数据JPA:找不到类型的属性(字段名包含查询方法 predicate 关键字)

yr9zkbsy  于 2023-01-01  发布在  Java
关注(0)|答案(3)|浏览(202)

所以我一直在尝试把我的大脑围绕在这个错误上,所以我得到了一个相当标准的错误,我明白它告诉我什么,我只是不明白它是怎么得到这个错误的。

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property stored found for type StorageItemEntity! Did you mean 'storedIn'?
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:90) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:437) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:413) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:366) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at java.base/java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:330) ~[na:na]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:348) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:331) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:249) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177) ~[na:na]
    at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:250) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:383) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177) ~[na:na]
    at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[na:na]
    at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) ~[na:na]
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:na]
    at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) ~[na:na]
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:384) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:95) ~[spring-data-commons-2.4.8.jar:2.4.8]
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:89) ~[spring-data-jpa-2.4.8.jar:2.4.8]
    ... 62 common frames omitted

这是一个标准错误,对吧?好吧,它告诉我没有为我的实体找到属性stored。有道理,但我似乎不明白为什么,因为我没有定义stored。好吧,这是一个把它弄得一团糟的查询

@Repository
public interface StorageItemDao extends JpaRepository<StorageItemEntity, Long> {
    
    // All other methods omitted for sake of keeping it short
    public List<StorageItemEntity> findAllByStoredIn(StorageType storedIn);

}

这是StorageItemEntity类

@Entity
@Table(name = "storage_item")
public class StorageItemEntity implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -5373908574936516693L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private int id;
    
    @NotEmpty(message = "Storage item name can not be empty")
    @Size(max = 100, message = "Storage item name can not exceed 100 characters")
    @Column(name = "name")
    private String name;
    
    @Range(min = 0, message = "Amount in storage can not be less than 0")
    @Column(name = "amount")
    private int amount;
    
    @Range(min = 0, message = "Amount expected in storage per week can not be less than 0")
    @Column(name = "amount_expected")
    private int amountExpected;
    
    @NotNull(message = "Storage in type can not be empty")
    @Column(name = "stored_in")
    private StorageType storedIn;
    
    @Size(max = 500, message = Constants.ADDITIONAL_INFO_EXCEED)
    @Column(name = "additional_info")
    private String additionalInfo;
    
    @NotNull(message = "You need to have a vendor associated to this item")
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "vendor_id", referencedColumnName="id")
    private VendorEntity vendor;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "type_of_storage_id", referencedColumnName="id")
    private TypeOfStorageEntity typeOfStorage;

    // All getters, setters, and constructors are omitted
}

我还想指出的是,StorageType是一个非常标准的枚举。这不是错误,但它是一个很好的说明,以防万一。那么,如果我从来没有在我的JPA方法中定义过said stored,为什么我会得到“没有为类型StorageItemEntity找到属性'stored'”呢?
谢谢

wlzqhblo

wlzqhblo1#

看起来In是Spring查询派生使用的关键字,所以它需要一个stored属性,即参数中提供的In值。
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#appendix.query.method.predicate
您需要使用@Query注解来获得您想要的内容。

tp5buhyn

tp5buhyn2#

我认为这是JPA模型中列名和变量名的问题,如果你想使用findAllByStoredIn,你可以像这样使用@Query注解。
存储库类,如

@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {
      
    @Query("SELECT a FROM Account a WHERE a.userName = :username")
    Account findByUsername(@Param("username") String username);
}

Model类就像

@Getter
@Setter
@RequiredArgsConstructor
@Entity
@Table(name = "account")
public class Account {

    @Id
    @GeneratedValue
    @Column(name="user_id")
    private Long userId;

    @NotNull
    @UniqueUsername(message="Username already exists")
    @Size(min = 6, max = 255, message = "Username have to be grater than 6 characters")
    @Column(name="user_name",unique = true)
    private String userName;

    }
vshtjzan

vshtjzan3#

这就足够解决这个问题了。@Repository公共接口AccountRepository扩展了JpaRepository〈Account,Long〉{

@Query("SELECT a FROM Account a WHERE a.username = :username")
Account findByUsername(@Param("username") String username);

}

相关问题