'Cannot access org.springframework.data from my Java Classes

I'm working on an online tutorial, and in the step where I need to work with databases and add jpa, I cannot access it in my project.

I have successfully put the dependency in my pom.xml file, and have received no errors. I was also able to access spring jpa through my application.properties file. The problem is that when I wanted to extend CrudRepository, it wasn't being recognized...I then realized that when I went to manually import, it would not import org.springframework.data.

I have maven set to always update snapshots.

Here is my pom.xml file. I apologize for the formatting, but I couldn't get it to all appear on stackoverflow with the correct formatting from intellij:

<?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.example</groupId>
<artifactId>demo3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo3</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</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-web</artifactId>
</dependency>

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

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

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

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

</dependencies>

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

And my 'application.properties' file:

spring.datasource.url=***    
spring.datasource.username=***
spring.datasource.password=***

spring.jpa.database=MYSQL

spring.jpa.hibernate.ddl-auto = update

spring.jpa.show-sql=false

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Cheese class:

package com.example.demo.models;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
public class Cheese {

    @Id
    @GeneratedValue
    private int id;

    @NotNull
    @Size(min=3, max=20)
    private String name;

    @NotNull
    @Size(min=1, message = "Description must not be empty")
    private String description;


    public Cheese(String name, String description) {
        this.name = name;
        this.description = description;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

CheeseDao (interface using Spring Data/CrudRepository)

package com.example.demo.models;
import com.example.demo.models.Cheese;


public class CheeseDao extends CrudRepository<Cheese, Integer> {
}


Solution 1:[1]

Add the Annotation to your DAO and it should be interface not class,

@Repository
public interface CheeseDao extends JpaRepository<Cheese, Integer> {

}

However, I would recommend you to use JpaRepository instead of CrudRepository. For details see this stackoverflow thread.

Now you can access them from any spring annotated class like below,

@Autowired
private CheeseDao cheeseDao;

Solution 2:[2]

It seem like you missing hibernate entity dependency, to resolve just add dependency below

 <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>

Solution 3:[3]

Replace

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>

with

<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
</dependency>

Solution 4:[4]

Instead of

package com.example.demo.models;
import com.example.demo.models.Cheese;


public class CheeseDao extends CrudRepository<Cheese, Integer> {
}

try

package com.example.demo.models;
import com.example.demo.models.Cheese;


public class CheeseDao extends CrudRepository<Object, Integer> {
}

The CrudRepository<> expects types.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ph?m PhĂșc
Solution 3 psmagin
Solution 4 Emesson Cavalcante