So i have been working on a spring boot application for a school project and i cant run it because when i try to save in a repository i get the following errors. I have tried many things like changing the relation types ,adding cascades but nothing seems to work. Below is the code for 2 classes and main and also the errors
package com.site.anime.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Fetch;
import javax.persistence.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
#Entity
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Show {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String image;
private Integer no_episodes;
private String description;
private Double overall_score;
#ManyToMany
private List<Caracter> caracters;
#OneToMany
private List<Review> reviews;
#ManyToMany
private List<Category> categories;
}
package com.site.anime.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.List;
#Entity
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Caracter {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String image;
private String description;
#ManyToMany
private List<Show> shows;
}
package com.site.anime;
import com.site.anime.model.*;
import com.site.anime.repository.*;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
#EnableJpaRepositories
#SpringBootApplication
public class AnimeApplication {
public static void main(String[] args) {
SpringApplication.run(AnimeApplication.class, args);
}
#Bean
CommandLineRunner init(RepositoryShow repositoryShow, RepositoryCategory repositoryCategory,
RepositoryCaracter repositoryCaracter, RepositoryUser repositoryUser,
RepositorySuperUser repositorySuperUser){
return args -> {
List<Show> shows=new LinkedList<>();
Caracter caracter=new Caracter(null,"rusu","","",null);
List<Caracter> caracters=new LinkedList<>();
caracters.add(caracter);
Profile profile=new Profile();
Utilizator user=new Utilizator(null,"","","",profile,null);
//Review review=new Review(null,user,"",0);
Review review=new Review(null,null,"",0);
List<Review> reviews=new LinkedList<>();
reviews.add(review);
user.setReviews(reviews);
Show show=new Show(null,"ReZero Season 1","",25,"",0.0,null,reviews,null);
shows.add(show);
caracter.setShows(shows);
show.setCaracters(caracters);
//
//repositoryShow.save(show);
repositoryCaracter.save(caracter);
//repositoryUser.save(user);
};
}
}
and the errors
2021-03-11 14:11:02.765 INFO 10248 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-03-11 14:11:02.771 INFO 10248 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-03-11 14:11:03.007 WARN 10248 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-03-11 14:11:03.065 INFO 10248 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-03-11 14:11:03.193 INFO 10248 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-03-11 14:11:03.198 INFO 10248 --- [ main] com.site.anime.AnimeApplication : Started AnimeApplication in 2.922 seconds (JVM running for 3.566)
2021-03-11 14:11:03.245 WARN 10248 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1064, SQLState: 42000
2021-03-11 14:11:03.245 ERROR 10248 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show (description, image, name, no_episodes, overall_score, id) values ('', '', ' at line 1
2021-03-11 14:11:03.252 INFO 10248 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-03-11 14:11:03.263 ERROR 10248 --- [ main] o.s.boot.SpringApplication : Application run failed
``
SHOW is a keyword for MySql (as well as NAME and DESCRIPTION), so you should use SQL quoted identifier, like below:
#Entity
#Table(name = "`show`")
public class Show {
// ...
#Column(name = "`name`")
private String name;
#Column(name = "`description`")
private String description;
}
I solved the problem
For anyone wondering ,one of my classes was called Show,and sql has a reserved word called show
Related
I'm trying to auto wire a repository for a simple springboot application. But When I start to do java -jar for my app it says: Field bugRepository in be.tquality.demospringdb.BugController required a bean of type 'be.tquality.demospringdb.BugRepository' that could not be found.
This is the bug repository:
package be.tquality.demospringdb;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface BugRepository extends JpaRepository<Bug, Long> {
}
and my controller
package be.tquality.demospringdb;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
#RestController
public class BugController {
#Autowired
private BugRepository bugRepository;
#GetMapping(value = "bug/findall")
public List<Bug> findAll(){
return bugRepository.findAll();
}
}
And this my actual database class
package be.tquality.demospringdb;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
#Entity
#Table(name = "BUG")
#Data
public class Bug implements Serializable {
private static final long serialVersionUID = -2343243243242432341L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
#Column(name = "TESTCASEID")
private int testcaseid;
#Column(name = "TESTSTEPID")
private int teststepid;
#Column(name = "TITLE")
private String title;
#Column(name = "DESCRIPTION")
private String description;
#Column(name = "STATUS")
private int status;
#Column(name = "ASSIGNEE")
private int assignee;
#Column(name = "PROJECTID")
private int projectid;
#Column(name = "EXPECTEDRESULT")
private String expectedresult;
#Column(name = "ACTUALRESULT")
private String actualresult;
public Bug(){}
public Bug(int testcaseid, int teststepid, String title, String description, int status,int assignee,int projectid,
String expectedresult,String actualresult) {
this.testcaseid = testcaseid;
this.teststepid = teststepid;
this.title = title;
this.description = description;
this.status = status;
this.assignee = assignee;
this.projectid = projectid;
this.expectedresult = expectedresult;
this.actualresult = actualresult;
}
}
I really don't see what I'm doing wrong and why it cannot connect to the repository. As in my ide it can connect easily
The part that's start issues when I run java -jar is
2022-11-15 14:45:27.901 WARN 32225 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bugController': Unsatisfied dependency expressed through field 'bugRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'be.tquality.demospringdb.BugRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
2022-11-15 14:45:27.905 INFO 32225 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2022-11-15 14:45:28.035 INFO 32225 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-11-15 14:45:28.095 ERROR 32225 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field bugRepository in be.tquality.demospringdb.BugController required a bean of type 'be.tquality.demospringdb.BugRepository' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'be.tquality.demospringdb.BugRepository' in your configuration.
My spring boot application
package be.tquality.demospringdb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class DemospringdbApplication {
public static void main(String[] args) {
SpringApplication.run(DemospringdbApplication.class, args);
}
}
I am currently learning spring cloud microservices and I follow a really simple example.
So I have 2 services 1 for currency exchange and 1 for currency conversion. those two services talk to each other using 'resttemplate', however, when I try to get an object from currency exchange service I get all fields apart from the integer one, which return 0.
Beans:
CurrencyExchange:
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
public class CurrencyExchange {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "currency_from")
private String from;
#Column(name = "currency_to")
private String to;
private int convertionMultiple;
private String environment;
CurrencyConversion:
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
public class CurrencyConversion {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "from_currency")
private String from;
#Column(name = "to_currency")
private String to;
private int conversionMultiple;
private int quantity;
private int totalCalculatedAmount;
private String environment;
}
Controllers:
CurrencyExchange Controller:
#RestController
#RequestMapping("/exchange")
public class exchangeCurrencyController {
#Autowired
private Environment environment;
#Autowired
private CurrencyExchangeService currencyExchangeService;
#GetMapping("/currency-exchange/from/{from}/to/{to}")
public CurrencyExchange exchangeCurrency(#PathVariable String from, #PathVariable String to) {
CurrencyExchange currencyExchange = currencyExchangeService.findByFromAndTo(from, to);
String port = environment.getProperty("local.server.port");
System.out.println(currencyExchange);
currencyExchange.setEnvironment(port);
return currencyExchange;
}
CurrencyConversion Controller:
#RestController
#RequestMapping("/conversion")
public class CurrencyConversionController {
#GetMapping("/currency-conversion/from/{from}/to/{to}/quantity/{quantity}")
public CurrencyConversion getCurrencyConversion(#PathVariable String from, #PathVariable String to, #PathVariable int quantity) {
HashMap<String, String> uriVariables = new HashMap<>();
uriVariables.put("from", from);
uriVariables.put("to", to);
ResponseEntity<CurrencyConversion> responseEntity = new RestTemplate().getForEntity("http://localhost:8000/exchange/currency-exchange/from/{from}/to/{to}", CurrencyConversion.class, uriVariables);
CurrencyConversion currencyConversion1 = responseEntity.getBody();
System.out.println(currencyConversion1);
return new CurrencyConversion(currencyConversion1.getId(), currencyConversion1.getFrom(), currencyConversion1.getTo(), currencyConversion1.getConversionMultiple(), quantity, currencyConversion1.getConversionMultiple(), currencyConversion1.getEnvironment());
}
}
I have 3 object in my data base.
id conversion_multiple environment currency_from currency_to
1 12 8000 AUS ILS
2 65 8000 USD ILS
3 424 8000 INR ILS
**Web debuging log:
ExchangeCurrency Class:
2021-12-16 21:39:00.550 DEBUG 6648 --- [nio-8000-exec-3] o.s.web.servlet.DispatcherServlet : GET "/exchange/currency-exchange/from/USD/to/ILS", parameters={}
2021-12-16 21:39:00.551 DEBUG 6648 --- [nio-8000-exec-3] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.in28minutes.microservices.currencyexchangeservice.controllers.exchangeCurrencyController#exchangeCurrency(String, String)
2021-12-16 21:39:00.552 DEBUG 6648 --- [nio-8000-exec-3] org.hibernate.SQL : select currencyex0_.id as id1_0_, currencyex0_.conversion_multiple as conversi2_0_, currencyex0_.environment as environm3_0_, currencyex0_.currency_from as currency4_0_, currencyex0_.currency_to as currency5_0_ from currency_exchange currencyex0_ where currencyex0_.currency_from=? and currencyex0_.currency_to=?
2021-12-16 21:39:00.557 DEBUG 6648 --- [nio-8000-exec-3] org.hibernate.SQL : select currencyex0_.id as id1_0_, currencyex0_.conversion_multiple as conversi2_0_, currencyex0_.environment as environm3_0_, currencyex0_.currency_from as currency4_0_, currencyex0_.currency_to as currency5_0_ from currency_exchange currencyex0_ where currencyex0_.currency_from=? and currencyex0_.currency_to=?
PrintCurrencyExchange{id=2, from='USD', to='ILS', convertionMultiple=65, environment='8000'}
2021-12-16 21:39:00.561 DEBUG 6648 --- [nio-8000-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/*+json, application/json, application/*+json]
2021-12-16 21:39:00.562 DEBUG 6648 --- [nio-8000-exec-3] m.m.a.RequestResponseBodyMethodProcessor : Writing [CurrencyExchange{id=2, from='USD', to='ILS', convertionMultiple=65, environment='8000'}]
2021-12-16 21:39:00.564 DEBUG 6648 --- [nio-8000-exec-3] o.s.web.servlet.DispatcherServlet : Completed 200 OK
CurrencyConversion Class**
2021-12-16 21:37:39.776 DEBUG 10736 --- [nio-8100-exec-9] o.s.web.servlet.DispatcherServlet : GET "/conversion/currency-conversion-feign/from/USD/to/ILS/quantity/1123123", parameters={}
2021-12-16 21:37:39.777 DEBUG 10736 --- [nio-8100-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.in28minutes.microservices.currencyconversionservice.controllers.CurrencyConversionController#getCurrencyConversionFeign(String, String, int)
2021-12-16 21:37:39.795 DEBUG 10736 --- [nio-8100-exec-9] o.s.w.c.HttpMessageConverterExtractor : Reading to [com.in28minutes.microservices.currencyconversionservice.beans.CurrencyConversion]
CurrencyConversion(id=2, from=USD, to=ILS, conversionMultiple=0, quantity=0, totalCalculatedAmount=0, environment=8000)
2021-12-16 21:37:39.798 DEBUG 10736 --- [nio-8100-exec-9] m.m.a.RequestResponseBodyMethodProcessor : Using 'application/json;q=0.8', given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.9, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]
2021-12-16 21:37:39.798 DEBUG 10736 --- [nio-8100-exec-9] m.m.a.RequestResponseBodyMethodProcessor : Writing [CurrencyConversion(id=2, from=USD, to=ILS, conversionMultiple=0, quantity=1123123, totalCalculatedAm (truncated)...]
2021-12-16 21:37:39.800 DEBUG 10736 --- [nio-8100-exec-9] o.s.web.servlet.DispatcherServlet : Completed 200 OK
when I print to console the response. I get
CurrencyConversion(id=2, from=USD, to=ILS, conversionMultiple=0, quantity=0, totalCalculatedAmount=0, environment=8000)
which shows String returning but int showing 0.
help me understand this.
THANKS
you need to add a #Column configuration:
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
public class CurrencyExchange {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(name = "currency_from")
private String from;
#Column(name = "currency_to")
private String to;
#Column(name = "conversion_multiple")
private int convertionMultiple;
private String environment;
}
otherwise Spring does not know which column to load the data from as the field name does not match your column name.
I try to create tables automatically with Spring Boot Jpa but it doesn't work, it doesn't execute any sql queries. I suspect it's due to the OneToMany or ManytoOne relationships. I looked up the website but failed to find a question with a similar problem. Spring doesn't execute any queries and create any tables.
Users can create many posts and each post has many comments etc.
Here are my entities:
User.java
#Entity
#Table
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column
private Long userId;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Topic> topics = new ArrayList<>();
#OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts = new ArrayList<>();
#OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();
Post.java
#Entity
#Table
public class Post {
#Id
#Column
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long postId;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "userId", referencedColumnName = "userId")
private User user;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "id", referencedColumnName = "id")
private Topic topic;
#OneToMany(mappedBy = "post", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();
Comment.java
#Entity
#Table
public class Comment {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column
private Long id;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "postId", referencedColumnName = "postId")
private Post post;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "userId", referencedColumnName = "userId")
private User user;
Topic.java
#Entity
#Table
public class Topic {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column
private Long id;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "topic", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts = new ArrayList<>();
#NotNull
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "userId", referencedColumnName = "userId")
private User user;
application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/portfolio2?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=ecommerceapp
spring.datasource.password=ecommerceapp
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-
auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
application start logs:
2020-10-17 18:56:17.694 INFO 2196 --- [ main] c.j.s.SpringPortfolioApplication : Starting SpringPortfolioApplication on Laptop with PID 2196 (C:\Users\UserOld.Laptop\IdeaProjects\spring-portfolio\target\classes started by UserOld in C:\Users\UserOld.Laptop\IdeaProjects\spring-portfolio)
2020-10-17 18:56:17.694 INFO 2196 --- [ main] c.j.s.SpringPortfolioApplication : No active profile set, falling back to default profiles: default
2020-10-17 18:56:18.147 WARN 2196 --- [kground-preinit] o.s.h.c.j.Jackson2ObjectMapperBuilder : For Jackson Kotlin classes support please add "com.fasterxml.jackson.module:jackson-module-kotlin" to the classpath
2020-10-17 18:56:18.553 INFO 2196 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-10-17 18:56:18.585 INFO 2196 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 23ms. Found 0 JPA repository interfaces.
2020-10-17 18:56:19.303 INFO 2196 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-10-17 18:56:19.319 INFO 2196 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-10-17 18:56:19.319 INFO 2196 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38]
2020-10-17 18:56:19.491 INFO 2196 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-10-17 18:56:19.491 INFO 2196 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1735 ms
2020-10-17 18:56:19.663 INFO 2196 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-17 18:56:19.699 INFO 2196 --- [ task-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-10-17 18:56:19.730 WARN 2196 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-10-17 18:56:19.730 INFO 2196 --- [ task-1] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.21.Final
2020-10-17 18:56:19.855 INFO 2196 --- [ task-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-10-17 18:56:19.949 INFO 2196 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-10-17 18:56:20.011 WARN 2196 --- [ main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2020-10-17 18:56:20.105 INFO 2196 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-10-17 18:56:20.121 INFO 2196 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2020-10-17 18:56:20.168 INFO 2196 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-17 18:56:20.168 INFO 2196 --- [ main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-10-17 18:56:20.168 INFO 2196 --- [ main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-10-17 18:56:20.183 INFO 2196 --- [ main] c.j.s.SpringPortfolioApplication : Started SpringPortfolioApplication in 2.932 seconds (JVM running for 3.878)
What is my mistake? Thank you!
The reason was that dll-auto doesn't work without Jpa or Crud repository so i created them and it started to work.
Here's my question. I am unable to write the JSON after I send a POST request from POSTMAN to create/add a row into my SQL database. I need the server to send back a response of the added entry in order to retrieve the ID that SQL generates.
I face this issue when the new Case entry is a child of two entities(User and Application) and a grandchild of one entity(Owner).
*User(Owner)
|\
| *Application
|/
*Case
I'm new and currently using the Spring Boot JPA package.
I'm aware that many have asked questions related to the error above. There is none regarding my case to my knowledge. All of them refer to fixes over a HTTP GET method. If you found some please guide me to them. Or please help to answer my query. Any help is appreciated!
I attach my codes here:
User Entity
package com.lightlce.CRUD.Entities;
import javax.persistence.*;
#Entity(name = "User")
#Table(name = "User", schema = "dbo")
public class User {
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
private String staffId;
private String username;
private String role;
public User (){}
public User(Integer id, String staffId, String username, String role) {
this.id = id;
this.staffId = staffId;
this.username = username;
this.role = role;
}
// Getters and Setters
}
Application Entity
package com.lightlce.CRUD.Entities;
import javax.persistence.*;
#Entity(name = "Application")
#Table(name = "InvApplication", schema = "dbo")
public class Application {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String applicationName;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "ownerId")
private User owner;
public Application(){}
public Application(Integer id, String applicationName, User owner) {
this.id = id;
this.applicationName = applicationName;
this.owner = owner;
}
// Getters and Setters
}
Case Entity
package com.lightlce.CRUD.Entities;
import com.lightlce.CRUD.AuditModel.Auditable;
import javax.persistence.*;
#Entity(name = "Case")
#Table(name = "InvCase", schema = "dbo")
public class Case extends Auditable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "userId")
private User user;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "applicationId")
private Application application;
//redacted
public Case() {
}
public Case(Integer id, User user, Application application) {
this.id = id;
this.user = user;
this.application = application;
}
// Getters and Setters
}
Case Controller
package com.lightlce.CRUD.Controllers;
import com.lightlce.CRUD.Entities.Application;
import com.lightlce.CRUD.Entities.Case;
import com.lightlce.CRUD.Entities.User;
import com.lightlce.CRUD.Repository.ApplicationRepository;
import com.lightlce.CRUD.Repository.CaseRepository;
import com.lightlce.CRUD.Repository.UserRepository;
import com.lightlce.CRUD.Services.ApplicationService;
import com.lightlce.CRUD.Services.CaseService;
import com.lightlce.CRUD.Services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
#RestController
public class CaseController {
#Autowired
private CaseService caseService;
#Autowired
private UserService userService;
#Autowired
private ApplicationRepository applicationRepository;
#Autowired
private UserRepository userRepository;
#Autowired
private CaseRepository caseRepository;
#Autowired
private UserController userController;
#RequestMapping("cases")
public Page<Case> getAllCases(Pageable pageable) {
return caseService.getAllCases(pageable);
}
#PostMapping("cases/add")
public Case addCase(#RequestBody Case aCase) {
User staff = userService.searchUser(aCase.getUser()); //Finds the user based on ID provided
Application application = applicationRepository.findById(aCase.getApplication().getId()).get(); //Finds the application based on ID provided
aCase.setUser(staff);
aCase.setApplication(application);
return caseService.addCase(aCase);
}
}
Case Service
package com.lightlce.CRUD.Services;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.lightlce.CRUD.Entities.Application;
import com.lightlce.CRUD.Entities.Case;
import com.lightlce.CRUD.Entities.User;
import com.lightlce.CRUD.Repository.CaseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
#Service
public class CaseService {
#Autowired
private CaseRepository caseRepository;
#Autowired
private UserService userService;
public Page<Case> getAllCases(Pageable pageable){
return caseRepository.customFindAll(pageable);
}
public Case addCase(Case aCase) {
caseRepository.save(aCase);
return aCase;
}
}
Case Repository
package com.lightlce.CRUD.Repository;
import com.lightlce.CRUD.Entities.Case;
import com.lightlce.CRUD.Entities.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
#Repository
public interface CaseRepository extends JpaRepository<Case, Integer>{
Page<Case> findAll(Pageable pageable);
#Query(value = "SELECT c FROM com.lightlce.CRUD.Entities.Case c " +
"JOIN FETCH c.user u " +
"JOIN FETCH c.application a " +
"JOIN FETCH a.owner o",
countQuery = "SELECT COUNT(c) FROM com.lightlce.CRUD.Entities.Case c " +
"JOIN c.user u " +
"JOIN c.application a " +
"JOIN a.owner o")
Page<Case> customFindAll(Pageable pageable);
}
POST http://localhost:8080/cases/add
{
"user": {
"staffId": "TEST123"
},
"application":{
"id": 2
}
}
Expected Response
{
"created_at": "2020-05-13T09:34:04.093+0000",
"modified_at": "2020-05-13T09:34:04.093+0000",
"id": 1
"user": {
"id": 1,
"staffId": "TEST123",
"username": "lightlce",
"role": "admin"
},
"application": {
"id": 2,
"applicationName": "ApplicationDemo",
"owner": {
"id": 1,
"staffId": "TEST123",
"username": "lightlce",
"role": "admin"
}
}
}
Postman Exception
{
"timestamp": "2020-05-14T02:36:40.999+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Could not write JSON: could not initialize proxy [com.lightlce.CRUD.Entities.User#2] - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy [com.lightlce.CRUD.Entities.User#2] - no Session (through reference chain: com.lightlce.CRUD.Entities.Case[\"application\"]->com.lightlce.CRUD.Entities.Application[\"owner\"]->com.lightlce.CRUD.Entities.User$HibernateProxy$QRpaILkJ[\"staffId\"])",
"path": "/cases/add"
}
Springboot Logs
2020-05-14 10:36:38.262 DEBUG 50878 --- [nio-8080-exec-6] org.hibernate.SQL :
select
user0_.id as id1_0_,
user0_.role as role2_0_,
user0_.staffId as staffId3_0_,
user0_.username as username4_0_
from
dbo.InvAllUser user0_
where
user0_.staffId=?
Hibernate:
select
user0_.id as id1_0_,
user0_.role as role2_0_,
user0_.staffId as staffId3_0_,
user0_.username as username4_0_
from
dbo.InvAllUser user0_
where
user0_.staffId=?
2020-05-14 10:36:38.278 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [TEST123]
2020-05-14 10:36:38.808 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor : extracted value ([id1_0_] : [INTEGER]) - [1]
2020-05-14 10:36:38.811 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor : extracted value ([role2_0_] : [VARCHAR]) - [admin]
2020-05-14 10:36:38.812 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor : extracted value ([staffId3_0_] : [VARCHAR]) - [TEST123]
2020-05-14 10:36:38.812 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor : extracted value ([username4_0_] : [VARCHAR]) - [lightlce]
2020-05-14 10:36:38.837 DEBUG 50878 --- [nio-8080-exec-6] org.hibernate.SQL :
select
applicatio0_.id as id1_1_0_,
applicatio0_.applicationName as applicat2_1_0_,
applicatio0_.ownerId as ownerId3_1_0_
from
dbo.InvApplication applicatio0_
where
applicatio0_.id=?
Hibernate:
select
applicatio0_.id as id1_1_0_,
applicatio0_.applicationName as applicat2_1_0_,
applicatio0_.ownerId as ownerId3_1_0_
from
dbo.InvApplication applicatio0_
where
applicatio0_.id=?
2020-05-14 10:36:38.839 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [2]
2020-05-14 10:36:39.427 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor : extracted value ([applicat2_1_0_] : [VARCHAR]) - [ApplicationDemo]
2020-05-14 10:36:39.427 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicExtractor : extracted value ([ownerId3_1_0_] : [INTEGER]) - [2]
2020-05-14 10:36:39.546 DEBUG 50878 --- [nio-8080-exec-6] org.hibernate.SQL :
insert
into
dbo.InvCase
(created_at, modified_at, applicationId, approverId, caseDesc, caseTitle, caseType, status, userId)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
insert
into
dbo.InvCase
(created_at, modified_at, applicationId, approverId, caseDesc, caseTitle, caseType, status, userId)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
2020-05-14 10:36:39.553 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [TIMESTAMP] - [Thu May 14 10:36:39 SGT 2020]
2020-05-14 10:36:39.555 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [TIMESTAMP] - [Thu May 14 10:36:39 SGT 2020]
2020-05-14 10:36:39.555 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [3] as [INTEGER] - [2]
2020-05-14 10:36:39.555 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [4] as [INTEGER] - [1]
2020-05-14 10:36:39.555 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [5] as [VARCHAR] - [ApplicationDemo]
2020-05-14 10:36:39.555 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [6] as [VARCHAR] - [TEST123]
2020-05-14 10:36:39.555 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [7] as [VARCHAR] - [new]
2020-05-14 10:36:39.556 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [8] as [VARCHAR] - [Pending]
2020-05-14 10:36:39.557 TRACE 50878 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder : binding parameter [9] as [INTEGER] - [1]
2020-05-14 10:36:40.987 WARN 50878 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: could not initialize proxy [com.lightlce.CRUD.Entities.User#2] - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy [com.lightlce.CRUD.Entities.User#2] - no Session (through reference chain: com.lightlce.CRUD.Entities.Case["application"]->com.lightlce.CRUD.Entities.Application["owner"]->com.lightlce.CRUD.Entities.User$HibernateProxy$QRpaILkJ["staffId"])]
There are as many problems with your code as i really don't know how to help to solve it.
but as a quick fix try to create a new entity class for user object.
Solved it with this workaround.
Since fooService.save(foo); also updates the foo object with the ID, I can simply return foo.getId() to achieve what I want without sending another find method.
i.e. Case Service
public Integer updateCase(Case aCase) {
caseRepository.save(aCase);
return aCase.getId();
}
I'm getting around 10 errors when I start my Spring Boot app using Hibernate and postgresql. How can I fix them? WHat's the problem?
Here are my entities:
Customer.java
#Entity
#Access(AccessType.FIELD) // so I can avoid using setters for fields that won't change
public class Customer {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long customerId;
#Embedded
private FirstName firstName;
#Embedded
private LastName lastName;
#Embedded
private PhoneNumber phoneNumber;
#ManyToOne
#JoinColumn(name = "ADDRESS_ID")
private Address address;
#OneToOne
#JoinColumn(name = "USER_ID")
private User user;
// jpa requirement
public Customer() {
}
public Customer(FirstName firstName, LastName lastName,
PhoneNumber phoneNumber, Address address, User user) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.address = address;
this.user = user;
}
public Long getCustomerId() {
return customerId;
}
public FirstName getFirstName() {
return firstName;
}
public LastName getLastName() {
return lastName;
}
public PhoneNumber getPhoneNumber() {
return phoneNumber;
}
// setter for phone number is needed because customer can change his phone number
public void setPhoneNumber(PhoneNumber phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Address getAddress() {
return address;
}
// setter for address is needed because customer can change his address
public void setAddress(Address address) {
this.address = address;
}
public User getUser() {
return user;
}
#Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Customer customer = (Customer) o;
if (firstName != null ? !firstName.equals(customer.firstName) : customer.firstName != null) {
return false;
}
if (lastName != null ? !lastName.equals(customer.lastName) : customer.lastName != null) {
return false;
}
return user != null ? user.equals(customer.user) : customer.user == null;
}
#Override
public int hashCode() {
int result = firstName != null ? firstName.hashCode() : 0;
result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
result = 31 * result + (user != null ? user.hashCode() : 0);
return result;
}
#Override
public String toString() {
return "Customer{" +
"firstName=" + firstName +
", lastName=" + lastName +
", phoneNumber=" + phoneNumber +
'}';
}
}
User.java
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
#Embedded
private EmailAddress emailAddress;
#Embedded
private Password password;
#OneToOne
#JoinColumn(name = "CUSTOMER_ID")
private Customer customer;
#Column
#Enumerated(EnumType.STRING)
private UserRole userRole;
// jpa requirement
public User() {
}
public User(EmailAddress emailAddress, Password password) {
this.emailAddress = emailAddress;
this.password = password;
this.userRole = UserRole.USER; // on creation everyone is just a user
}
public Long getUserId() {
return userId;
}
public EmailAddress getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(EmailAddress emailAddress) {
this.emailAddress = emailAddress;
}
public Password getPassword() {
return password;
}
public void setPassword(Password password) {
this.password = password;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public UserRole getUserRole() {
return userRole;
}
public void setUserRole(UserRole userRole) {
this.userRole = userRole;
}
}
And Book.java:
#Entity
#Access(AccessType.FIELD) // so I can avoid using setters for fields that won't change
public class Book {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long bookId;
#Embedded
private Isbn isbn;
#Embedded
private Title title;
#Embedded
private Author author;
#Embedded
private Genre genre;
private Year publicationYear;
private BigDecimal price;
// jpa requirement
public Book() {
}
public Book(Isbn isbn, Title title, Author author, Genre genre, Year publicationYear,
BigDecimal price) {
this.isbn = isbn;
this.title = title;
this.author = author;
this.genre = genre;
this.publicationYear = publicationYear;
this.price = price;
}
public Long getBookId() {
return bookId;
}
public Isbn getIsbn() {
return isbn;
}
public Title getTitle() {
return title;
}
public Author getAuthor() {
return author;
}
public Genre getGenre() {
return genre;
}
public BigDecimal getPrice() {
return price;
}
public Year getPublicationYear() {
return publicationYear;
}
// setter for price is needed because price of the book can change (discounts and so on)
public void setPrice(BigDecimal price) {
this.price = price;
}
}
My application.properties for postgresql settings:
spring.datasource.url= jdbc:postgresql://localhost:5432/bookrest
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create-drop
And here is the stack trace for hibernate part (ommitted all the controoller mapping):
2017-02-27 11:03:05.481 INFO 6563 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-02-27 11:03:05.555 INFO 6563 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.0.11.Final}
2017-02-27 11:03:05.556 INFO 6563 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-02-27 11:03:05.558 INFO 6563 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-02-27 11:03:05.609 INFO 6563 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-02-27 11:03:05.819 INFO 6563 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2017-02-27 11:03:05.997 INFO 6563 --- [ restartedMain] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2017-02-27 11:03:05.999 INFO 6563 --- [ restartedMain] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType#c866efb
2017-02-27 11:03:06.228 WARN 6563 --- [ restartedMain] org.hibernate.orm.deprecation : HHH90000014: Found use of deprecated [org.hibernate.id.SequenceGenerator] sequence-based id generator; use org.hibernate.id.enhanced.SequenceStyleGenerator instead. See Hibernate Domain Model Mapping Guide for details.
2017-02-27 11:03:06.580 INFO 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2017-02-27 11:03:06.584 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer drop constraint FKglkhkmh2vyn790ijs6hiqqpi
2017-02-27 11:03:06.584 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "customer" does not exist
2017-02-27 11:03:06.584 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer drop constraint FKidmyb2vdwmk3o502u0rg8g32h
2017-02-27 11:03:06.585 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "customer" does not exist
2017-02-27 11:03:06.585 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table user drop constraint FK2q989f4c89rv2b9xvtomfc0fs
2017-02-27 11:03:06.586 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user"
Position: 13
2017-02-27 11:03:06.587 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: drop table if exists user cascade
2017-02-27 11:03:06.587 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user"
Position: 22
2017-02-27 11:03:06.588 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: drop sequence hibernate_sequence
2017-02-27 11:03:06.588 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: sequence "hibernate_sequence" does not exist
2017-02-27 11:03:06.607 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: create table user (user_id bigserial not null, email_address varchar(255), password varchar(255), user_role varchar(255), customer_customer_id int8, primary key (user_id))
2017-02-27 11:03:06.607 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user"
Position: 14
2017-02-27 11:03:06.610 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer add constraint FKidmyb2vdwmk3o502u0rg8g32h foreign key (user_user_id) references user
2017-02-27 11:03:06.610 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user"
Position: 103
2017-02-27 11:03:06.610 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table user add constraint FK2q989f4c89rv2b9xvtomfc0fs foreign key (customer_customer_id) references customer
2017-02-27 11:03:06.610 ERROR 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: syntax error at or near "user"
Position: 13
2017-02-27 11:03:06.611 INFO 6563 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
2017-02-27 11:03:06.673 INFO 6563 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
EDIT 1:
After changing my User to AppUser (table name user is prohibited) I'm still getting few errors:
2017-02-27 11:45:44.253 INFO 9560 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.0.11.Final}
2017-02-27 11:45:44.255 INFO 9560 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2017-02-27 11:45:44.256 INFO 9560 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2017-02-27 11:45:44.298 INFO 9560 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2017-02-27 11:45:44.621 INFO 9560 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2017-02-27 11:45:44.825 INFO 9560 --- [ restartedMain] o.h.e.j.e.i.LobCreatorBuilderImpl : HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2017-02-27 11:45:44.828 INFO 9560 --- [ restartedMain] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType#35c0ac4e
2017-02-27 11:45:45.370 INFO 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl schema export
2017-02-27 11:45:45.373 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table app_user drop constraint FKf8cjd2mkc4tu1u5nhju0clae7
2017-02-27 11:45:45.374 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "app_user" does not exist
2017-02-27 11:45:45.374 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer drop constraint FKglkhkmh2vyn790ijs6hiqqpi
2017-02-27 11:45:45.374 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "customer" does not exist
2017-02-27 11:45:45.375 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table customer drop constraint FKslkyb5dphxe4c7au3hqx3la6m
2017-02-27 11:45:45.375 ERROR 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : ERROR: relation "customer" does not exist
2017-02-27 11:45:45.408 INFO 9560 --- [ restartedMain] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema export complete
2017-02-27 11:45:45.467 INFO 9560 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
user is a reserved keyword in postgresql. I would suggest renaming your entity class to some other name, or just use hibernate annotations #Table to specify postgresql friendly table name, like this:
#Entity
#Table(name = "library_user")
public class User {...}
User is reserved word in most if not all of the databases. Use some other name for this purpose.
Words