Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CrudRepository cannot be resolved to a type

I'm working on a Spring Boot project (generated by Spring Initializr), using Maven. I want to create a CrudRepository, but I'm getting the error "CrudRepository cannot be resolved to a type" and the package org.springframework.data.repository does not contain the CrudRepository class. I tried to follow a bunch of tutorials to understand what is wrong and I didn't find anything. My POM looks right to me, I don't have any build failure when I run maven clean and package goals, I tried to update the project and download sources, but nothing works. Eclipse can't find the CrudRepository class.

Here is my POM file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.myquickstart</groupId>
    <artifactId>SpringQuickstart</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>SpringQuickstart</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

And my repository:

package com.myquickstart.repositories;

import org.springframework.stereotype.Repository;
import org.springframework.data.repository.CrudRepository;
import com.myquickstart.entities.Movie;

@Repository
public interface MovieRepository extends CrudRepository<Movie, Long> {

}

Thanks for your help

Edit:

I've updated the project with the "Maven => Update project" in Eclipse. After that, I got this error in the "Problems" view:

C:/Users/carrm/.m2/repository/org/hibernate/hibernate-core/5.0.11.Final/hibernate-core-5.0.11.Final.jar' in project 'SpringQuickstart' cannot be read or is not a valid ZIP file

I'm not sure to know how to solve this/what is the link with my CrudRepository problem.

like image 810
Carrm Avatar asked Feb 22 '17 16:02

Carrm


2 Answers

P.S - I know the original answer used interface not class. But as we can't ask same question again. The answer is for beginner who may missed the interface part.

We generally get this type of error when we create the repository as class instead of interface. CrudRepository is an interface, you most probably extend it using an interface only:

Wrong (Some times when we create the repository we create a class by mistake ):

 public class MovieRepository extends CrudRepository<Movie, Long>

Error: CrudRepository cannot be resolved to a type

Just changed it to interface:

public interface MovieRepository extends CrudRepository<Movie, Long>
like image 63
Pinaki Mukherjee Avatar answered Sep 18 '22 15:09

Pinaki Mukherjee


It looks like it was an issue with Maven libraries. I deleted the whole content in .m2/repository and ran Maven > Update project in Eclipse, so that Maven had to download the whole content again. No more error after this!

Edit

As pointed out by user3578953, executing maven-clean does the same thing that I did by deleting the whole m2 repository content. I didn't know much about Maven when I first asked this question, but this is obviously a better way to solve the issue.

like image 27
Carrm Avatar answered Sep 20 '22 15:09

Carrm