使用SpringBoot在Jpa中查询时出错

时间:2015-07-10 08:44:13

标签: java spring hibernate jpa spring-boot

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

我的域名

@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);
}

stacktrace

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

2 个答案:

答案 0 :(得分:3)

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

UserIdAndType需要以下方法签名:

User getOneByUserIdAndType(int userId,int type)

  

查询是getOneBy。查询参数是字段userId将映射到字段UserId并在字段类型

上键入

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

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

仍然可以为您解决问题,因为JPA仍然可以访问正确的列。

答案 1 :(得分:2)

由于您的存储库接口方法名为getOneByUserIdAndType,因此Spring将查找名为UserId的属性。它不存在,因此“找不到类型用户的UserId属性!”。

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

另见this question on underscores in column names