Reputation: 1328
I am trying to add an entry in my MySQL database, using a pretty basic (so far) Spring Boot app. I've used some bits and pieces I've found online and this is the code that I'm trying to follow:
netgloo/spring-boot-samples/spring-boot-mysql-springdatajpa-hibernate
Currently I get the following issues when I run my app.
When I first run the app the following Exception is thrown:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL via JDBC Statement
And when I make a POST request to the controller, which should create a new entry in the database I get the following Exception:
org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibernate.exception.GenericJDBCException: could not execute statement
which in turn is caused by the following:
Caused by: java.sql.SQLException: No database selected
application.properties:
# Web pages extension
spring.mvc.view.suffix =.html
# ==============================
# --- DATA SOURCE
# ==============================
# Database url
spring.datasource.url = jdbc:mysql://localhost:3306?useSSL=false
# Database credentials
spring.datasource.username = root
spring.datasource.password = root
# Keep database connection alive
spring.datasource.tomcat.test-while-idle = true
spring.datasource.tomcat.validation-query = SELECT 1
# ==============================
# --- JPA / Hibernate
# ==============================
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming.strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>my.group</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myapp</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.12.Final</version>
</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.json</groupId>
<artifactId>json</artifactId>
<version>20171018</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
I have this simple interface:
@Transactional
public interface UserDAO extends CrudRepository<User, Long>
{
public User findByEmail(String email);
}
And here's my controller:
@Controller
public class UserController
{
@Autowired
private UserDAO userDAO;
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String getRegisterView()
{
return "register/registerView";
}
@RequestMapping(value = "/register", method = RequestMethod.POST)
public @ResponseBody
ControllerResponse registerRequest(@RequestBody String request)
{
User user = null;
JSONObject json = new JSONObject(request);
try
{
user = new User();
user.setEmail(json.getString("email"));
user.setName(json.getString("name"));
user.setSurname(json.getString("surname"));
user.setPassword(json.getString("password"));
userDAO.save(user);
}
catch (Exception e)
{
e.printStackTrace();
}
ControllerResponse response = new ControllerResponse();
response.setMessage("Controller returned this");
response.setSuccess(true);
return response;
}
}
My User class is defined as follows:
public class User
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private String email;
@NotNull
private String password;
@NotNull
private String name;
@NotNull
private String surname;
@Column(name = "authorization_token")
private String authorizationToken;
// bunch of getters/setters
}
Upvotes: 3
Views: 27953
Reputation: 165
Adding this configuration in application.properties fixed this issue.
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
Upvotes: 4
Reputation: 2858
Your datasource url in application.properties is missing database name. Should be like this.
spring.datasource.url = jdbc:mysql://localhost:3306/dbname?useSSL=false
Upvotes: 5