0

I have seen a lot of questions but the answers are not satisfying my problem...

I generate my project with Spring initialzr enter image description here

I tried with the 3 java versions 14, 11, 8.

This is my project structure.

enter image description here

GardenApplication.java. I include all anottations even spring doc says that with @SpringBootApplication is enough. Before this error @Repository couldn't be found and I include @EnableJpaRepositories

package com.garden.garden;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com.garden.garden.controller", "com.garden.garden.service.impl"})
@EnableJpaRepositories(basePackages = {"com.garden.garden.repository"})
@EntityScan(basePackages = {"com.garden.garden.model"})
public class GardenApplication {

    public static void main(String[] args) {
        SpringApplication.run(GardenApplication.class, args);
    }

}

VegetableController.java

package com.garden.garden.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.garden.garden.model.Vegetable;
import com.garden.garden.service.VegetableService;

@Controller
@RequestMapping
public class VegetableController {

    @Autowired
    private VegetableService vegetableService;

    @GetMapping("/vegetableList")
    public String list(Model model) {
        List<Vegetable> listVegetables = vegetableService.list();
        model.addAttribute("vegetables", listVegetables);
        return "index";
    }

}

VegetableServiceImpl.java

package com.garden.garden.service.impl;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.garden.garden.model.Vegetable;
import com.garden.garden.repository.VegetableRepository;
import com.garden.garden.service.VegetableService;

@Service
public class VegetableServiceImpl implements VegetableService {

    @Autowired
    private VegetableRepository vegetableRepository;

    @Override
    public List<Vegetable> list() {
        return (List<Vegetable>) vegetableRepository.findAll();
    }

    @Override
    public Optional<Vegetable> listById(int id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int save(Vegetable vegetable) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void delete(int id) {
        // TODO Auto-generated method stub

    }

}

VegetableService.java

package com.garden.garden.service;

import java.util.List;
import java.util.Optional;

import com.garden.garden.model.Vegetable;

public interface VegetableService {
    public List<Vegetable> list();
    public Optional<Vegetable> listById(int id);
    public int save(Vegetable vegetable);
    public void delete(int id);
}

VegetableRepository.java

package com.garden.garden.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.garden.garden.model.Vegetable;

@Repository
public interface VegetableRepository extends JpaRepository<Vegetable, Integer> {

}

Vegetable.java

package com.garden.garden.model;

import java.math.BigDecimal;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "vegetable")
public class Vegetable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String family;
    private int daysOnNursery;
    private int daysToGrow;
    private int daysHarvesting;
    private java.math.BigDecimal plantsPerSquare;
    private java.math.BigDecimal price;
    private java.math.BigDecimal weight;



    public Vegetable(int id, String name, String family, int daysOnNursery, int daysToGrow, int daysHarvesting,
            BigDecimal plantsPerSquare, BigDecimal price, BigDecimal weight) {
        super();
        this.id = id;
        this.name = name;
        this.family = family;
        this.daysOnNursery = daysOnNursery;
        this.daysToGrow = daysToGrow;
        this.daysHarvesting = daysHarvesting;
        this.plantsPerSquare = plantsPerSquare;
        this.price = price;
        this.weight = weight;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getFamily() {
        return family;
    }

    public void setFamily(String family) {
        this.family = family;
    }

    public int getDaysOnNursery() {
        return daysOnNursery;
    }

    public void setDaysOnNursery(int daysOnNursery) {
        this.daysOnNursery = daysOnNursery;
    }

    public int getDaysToGrow() {
        return daysToGrow;
    }

    public void setDaysToGrow(int daysToGrow) {
        this.daysToGrow = daysToGrow;
    }

    public int getDaysHarvesting() {
        return daysHarvesting;
    }

    public void setDaysHarvesting(int daysHarvesting) {
        this.daysHarvesting = daysHarvesting;
    }

    public java.math.BigDecimal getPlantsPerSquare() {
        return plantsPerSquare;
    }

    public void setPlantsPerSquare(java.math.BigDecimal plantsPerSquare) {
        this.plantsPerSquare = plantsPerSquare;
    }

    public java.math.BigDecimal getPrice() {
        return price;
    }

    public void setPrice(java.math.BigDecimal price) {
        this.price = price;
    }

    public java.math.BigDecimal getWeight() {
        return weight;
    }

    public void setWeight(java.math.BigDecimal weight) {
        this.weight = weight;
    }

}

I also try to solve the problem adding this dependency:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
</dependency>

pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.garden</groupId>
    <artifactId>garden</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>garden</name>
    <description>Market garden web application</description>

    <properties>
        <java.version>14</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

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

</project>

application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

spring.datasource.url=jdbc:mysql://localhost:3306/garden
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=create
2
  • 1
    This usually happens when you don't have access to your database. Can you check that you can access your MySql database using what you provided in application.properties? Commented Apr 28, 2020 at 18:14
  • You are right. I don't include time zone in the connection url. At the beginning I solve this problem adding this property spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect. Commented Apr 29, 2020 at 9:02

1 Answer 1

1

Please remove the exclusion of the auto configuration in your application.properties and add an hibernate-dialect, i.e. update your application.properties as follows:

spring.datasource.url=jdbc:mysql://localhost:3306/garden
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=create
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

In addition you can simplify GardenApplication to

package com.garden.garden;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
@EntityScan
public class GardenApplication {

  public static void main(String[] args) {
    SpringApplication.run(GardenApplication.class, args);
  }

}

and remove tomcat dependency from your pom (it comes in transitively with spring-boot-starter-web). Besides, having lombok in your dependencies, you can annotate Vegetable with @Data and remove Getters, Setters and the constructor.

Sign up to request clarification or add additional context in comments.

2 Comments

The spring.autoconfigure.exclude = org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration was the one that cause the problem. I add this because I read in other post that with that I solve the error of mysql server time zone.
Also I have to say that if I add spring.jpa.database-platform = org.hibernate.dialect.MySQLDialect I get an error executing DDL type=MyISAM. Is no need to expecified this, I read in other post that if you add this property you have to especify mysql version: stackoverflow.com/questions/43716068/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.