i am new to springframework technology and i am struggling to solve the following error.
the project starts successfully, i get the following error when the postman is executed
can anyone hel me solving the issue.
i guess i have pasted the important classes, i can post the whole code if needed
usercontroller
package com.appsdeveloper.blog.app.ws.controller;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.appsdeveloper.blog.app.ws.service.UserService;
import com.appsdeveloper.blog.app.ws.shared.dto.UserDto;
import com.appsdeveloper.blog.app.ws.ui.model.request.UserDetailsRequestModel;
import com.appsdeveloper.blog.app.ws.ui.model.response.UserRest;
#RestController
#RequestMapping("users")//http://localhost:8586/users
public class UserController{
#Autowired(required=true)//the bracket added from the internet
UserService userService;
//#Autowired
//private UserRepository userRepository;
#PostMapping
public UserRest createUser(#RequestBody UserDetailsRequestModel userDetails){
UserRest returnValue = new UserRest();
UserDto userDto = new UserDto();
BeanUtils.copyProperties(userDetails, userDto);
UserDto createdUser = userService.createUser(userDto);
BeanUtils.copyProperties(createdUser, returnValue);
return returnValue;
}
#DeleteMapping
public String deleteUser(){
return "delete user was called";
}
#GetMapping
public String getUser(){
return "get User was called";
}
#PutMapping
public String updateUser(){
return "update user was called";
}
}
UserEntity
package com.appsdeveloper.blog.app.ws.io.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="users", schema = "photo_app" )//name of table created to store information
public class UserEntity implements Serializable {
private static final long serialVersionUID = 7401188933477397731L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name="Id")
private long id;
#Column(name= "userId", nullable = false)
private String userId;
#Column(name = "firstName", nullable = false, length = 50)//the length is important to avoid default size 0f 250 varchar
private String firstName;
#Column(name = "lastName", nullable = false, length = 50)
private String lastname;
#Column(name = "email", length = 50)
private String email;
#Column(name = "password", nullable = false)
private String encryptedPassword;
//#Column(name = "emailVerificationToken", nullable = false)
private String emailVerificationToken;
#Column(name = "emailVerificationStatus", nullable=false)
private Boolean emailVerificationStatus = false;
public long getId() {
return id;
}
public String getUserId() {
return userId;
}
public String getFirstName() {
return firstName;
}
public String getLastname() {
return lastname;
}
public String getEmail() {
return email;
}
public String getEncryptedPassword() {
return encryptedPassword;
}
public String getEmailVerificationToken() {
return emailVerificationToken;
}
public Boolean getEmailVerificationStatus() {
return emailVerificationStatus;
}
///////////////////////////////////////////
public void setId(long id) {
this.id = id;
}
public void setUserId(String userId) {
this.userId = userId;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public void setEmail(String email) {
this.email = email;
}
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
public void setEmailVerificationToken(String emailVerificationToken) {
this.emailVerificationToken = emailVerificationToken;
}
public void setEmailVerificationStatus(boolean emailVerificationStatus) {
this.emailVerificationStatus = emailVerificationStatus;
}
}
application.property
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/photo_app
server.port=8586
spring.jpa.hibernate.use-new-id-generator-mappings=false
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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.appsdeveloperblog-app-ws</groupId>
<artifactId>mobile-app-ws</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mobile-app-ws</name>
<description>Demo project for Spring Boot</description>
<properties><!--</tiles:insertDefinition>-->
<!-- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>-->
<java.version>1.8</java.version>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</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>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.0.0.Alpha4</version>
<type>pom</type>
</dependency>
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jadira.usertype/usertype.core -->
<dependency>
<groupId>org.jadira.usertype</groupId>
<artifactId>usertype.core</artifactId>
<version>7.0.0.CR1</version>
</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>
error code
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: The database returned no natively generated identity value; nested exception is org.hibernate.HibernateException: The database returned no natively generated identity value] with root cause
org.hibernate.HibernateException: The database returned no natively generated identity value
You have to auto-increment your id column in your database.
Something like this: id INT NOT NULL AUTO_INCREMENT
Related
I'm fairly new at maven spring boot.
I'm running mvn clean install then mvn spring-boot:run
I'm then able to go an http post and insert data into the database. I can do an http get and see the data. I can view the data in mysql.
When I re-compile and run mvn clean install again, I'm not sure what's making it delete all the data in my product table in the database.
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.0</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>sample.info</groupId>
<artifactId>mainapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mainapp</name>
<description>Spring Boot Project</description>
<properties>
<java.version>18</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</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.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>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>0</forkCount>
<reuseForks>true</reuseForks>
<argLine>-Xmx1024m -XX:MaxPermSize=256m</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/appdb
spring.datasource.username=root
spring.datasource.password=
server.port = 9095
MainappApplication.java
package sample.info.mainapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
#EnableJpaAuditing
#SpringBootApplication
public class MainappApplication {
public static void main(String[] args) {
SpringApplication.run(MainappApplication.class, args);
}
}
controllers/ProductController.java
package sample.info.mainapp.controllers;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sample.info.mainapp.model.Product;
import sample.info.mainapp.repository.ProductRepository;
#RestController
#RequestMapping("/api")
public class ProductController {
#Autowired
ProductRepository productRepository;
#GetMapping("/products")
public List<Product> getAllProducts() {
final List<Product> productList = new ArrayList<Product>();
Iterable<Product> iterable = productRepository.findAll();
iterable.forEach(productList::add);
return productList;
}
#GetMapping("/products/{id}")
public ResponseEntity<Product> getProductById(#PathVariable(value = "id") Long id) {
Optional<Product> product = productRepository.findById(id);
return product.isPresent() ? new ResponseEntity<Product>(product.get(), HttpStatus.OK)
: new ResponseEntity("No data found", HttpStatus.NOT_FOUND);
}
#PostMapping("/products")
public Product createProduct(#RequestBody Product product) {
return productRepository.save(product);
}
#PutMapping("/products/{id}")
public ResponseEntity<Product> updateProduct(#PathVariable(value = "id") Long id, #RequestBody Product newProduct) {
Optional<Product> product = productRepository.findById(id);
if (product.isPresent()) {
Product prod = product.get();
prod.setDescription(newProduct.getDescription());
prod.setPrice(newProduct.getPrice());
prod.setTitle(newProduct.getTitle());
prod = productRepository.save(prod);
return ResponseEntity.ok().body(prod);
} else {
return ResponseEntity.notFound().build();
}
}
#DeleteMapping("/products/{id}")
public ResponseEntity<Product> deleteProduct(#PathVariable(value = "id") Long id) {
Optional<Product> product = productRepository.findById(id);
if (product.isPresent()) {
productRepository.delete(product.get());
return new ResponseEntity("Product has been deleted successfully.", HttpStatus.OK);
} else {
return ResponseEntity.notFound().build();
}
}
}
model/Product.java
package sample.info.mainapp.model;
import javax.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.Date;
#Entity
#Table(name = "products")
#EntityListeners(AuditingEntityListener.class)
#JsonIgnoreProperties(value = { "created_at", "updated_at" }, allowGetters = true)
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "title")
private String title;
#Column(name = "description")
private String description;
#Column(name = "price")
private float price;
#Column(nullable = false, updatable = false)
#Temporal(TemporalType.TIMESTAMP)
#CreatedDate
private Date created_at;
#Column(nullable = false)
#Temporal(TemporalType.TIMESTAMP)
#LastModifiedDate
private Date updated_at;
public Product() {
}
public Product(Long id, String title, String description, float price) {
this.id = id;
this.title = title;
this.description = description;
this.price = price;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Date getCreated_at() {
return created_at;
}
public Date getUpdated_at() {
return updated_at;
}
}
repository/ProductRepository.java
package sample.info.mainapp.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import sample.info.mainapp.model.Product;
#Repository
public interface ProductRepository extends CrudRepository <Product, Long> {
}
spring.jpa.hibernate.ddl-auto = create -– Hibernate first drops existing tables, then creates new tables. Click here for more details.
Remove / comment this property from application.properties and try.
I am trying to do a project in Angular but I noticed that the table is not created automatically. I put the structure of the project and the related code to do this.
As you can see in mysql, the users_database database is created with the statement "create database", but the employee table is not created.
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.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.guides.springboot2</groupId>
<artifactId>springboot2-jpa-crud-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Student_Angular-1</name>
<description>Rest API for a Simple Employee Management Application</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>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</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:mysql://localhost:3306/users_database?useSSL=false
spring.datasource.username = root
spring.datasource.password = root
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
server.servlet.context-path=/springboot-crud-rest
Employee.java
package net.guides.springboot2.springboot2jpacrudexample.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name = "employees")
public class Employee {
private long id;
private String firstName;
private String lastName;
private String emailId;
public Employee() {
}
public Employee(String firstName, String lastName, String emailId) {
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
#Column(name = "first_name", nullable = false)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Column(name = "last_name", nullable = false)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Column(name = "email_address", nullable = false)
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
#Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailId=" + emailId
+ "]";
}
}
EmployeeRepository.java
package net.guides.springboot2.springboot2jpacrudexample.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import net.guides.springboot2.springboot2jpacrudexample.model.Employee;
#Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
}
EmployeeController.java
package net.guides.springboot2.springboot2jpacrudexample.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.guides.springboot2.springboot2jpacrudexample.exception.ResourceNotFoundException;
import net.guides.springboot2.springboot2jpacrudexample.model.Employee;
import net.guides.springboot2.springboot2jpacrudexample.repository.EmployeeRepository;
#RestController #CrossOrigin(origins = "http://localhost:4200")
#RequestMapping("/api/v1")
public class EmployeeController {
#Autowired
private EmployeeRepository employeeRepository;
#GetMapping("/employees")
public List<Employee> getAllEmployees() {
return employeeRepository.findAll();
}
#GetMapping("/employees/{id}")
public ResponseEntity<Employee> getEmployeeById(#PathVariable(value = "id") Long employeeId)
throws ResourceNotFoundException {
Employee employee = employeeRepository.findById(employeeId)
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
return ResponseEntity.ok().body(employee);
}
#PostMapping("/employees")
public Employee createEmployee(#Valid #RequestBody Employee employee) {
return employeeRepository.save(employee);
}
#PutMapping("/employees/{id}")
public ResponseEntity<Employee> updateEmployee(#PathVariable(value = "id") Long employeeId,
#Valid #RequestBody Employee employeeDetails) throws ResourceNotFoundException {
Employee employee = employeeRepository.findById(employeeId)
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
employee.setEmailId(employeeDetails.getEmailId());
employee.setLastName(employeeDetails.getLastName());
employee.setFirstName(employeeDetails.getFirstName());
final Employee updatedEmployee = employeeRepository.save(employee);
return ResponseEntity.ok(updatedEmployee);
}
#DeleteMapping("/employees/{id}")
public Map<String, Boolean> deleteEmployee(#PathVariable(value = "id") Long employeeId)
throws ResourceNotFoundException {
Employee employee = employeeRepository.findById(employeeId)
.orElseThrow(() -> new ResourceNotFoundException("Employee not found for this id :: " + employeeId));
employeeRepository.delete(employee);
Map<String, Boolean> response = new HashMap<>();
response.put("deleted", Boolean.TRUE);
return response;
}
}
Try changing your value of hibernate.hbm2ddl.auto to create instead of update
you know it worked for me removing the useSSL=false
spring.datasource.url = jdbc:mysql://localhost:3306/users_database?
spring.datasource.username = root
spring.datasource.password = root
You also need to add the following driver-class-name, platform and generate-dll properties to auto-generate ddl.
Add these properties also.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.generate-ddl=true
I got this error
2021-02-04 10:31:48.341 WARN 6496 --- [nio-8080-exec-8]
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved
[org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Cannot construct instance of
com.sun.org.apache.xpath.internal.operations.String (although at
least one Creator exists): no String-argument constructor/factory
method to deserialize from String value ('springboot#gmail.com');
nested exception is
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot
construct instance of
com.sun.org.apache.xpath.internal.operations.String (although at
least one Creator exists): no String-argument constructor/factory
method to deserialize from String value ('springboot#gmail.com') at
[Source: (PushbackInputStream); line: 5, column: 13] (through
reference chain: com.sample.springbootdemo.domain.UserDTO["email"])
my code is,
<?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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sample</groupId>
<artifactId>springboot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
my domain is,
package com.sample.springbootdemo.domain;
import com.sun.org.apache.xpath.internal.operations.String;
import javax.persistence.*;
#Entity // This tells Hibernate to make a table out of this class
#Table(name = "user")
public class UserDTO {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name = "user_id" )
private Integer id;
#Column(name = "first_name",columnDefinition = "varchar(255)")
private String firstName;
#Column(name = "last_name",columnDefinition = "varchar(255)")
private String lastName;
#Column(name = "age")
private Integer age;
#Column(name = "email",columnDefinition = "varchar(255)")
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
** my controller is,**
package com.sample.springbootdemo.controllers;
import com.sample.springbootdemo.domain.UserDTO;
import com.sample.springbootdemo.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
#RestController
#RequestMapping(path="/user")
public class User {
#Autowired
private UserServices userServices;
#GetMapping("/all")
public List<UserDTO> allUsers(){
return userServices.findAllUsers();
}
#PostMapping(path = "/add")
public String addUser(#RequestBody UserDTO Userdata){
return userServices.saveUser(Userdata);
}
}
please help me to fix this error.
"String" should come with java natively. Do not use the import "com.sun.org.apache.xpath.internal.operations.String" Remove that line from your UserDTO.
I have a Spring/Boot REST controller which I inject a repository into like so:
package com.example.schooltimetable.controllers;
import com.example.schooltimetable.models.entities.StudentEntity;
import com.example.schooltimetable.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
#RestController
#CrossOrigin
#RequestMapping("student")
public class StudentController {
private final StudentRepository studentRepository;
#Autowired
public StudentController(StudentRepository studentRepository) {
this.studentRepository = studentRepository;
}
#RequestMapping(value = "/getAll", method = RequestMethod.GET)
public Iterable<StudentEntity> getAllStudents() {
return this.studentRepository.findAll();
}
}
The problem I'm facing is that whenever i start my application it throws the following error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.schooltimetable.controllers.StudentController required a bean of type 'com.example.schooltimetable.repository.StudentsRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.schooltimetable.repository.StudentsRepository' in your configuration.
Process finished with exit code 1
I faced this issue before when trying to find an Entity in another class, I fixed this by using the #ComponentScan() annotation in Application.java, however, it doesn't work to fix this issue.
My main application file looks like this:
SchoolTimetableApplication
package com.example.schooltimetable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
#SpringBootApplication
#ComponentScan({
"com.example.schooltimetable.controllers",
"org.springframework.stereotype",
"com.example.schooltimetable.repository",
})
public class SchoolTimetableApplication {
public static void main(String[] args) {
SpringApplication.run(SchoolTimetableApplication.class, args);
}
}
And my repository looks like this:
StudentRepository
package com.example.schooltimetable.repository;
import com.example.schooltimetable.models.entities.StudentEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
#Repository
#Component
public interface StudentRepository extends CrudRepository<StudentEntity, Long> { }
The StudentEntity referenced in the repository:
package com.example.schooltimetable.models.entities;
import com.example.schooltimetable.models.constants.StudentEntityConstants;
import com.example.schooltimetable.models.constants.TimetableEntityConstants;
import org.springframework.lang.NonNull;
import javax.persistence.*;
import java.util.UUID;
#Entity
#Table(name = StudentEntityConstants.TABLE_NAME)
public class StudentEntity {
#Id
#Column(name = StudentEntityConstants.ID)
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(name = StudentEntityConstants.FIRST_NAME)
#NonNull
private String firstName;
#Column(name = StudentEntityConstants.LAST_NAME)
#NonNull
private String lastName;
#Column(name = StudentEntityConstants.REFERENCE)
#NonNull
#GeneratedValue(strategy = GenerationType.AUTO)
private UUID reference;
#OneToOne(targetEntity = TimeTableEntity.class)
#JoinColumn(name = TimetableEntityConstants.REFERENCE)
private TimeTableEntity timeTable;
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public UUID getReference() {
return this.reference;
}
public void setReference(UUID reference) {
this.reference = reference;
}
public TimeTableEntity getTimeTable() {
return this.timeTable;
}
}
And 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 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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>school-timetable</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>school-timetable</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>com.h2database</groupId>
<artifactId>h2</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Is there something I've done wrong? I'm very new to Spring/Boot. An interesting note with this issue is that if I remove
#ComponentScan({
"com.example.schooltimetable.controllers", <---- This
"org.springframework.stereotype",
"com.example.schooltimetable.repository",
})
Then the application loads but the controller will throw a 404 when trying to hit: http://localhost:8080/student/getAll
EDIT!!!
There was a few things wrong with the application at this point.
Firstly, the POM file needed updating
<?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.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>school-timetable</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>school-timetable</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>com.h2database</groupId>
<artifactId>h2</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-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Secondly, the MAIN CLASS
package com.example.schooltimetable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
#SpringBootApplication
#PropertySource("classpath:application.properties")
#EntityScan("com.example.schooltimetable.models.entities")
#EnableJpaRepositories("com.example.schooltimetable.repository")
public class SchoolTimetableApplication {
public static void main(String[] args) {
SpringApplication.run(SchoolTimetableApplication.class, args);
}
}
There were also things wrong with the entities when joining columns: They should be set to read and write: false
package com.example.schooltimetable.models.entities;
import com.example.schooltimetable.models.constants.StudentEntityConstants;
import com.example.schooltimetable.models.constants.TimetableEntityConstants;
import org.springframework.lang.NonNull;
import javax.persistence.*;
import java.util.UUID;
#Entity
#Table(name = StudentEntityConstants.TABLE_NAME)
public class StudentEntity {
#Id
#Column(name = StudentEntityConstants.ID)
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#Column(name = StudentEntityConstants.FIRST_NAME)
#NonNull
private String firstName;
#Column(name = StudentEntityConstants.LAST_NAME)
#NonNull
private String lastName;
#Column(name = StudentEntityConstants.REFERENCE)
#GeneratedValue(strategy = GenerationType.AUTO)
private UUID reference;
#OneToOne(targetEntity = TimeTableEntity.class)
#JoinColumn(name = TimetableEntityConstants.REFERENCE, updatable = false, insertable = false)
private TimeTableEntity timeTable;
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public UUID getReference() {
return this.reference;
}
public void setReference(UUID reference) {
this.reference = reference;
}
public TimeTableEntity getTimeTable() {
return this.timeTable;
}
}
Thanks to #Abhinaba Chakraborty for pointing me in the right direction
First of all , you don't need a component scan with the annotation SpringBootApplication in place. Spring automatically scans all components annotated with Component,Service,RestController,Respository,Configuration etc. and creates the Beans.
The request mapping annotation value in StudentController should be prefixed with "/" eg. #RequestMapping("/student") - Although it has nothing to do with dependency injection . But this is the reason why you are getting a 404 at http://localhost:8080/student/getAll
In the repository interface, just use #Repository annotation instead of having both #Repository and #Component annotations.
Also I dont see a data-jpa dependency defined in your POM.xml yet How are you importing the org.springframework.data.repository.CrudRepository in your repository. You need that dependency.
As far as directory structure concerned, it looks OK ,but please verify:
FIXED!!!
So the problem was with the way I'd implemented the #OneToOne relationship between the student and the Timetable.
The edit in the question was a work around as stated by #M.Deinum. The correct solution was to first clean up the relationship between Student and Timetable, like so:
StudentEntity
package com.example.schooltimetable.models.entities;
import com.example.schooltimetable.models.constants.StudentEntityConstants;
import org.springframework.lang.NonNull;
import javax.persistence.*;
#Entity
#Table(name = StudentEntityConstants.TABLE_NAME)
public class StudentEntity {
#Id
#Column(name = StudentEntityConstants.ID)
private long id;
#Column(name = StudentEntityConstants.FIRST_NAME)
#NonNull
private String firstName;
#Column(name = StudentEntityConstants.LAST_NAME)
#NonNull
private String lastName;
#OneToOne(mappedBy = "student", cascade = CascadeType.ALL) // no need to join the columns on the Student.
private TimeTableEntity timeTable;
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public TimeTableEntity getTimeTable() {
return this.timeTable;
}
public void setTimeTable(TimeTableEntity timeTable) { this.timeTable = timeTable; }
}
TimeTableEntity
package com.example.schooltimetable.models.entities;
import com.example.schooltimetable.models.constants.TimetableEntityConstants;
import javax.persistence.*;
import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;
#Entity
#Table(name = TimetableEntityConstants.TABLE_NAME)
public class TimeTableEntity implements Serializable {
#Id
#Column(name = TimetableEntityConstants.ID)
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
#OneToOne
#MapsId // <-- This automatically knows to join by the student ID column
private StudentEntity student;
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private final Set<ModuleEntity> modules = new LinkedHashSet();
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public void setStudent(StudentEntity student) {
this.student = student;
}
public Set<ModuleEntity> getModules() {
return this.modules;
}
public void setModules(ModuleEntity module) {
this.modules.add(module);
}
}
Now, the next few refactors, I could just do, I don't know why it worked all of a sudden but I have a feeling it was to do with the bad entity setup.I was able to remove the
#EntityScan("com.example.schooltimetable.models.entities") & #EnableJpaRepositories("com.example.schooltimetable.repository")
Annotations from my main class, and I was also able to remove a lot of #Scope and #Component annotations from my code. My main class now looks like this:
SchoolTimetableApplication
package com.example.schooltimetable;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
#SpringBootApplication
#PropertySource("classpath:application.properties")
public class SchoolTimetableApplication {
public static void main(String[] args) {
SpringApplication.run(SchoolTimetableApplication.class, args);
}
}
Much better! I also have data pulling through for the relationships rather than null (another issue I was facing).
I'm new to using Spring MVC and JPA. I have a Postgresql database in which I create two tables: author and userType.
CREATE TABLE UserType (
idUserType INTEGER NOT NULL,
userType VARCHAR NOT NULL,
CONSTRAINT pkusertype PRIMARY KEY (idUserType)
);
CREATE TABLE Author (
idAuthor INTEGER NOT NULL DEFAULT nextval('author_idauthor_seq'),
lastName VARCHAR NOT NULL,
firstName VARCHAR NOT NULL,
CONSTRAINT pk_author PRIMARY KEY (idAuthor)
);
Then I want to map them to my code in Spring and show them. The issue is that even though I do it in the same way, Author is displayed correctly ([some JSON]) while when I want to see userType I receive an empty array ([]). I have no idea why it's happening and would be grateful for help.
Author Model
package com.shareabook.model;
import javax.persistence.*;
#Entity
#Table(name = "author")
public class Author {
#Id
#GeneratedValue
#Column(name = "idauthor")
private Integer id;
#Column(name = "lastname")
private String lastName;
#Column(name = "firstname")
private String firstName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
UserType model
package com.shareabook.model;
import javax.persistence.*;
#Entity
#Table(name = "UserType ")
public class UserType {
#Id
#GeneratedValue
#Column(name = "idusertype")
private Integer id;
#Column(name = "usertype")
private String userType;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
}
AUTHOR CONTROLLER
package com.shareabook.controller;
import com.shareabook.model.Author;
import com.shareabook.repository.AuthorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
#RequestMapping(value = "/rest/author")
public class AuthorController {
#Autowired
AuthorRepository authorRepository;
#GetMapping(value = "/all")
public List<Author> getAll(){
return authorRepository.findAll();
}
}
USERTYPE CONTROLLER
package com.shareabook.controller;
import com.shareabook.model.UserType;
import com.shareabook.repository.UserTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
#RequestMapping(value = "/rest/usertype")
public class UserTypeController {
#Autowired
UserTypeRepository userTypeRepository;
#GetMapping(value = "/all")
private List<UserType> getAll(){
return userTypeRepository.findAll();
}
}
Author Repository
package com.shareabook.repository;
import com.shareabook.model.Author;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AuthorRepository extends JpaRepository<Author, Integer>{
}
UserType repository
package com.shareabook.repository;
import com.shareabook.model.UserType;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserTypeRepository extends JpaRepository<UserType, Integer>{
}
And the main class
package com.shareabook;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
#EnableJpaRepositories(basePackages = "com.shareabook.repository")
#SpringBootApplication
public class ShareabookApplication {
public static void main(String[] args) {
SpringApplication.run(ShareabookApplication.class, args);
}
}
My pom.xml 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.shareabook</groupId>
<artifactId>shareabook</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>shareabook</name>
<description>Basic project for application Share A Book</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.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-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<scope>provided</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.dbcp2.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/shareabookthesis
spring.datasource.username=postgres
spring.datasource.password=wiktoria2
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
Database name in Postgres should be in the small case and Table name should not be in CamelCase.Table name should be usertype instead of UserType.