2015-07-10 83 views
0

我想从数据库中获取数据。但是我得到编译时错误。查询出现了什么问题以及如何解决它?Jpa with SpringBoot在查询时出错

我的领域类

@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 

回答

2

由于您的仓库接口方法命名为getOneByUserIdAndType,Spring就会查找一个名为UserId属性。它不存在,因此“为User类型找到没有属性UserId”。

您可以尝试一个名为getOneByUser__IdAndType的存储库接口方法(请注意双重的_!),或尝试命名实例变量userId。请参阅this question on underscores in column names

3

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

UserIdAndType需要以下方法签名:

用户getOneByUserIdAndType(INT用户id,整型)

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

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

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

仍然应该为您解决问题,因为JPA仍会访问正确的列。