Carrm
Carrm

Reputation: 1593

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.

Upvotes: 17

Views: 61638

Answers (11)

Saeed Kiarsi Zadeh
Saeed Kiarsi Zadeh

Reputation: 87

Another option is using JpaRepository 'cause is more comprehensive than CrudRepository that made for simple operations:


JpaRepository Maven repository page:

https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa

Upvotes: 0

Augustine Awuori
Augustine Awuori

Reputation: 21

Add JPA dependency when creating your project. Worked for me!

Upvotes: 1

hexhad
hexhad

Reputation: 1303

In my case I wasn't able to import CrudRepository

to Resolve this

1. Using Intellij idea

  1. type "extends CrudRepository" click on CrudRepository and press Alt and Enter
  2. Click add maven dependency
  3. Select CrudRepository

2. Adding dependency

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons</artifactId>
    <version>2.4.8</version>
</dependency>

Sometimes (after adding dependency) you will get an error, importing org.junit.jupiter.api.Test then add

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.8.0-M1</version>
    <scope>test</scope>
</dependency>

Upvotes: 0

Zach Pedigo
Zach Pedigo

Reputation: 426

If you are having this problem, check if the org.springframework.data.repository.CrudRepository import is working. If it is then this answer probably won't be the solution. If it is not working and the "data" part is highlighted, then check your pom.xml and if you don't have it already, add the following dependency to it:

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

After adding the dependency, delete your local .m2 directory and build from the command-line. You may also want to reimport the maven project to your workspace.

Upvotes: 9

z atef
z atef

Reputation: 7689

Here is how i got the message to disappear:

enter image description here

Upvotes: 0

Pinaki Mukherjee
Pinaki Mukherjee

Reputation: 1656

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>

Upvotes: 50

samivic
samivic

Reputation: 1310

i had the same problem, just delete all .m2 content and re-import Maven. it works!

Upvotes: 0

Carrm
Carrm

Reputation: 1593

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.

Upvotes: 8

user3578953
user3578953

Reputation: 146

check mavendependencies for spring-data-jpa-...RELEASE.jar if it contains CrudRepository.class under org.springframework.data.jpa.repository package.

If the class file is there, just maven->update project and run->maven-clean.

If the class file is not there, just search the package in mvnrepository.com.

Upvotes: 2

user8220390
user8220390

Reputation:

CrudRepository is an interface not a class. Replace extends with implements.

Upvotes: -3

Oleksandr Yefymov
Oleksandr Yefymov

Reputation: 6509

All seems nice with your dependencies, please try next variants:

  1. build project with Maven from command line($ mvn spring-boot:run)
  2. In Eclipse IDE: right click on project-Maven-update project

Upvotes: 1

Related Questions