Spring Data JPA: No property foo found for type Bar

关于这个异常,可能有很多原因,

但这个本人遇到的情况解决方案实在是无语,

应该很快会在未来版本中修复,

写出来备忘一下,以作参考。


环境:

jdk 1.7 + mvn 3.x + Spring Boot 1.1.8.RELEASE+ Spring Data JPA 1.1.8.RELEASE


项目结构:

一个简单的基于spring boot 的 mvn 包结构


代码:

Customer 实体类

@Entity
public class Customer {

    private int id;
    private String name;
    private int age;

    public Customer() {
    }

    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    
}

CustomerDao 接口类,

继承 CrudRepository,使用Spring Data JPA已提供的通用CRUD方法,还可以追加部分自定义查询方法(不需要写实现代码),可以提高开发效率。

继承 CustomerDaoCustom,追加自定义查询方法,可以自己实现那些目前框架不提供的功能。

public interface CustomerDao extends CrudRepository<Customer,Integer>, CustomerDaoCustom {

    //根据getBy后面的属性名查询
    List<Customer> getByName(String name);

    //根据query语句查询
    @Query(value = "select c from Customer c where c.age > :age")
    List<Customer> getByMinAge(@Param("age") int minAge);

}

CustomerDaoCustom 自定义接口类,用于追加自定义查询方法(自己写实现代码)。

public interface CustomerDaoCustom {

    /**
     * 根据属性名,属性值查询
     * @param prop 属性名
     * @param value 属性值
     * @return 符合条件的 Customer list 集合
     */
    List<Customer> getByProp(String prop, Object value);

}

CustomerDaoCustomImpl 自定义接口实现类,用于给出具体实现。

public class CustomerDaoCustomImpl implements CustomerDaoCustom {

    @PersistenceContext//Expresses a dependency on a container-managed EntityManager and its associated persistence context.
    private EntityManager em;
    public void setEm(EntityManager em) {
        this.em = em;
    }

    @Override
    public List<Customer> getByProp(String prop, Object value) {
        String ql = "select c from Customer as c where c."+prop+" = :value";
        Query query = em.createQuery(ql);
        query.setParameter("value", value);
        List<Customer> list = query.getResultList();
        return list;
    }
}

TestCustomerDao 单元测试类

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@Transactional
public class TestCustomerDao2 {

    @Resource
    private CustomerDao customerDao;

    @Test
    public void testGetByProp() {
        List<Customer> list = customerDao.getByProp("name", "Lindsey Craft");
        Assert.assertTrue(list.size()>0);
    }

}


异常信息:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property prop found for type Customer!
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:213)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:321)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:301)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:85)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:60)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:168)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:320)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:169)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
... 55 more


解决方案:

     将CustomerDaoCustomImpl 类名改为 CustomerDaoImpl 即可 ! 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值