简体   繁体   中英

Exception: No property delete found for type void in Spring Data JPA

I'm using Spring Data Jpa and facing an exception when I deployed the project I have used findAll() method in service and serviceImpl class and I received an exception, this is my source:

Model:

@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      
}

Repository class:

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

Service class:

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

ServiceImpl class:

@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 class:

@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>

Log message:

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)

I have found some same topic but it's not get good result for me, How to fix this exception? thank you so much!

Upgrade your version of Spring Boot to the latest (1.5.1.RELEASE).

You're using an ancient version which will in turn be pulling in an old version of Spring Data which may have not supported delete.

If you want to use delete operation but don't want to update Spring Data JPA , you have to write your own delete implementation. Example:

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

Related docs:

  1. Using @Query annotation
  2. @Modifying annotation
  3. @Param annotation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM