Java 使用SpringBoot在Jpa中查询时出错

Java 使用SpringBoot在Jpa中查询时出错,java,spring,hibernate,jpa,spring-boot,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;

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

我的域类

@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);
}
@Transactional
公共接口UserDao扩展了crudepository{
用户getOneByUserIdAndType(int用户id,int类型);
}
堆栈跟踪

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
原因:org.springframework.data.mapping.PropertyReferenceException:找不到用户类型的属性UserId!
位于org.springframework.data.mapping.PropertyPath.(PropertyPath.java:75)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
位于org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
位于org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
位于org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
位于org.springframework.data.repository.query.parser.Part.(Part.java:76)
位于org.springframework.data.repository.query.parser.PartTree$OrPart.(PartTree.java:235)
位于org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)
位于org.springframework.data.repository.query.parser.PartTree$Predicate.(PartTree.java:353)
位于org.springframework.data.repository.query.parser.PartTree(PartTree.java:87)
位于org.springframework.data.jpa.repository.query.PartTreeJpaQuery(PartTreeJpaQuery.java:61)
位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:94)
位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:205)
位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:72)
位于org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor。(RepositoryFactorySupport.java:369)
位于org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:192)
位于org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
位于org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertieSet(RepositoryFactoryBeanSupport.java:225)
位于org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.AfterPropertieSet(JpaRepositoryFactoryBean.java:92)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 73多

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

您可以尝试名为
getOneByUser\uu-id和type
(请注意双精度
\uu
!)的存储库接口方法,或者尝试命名实例变量userId


另请参见。

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

您可以尝试名为
getOneByUser\uu-id和type
(请注意双精度
\uu
!)的存储库接口方法,或者尝试命名实例变量userId


另请参见。

getOneByUserIdAndType是一个Spring JPA数据查询方法(),它带有一些约定

UserIdAndType需要以下方法签名:

用户getOneByUserIdAndType(int用户ID,int类型)

查询是getOneBy。查询参数是字段userId将映射到字段userId,类型映射到字段type

现在是PropertyReferenceException。JPA数据无法将UserId映射到Entity字段user_id。您必须将Entity字段更改为此声明:

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

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

getOneByUserIdAndType是一个Spring JPA数据查询方法(),它带有一些约定

UserIdAndType需要以下方法签名:

用户getOneByUserIdAndType(int用户ID,int类型)

查询是getOneBy。查询参数是字段userId将映射到字段userId,类型映射到字段type

现在是PropertyReferenceException。JPA数据无法将UserId映射到Entity字段user_id。您必须将Entity字段更改为此声明:

@Column(name = "user_id")
private int userId;
应该仍然适合您,因为JPA仍然访问正确的列