[英]Error in query in 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
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無法在Entiy字段user_id上映射UserId。 您必須將Entity字段更改為以下聲明:
@Column(name = "user_id")
private int userId;
應該仍然可以為您解決問題,因為JPA仍會訪問正確的列。
由於您的存儲庫接口方法名為getOneByUserIdAndType
,因此Spring將查找名為UserId
的屬性。 它不存在,因此“找不到類型為User!的屬性UserId!”。
您可以嘗試使用名為getOneByUser__IdAndType
的存儲庫接口方法(請注意雙_
!),或嘗試命名實例變量userId。
另請參見有關列名稱中下划線的問題 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.