【问题标题】:Error in query in Jpa with SpringBoot使用 Spring Boot 在 Jpa 中查询时出错
【发布时间】:2015-09-28 22:29:45
【问题描述】:

我正在尝试从数据库中获取数据。但是我收到编译时错误。查询有什么问题以及如何解决?

我的域类

@Entity
@Table(name="user")
@Data
public class User {
    @JsonIgnore
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Column(name = "user_id")
    private int user_id;
    @Column(name = "type")
    private int type;
}

我的道课

@Transactional
public interface UserDao extends CrudRepository<User, Long> {
    User getOneByUserIdAndType(int user_id,int type);
}

堆栈跟踪

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property UserId found for type User!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:87)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:61)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:94)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:205)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:72)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:369)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:192)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 73 more

【问题讨论】:

    标签: java spring hibernate jpa spring-boot


    【解决方案1】:

    getOneByUserIdAndType 是一个带有一些约定的 Spring-JPA-Data 查询方法 (http://docs.spring.io/spring-data/jpa/docs/1.8.1.RELEASE/reference/html/#repositories.query-methods)。

    UserIdAndType 需要以下方法签名:

    用户 getOneByUserIdAndType(int userId, int type)

    查询是getOneBy。查询参数是字段 userId 将映射到字段 UserId 上并在字段 Type 上键入

    现在是 PropertyReferenceException。 JPA-Data 无法将 UserId 映射到 Entiy 字段 user_id。您必须将实体字段更改为此声明:

    @Column(name = "user_id")
    private int userId;
    

    应该仍然适合您,因为 JPA 仍然可以访问正确的列。

    【讨论】:

      【解决方案2】:

      由于您的存储库接口方法名为getOneByUserIdAndType,Spring 将查找名为UserId 的属性。它不存在,因此“No property UserId found for type User!”。

      您可以尝试使用名为 getOneByUser__IdAndType 的存储库接口方法(注意双精度 _!),或尝试命名实例变量 userId。

      另见this question on underscores in column names

      【讨论】:

        猜你喜欢
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        • 2020-08-13
        • 2021-10-13
        • 1970-01-01
        • 2021-09-15
        • 2022-01-24
        • 1970-01-01
        相关资源
        最近更新 更多