2015-07-10 85 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仍會訪問正確的列。