Reputation: 4417
I am trying to get data from the database.But I am getting compile time error.What's wrong with the query and how to solve it?
My domain class
@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;
}
My dao class
@Transactional
public interface UserDao extends CrudRepository<User, Long> {
User getOneByUserIdAndType(int user_id,int type);
}
The 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
Upvotes: 0
Views: 1365
Reputation: 4968
getOneByUserIdAndType is a Spring-JPA-Data Query Method (http://docs.spring.io/spring-data/jpa/docs/1.8.1.RELEASE/reference/html/#repositories.query-methods) which comes with some conventions.
UserIdAndType needs the following method signature:
User getOneByUserIdAndType(int userId, int type)
The query is getOneBy. Query parameters are fields userId will be mapped on field UserId and type on field Type
Now for the PropertyReferenceException. JPA-Data can't map UserId on the Entiy field user_id. You will have to change the Entity fields to this declaration:
@Column(name = "user_id")
private int userId;
Should still work out for you because JPA still accesses the correct column.
Upvotes: 3
Reputation: 9705
Since your repository interface method is named getOneByUserIdAndType
, Spring will look for a property called UserId
. It doesn't exist, hence the "No property UserId found for type User!".
You might try an repository interface method named getOneByUser__IdAndType
(note the double _
!), or try naming the instance variable userId.
See also this question on underscores in column names.
Upvotes: 2