例外:在Spring Data JPA中找不到类型void的属性删除

时间:2017-03-09 17:19:28

标签: java spring-boot spring-data-jpa

我使用Spring Data Jpa并在部署项目时遇到异常 我在service和serviceImpl类中使用了findAll()方法,我收到了一个异常,这是我的来源:

型号:

@Entity
@Table(name="CITY")
public class City {

  @Id
  @Column(name="ID", nullable = false, unique = true)
  @GeneratedValue(strategy=GenerationType.AUTO)
  private int id;

  @Column(name = "NAME")
  private String name;
  // get, set method      
}

存储库类:

public interface CityJpaRepositoryCustom extends JpaRepository<City, Integer> { 
}

服务类:

public interface CityService {
   public List<City> findAll();
}

ServiceImpl类:

@Service
public class CityServiceImpl implements CityService {

@Resource
private CityJpaRepositoryCustom cityRepository; 

@Override
@Transactional
public List<City> findAll() {       
    // TODO Auto-generated method stub      
    return cityRepository.findAll();        
}
}

控制器类:

@Controller
public class CityController {

@Autowired
private CityService cityService;    

@RequestMapping(value="/list", method=RequestMethod.GET)
public ModelAndView getListCity(HttpServletRequest request, 
                                          HttpServletResponse response,
                                          ModelMap model) {     
    ModelAndView mav = new ModelAndView("manageCity");

    List<City> cityList = cityService.findAll();
    mav.addObject("cityList", cityList);

    return mav;
}

的pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.0.1.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>hibernate-entitymanager</artifactId>
                <groupId>org.hibernate</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- SOLR -->
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-solr</artifactId>
        <version>${spring-data-solr.verion}</version>
    </dependency>

    <!-- JSON -->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.11</version>
    </dependency>

    <!-- WEB -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Hibernate 4 dependencies -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

</dependencies>

记录消息:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cityJpaRepositoryCustom': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property delete found for type void!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)

我找到了一些相同的主题,但它对我来说没有得到好结果,如何修复此异常?非常感谢你!

2 个答案:

答案 0 :(得分:1)

将您的Spring Boot版本升级到最新版本(1.5.1.RELEASE)。

你正在使用一个古老的版本,而这个版本又会引入旧版本的Spring Data,这可能不支持删除。

答案 1 :(得分:1)

如果您想使用删除操作但不想更新Spring Data JPA,则必须编写自己的删除实现。例如:

public interface CityJpaRepositoryCustom extends JpaRepository<City, Integer> {
    @Modifying
    @Query("delete from City where name = :name")
    void deleteByName(@Param("name") String name);
}

相关文档:

  1. Using @Query注释
  2. @Modifying注释
  3. @Param注释