Spring Boot + Hibernate + Postgres - not creating tables - java

I am trying to generate schema tables based on entities. The application starts correctly, the SQL is generated but there is no result - none of tables are created. What's wrong? I have used the same settings in plain Spring MVC + Hibernate JPA without Spring Boot and everything was working correctly.
Here is my 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.agileplayers</groupId>
<artifactId>applicationname</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ApplicationName</name>
<description>Application Name</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<!--<scope>runtime</scope>-->
<version>9.4-1201-jdbc41</version>
</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>
</project>
application.properties:
spring.datasource.url = jdbc:postgresql://localhost:5432/postgres
spring.datasource.driverClassName = org.postgresql.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect
spring.datasource.schema=schemaName
spring.datasource.username=userName
spring.datasource.password=userPassword
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
BaseEntity.java:
package com.agileplayers.applicationname.core.domain;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.util.Date;
#MappedSuperclass
public class BaseEntity {
#Id
#GeneratedValue
private int id;
private Date createdOn;
public String description;
public BaseEntity() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
example of Entity which extends BaseEntity - Entry.java:
package com.agileplayers.applicationname.core.domain;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import java.util.Date;
#Entity
public class Entry extends BaseEntity{
private String type;
#ManyToOne
private Account account;
public Entry() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}

I have fixed the issue. Instead of spring.datasource.schema=schemaName there should be spring.jpa.properties.hibernate.default_schema=schemaName.
The set of necessary properties required for generating tables in my case is the following:
spring.datasource.url = jdbc:postgresql://localhost:5432/postgres
spring.jpa.properties.hibernate.default_schema = schemaName
spring.datasource.username = userName
spring.datasource.password = userPassword
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.show-sql = true

Related

No operations defined in spec!! and 404 error in postman. The spring boot don't show any error from my code. yet i can't establish the connection

This is a simple code that i got from youtube for practice. well i did the code correcly but i cant get the explicit mapping correclty, that what the 404 error in the postman says.
I don't know if its my Spring boot app problem or the code error. please have a look at this , Since spring boot is not giving a proper answer.
//CONTROLLER
#Slf4j
#RestController
#RequestMapping("/api/v1")
public class RestuController {
#Autowired
private RestuService restuService;
/////////////////////////////////////POST MAPPING//////////////////////////////////////////////////////////////////////
#PostMapping("/POST/save")
Restaurant addRestaurant(#RequestBody Restaurant restaurant) {
return restuService.saveRestu(restaurant);
}
#PostMapping("/POST/saves")
List<Restaurant> addRestaurants(#RequestBody List<Restaurant> restaurants) {
return restuService.saveRestaurants(restaurants);
}
#GetMapping("/GET/resturants")
List<Restaurant> findAllRestaurants(){
log.debug("reached /GET/resturants");
return restuService.getRestaurants();
}
#GetMapping("/GET/{id}")
Restaurant findRestaurantById(#PathVariable int id) {
log.debug("reached /GET/{id} :" + id);
return restuService.getRestaurantById(id);
}
#GetMapping("/GET/{name}")
Restaurant findRestaurantByName(#PathVariable String name) {
log.debug("reached /GET/{name}:" + name);
return restuService.getRestaurantByname(name);
}
#GetMapping("/GET/{dist}")
List<Restaurant> findRestaurantByDist(#PathVariable int distance) {
log.debug("reached /GET/{dist} :" + distance);
return restuService.getRestaurantbyDist(distance);
}
#GetMapping("/GET/{loc}")
List<Restaurant> findRestaurantByLocation(#PathVariable String location) {
log.debug("reached /GET/{loc} : " + location);
return restuService.getRestaurantbyLocation(location);
}
#GetMapping("/GET/{cuisine}")
List<Restaurant> findRestaurantByCuisine(#PathVariable String cuisine) {
log.debug("reached /GET/{cuisine} : " + cuisine);
return restuService.getRestaurantbyCuisine(cuisine);
}
#GetMapping("/GET/{budget}")
List<Restaurant> findRestaurantByBudget(#PathVariable int budget) {
log.debug("reached /GET/{Budget} : " + budget);
return restuService.getRestaurantbyBudget(budget);
}
}
//SERVICE
package SERVICE;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ENTITY.Restaurant;
import REPOSITORY.RestuRepository;
#Service
public class RestuService {
#Autowired
RestuRepository restuRepository;
///////////////////postmethods//////////////////////////////////////
public Restaurant saveRestu(Restaurant restaurant) {
return restuRepository.save(restaurant);
}
public List<Restaurant> saveRestaurants(List<Restaurant> restaurants) {
return restuRepository.saveAll(restaurants);
}
///////////////////////////////////////////////////////////////////////////////////////
//////////////////GET METHODS//////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
public List<Restaurant> getRestaurants(){
return restuRepository.findAll();
}
public Restaurant getRestaurantById(int id){
return restuRepository.findById(id).orElse(null);
}
public Restaurant getRestaurantByname(String name){
return restuRepository.findByName(name);
}
public List<Restaurant> getRestaurantbyDist(int distance){
return restuRepository.findByDist(distance);
}
public List<Restaurant> getRestaurantbyLocation(String location){
return restuRepository.findByLocation(location);
}
public List<Restaurant> getRestaurantbyCuisine(String cuisine){
return restuRepository.findBycuisine(cuisine);
}
public List<Restaurant> getRestaurantbyBudget(int budget){
return restuRepository.findBybudget(budget);
}
}
//REPOSITORY
package REPOSITORY;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import ENTITY.Restaurant;
#Repository
public interface RestuRepository extends JpaRepository<Restaurant, Integer> {
Restaurant findByName(String name);
List<Restaurant> findByDist(int distance);
List<Restaurant> findByLocation(String location);
List<Restaurant> findBycuisine(String cuisine);
List<Restaurant> findBybudget(int budget);
}
//ENTITY OR MODEL CLASS
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
#Data
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "restaurant_tbl")
public class Restaurant {
#Id
#GeneratedValue
private int id;
private String name;
private String cuisine;
private String location;
private int distance;
private int budget;
}
//APPLICATION.PROPERTIES
server.port = 6282
spring.datasource.dbcp2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url =jdbc:mysql://localhost:3306/restaurants
spring.datasource.username = root
spring.datasource.password = ****
spring.jpa.show-sql =true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
springdoc.api-docs.path=/api-docs
springdoc.swagger-ui.path=/swagger-ui.html
//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.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sangamam</groupId>
<artifactId>RESTAURANT-SERVICE</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>RESTAURANT-SERVICE</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</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-web</artifactId>
</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.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.4</version>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

#NotNull constraint is not working in Spring Boot

I am using Spring Boot and the #NotNull is not working. When the name value is not provided the function runs fine.
#Entity
public class Employee implements Serializable{
#Id
//#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
//#Id
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
#NotNull // not working
private String name;
private String phone;
}
the controller is
#Controller
public class URLController {
#Autowired
EmployeeServiceImpl empService;
#GetMapping({"/", "/index"})
public String index1(Model model){
model.addAttribute("employee",new Employee());
return "index";
}
#PostMapping("/result")
public String result(#Valid #ModelAttribute Employee employee){
System.out.print(employee.getName());
empService.save(employee);
return "result";
}
}
pom.xml
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.springs</groupId>
<artifactId>springs</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springs</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.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-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.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
i am using the model attribute to get the employee object. the pom.xml file contains the hibernate validator dependency.
you must add #Valid annotaion in mapping part in your code for example
void addEmployee(#Valid Employee emp);
using this import "import javax.validation.Valid;"
Another note if you want to validate List you must make a wrapper list
Like
public class ValidList<E> implements List<E> {
#Valid
private List<E> list;}
And override all methods in it
Try modifying method like below to check details about errors.
#PostMapping("/result")
public String result(#Valid #ModelAttribute("employee") Employee employee, BindingResult bindingResult){
List<FieldError> errors = bindingResult.getFieldErrors();
for (FieldError error : errors ) {
System.out.println (error.getObjectName() + " - " +error.getDefaultMessage());
}
System.out.print(employee.getName());
empService.save(employee);
return "result";
}
If no error in bindingResult then refer notnull-constraint-not-working to troubleshoot further.

Error executing DDL via JDBC Statement with Spring Boot default configuration

This is error which I get when I try to run the Spring Boot app as java app:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
Caused by: java.sql.SQLSyntaxErrorException: Schema 'SA' does not exist
I have tride to change hibernate dialect in propereties in pom.xml (like written in other simillar topics about this problem) but it didn't help. I have also tried to change names of Topic class and fields.
This is the 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.julian.boot3</groupId>
<artifactId>boot3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>boot3</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</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>
</project>
Main class:
package com.julian.spring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
POJO:
package com.julian.spring.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="topics")
public class Topic {
#Id
private Integer id;
private String name;
private String description;
public Topic() {
}
public Topic(int id, String name, String description) {
this.id = id;
this.name = name;
this.description = description;
}
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 getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Put this in application.properties file under resources:
spring.jpa.hibernate.ddl-auto=update
I solved it by downgrading version of spring-boot-starter-parent fomr 2.0.4.REALESE to 1.5.1.RELEASE
Please read https://spring.io/guides/gs/accessing-data-mysql/ this, is so regular question, how to arrange connection to database.
If you are not using MySQL, there are article for other databases, but for relational databases, all are the same.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userResource

I've been having this issue for days now on my eclipse console:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userResource'
followed by
Caused by: java.lang.IllegalStateException: Failed to introspect Class
and
Caused by: java.lang.NoClassDefFoundError: org/springframework/data/jpa/repository/JpaRepository
then
Caused by: java.lang.ClassNotFoundException:
org.springframework.data.jpa.repository.JpaRepository
this is my
user.java
package com.example.SpringMVC4.webservices.SpringMVC04webservices;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
#Entity
public class User {
#Id
#GeneratedValue
private Integer id;
private String name;
private Date birthDate;
public User(Integer id, String name, Date birthDate) {
super();
this.id = id;
this.name = name;
this.birthDate = birthDate;
}
public User(String string, String string2) {
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
#Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", birthDate=" + birthDate + "]";
}
}
Below:
UserRepository.java
package com.example.SpringMVC4.webservices.SpringMVC04webservices;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface UserRepository extends JpaRepository<User, Integer>{
User findOne(int id);
}
Below is
UserResource.java
package com.example.SpringMVC4.webservices.SpringMVC04webservices;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
#RestController
public class UserResource {
#Autowired
private UserRepository userRepository;
// retrieve All Users GET
#GetMapping("/users")
public List<User> retrieveAllUsers(){
return userRepository.findAll();
}
//retrieve specific user
#GetMapping("/users/{id}")
public User retrieveUser(#PathVariable int id){
return userRepository.findOne(id);
}
}
Below is my 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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.SpringMVC.04.webservices</groupId>
<artifactId>SpringMVC-04-webservices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringMVC-04-webservices</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.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-web</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-data-jpa</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Can't create table with jpa+spring boot

I'm trying to generate table using JPA. but i can't create it. There is no error in the code, but it seems that there is configuration error. but i can't find it, i tried many configurations but nothing happen.
Thanks a lot.
This is application.property:
spring.datasource.url=jdbc:mysql://localhost:3306/springapp
spring.datasource.username= root
spring.datasource.password= me
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
this is my class:
#Entity
public class Customer {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;
private String phone;
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 getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Customer(String name, String phone) {
super();
this.name = name;
this.phone = phone;
}
public Customer() {
super();
}
}
This is the application:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And this is the controller:
#RestController
#RequestMapping("/customer")
public class CustomerController {
#Autowired
CustomerRepository customerRepository;
#RequestMapping("/findall")
#ResponseBody
public List<Customer> findAll() {
return customerRepository.findAll();
}
}
Pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>GStock-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>GStock-3</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</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>
</project>
Try to add #EntityScan
#EntityScan("<package with entities>")
#SpringBootApplication
public class Application { ... }
With the current information I have a couple of suggestions / Questions that might help you allong:
Does the "springapp" database exist with the correct user (root) and password (me) assigned to it in MySQL
Has MySQL been started?
I see no #Repository definition in your code e.g.
#Repository
public interface CustomerRepository extends JpaRepository{}
execute following on your mysql:
mysql> create database db_example; -- Create the new database
mysql> create user 'springuser'#'localhost' identified by 'ThePassword'; -- Creates the user
mysql> grant all on db_example.* to 'springuser'#'localhost'; -- Gives all the privileges to the new user on the newly created database
and add into application.xml:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
OR
you can go through this link

Categories

Resources