Field 'id' doesn't have a default value. AbstractPersistable problem

I'm crating webservice with connection to db. I have met one problem. In my User class i used AbstractPersistable to generate primary keys but it seems to have some problem with that task.

import javax.persistence.Entity;
import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
public class User extends AbstractPersistable<Long>{ private String userId; private String userName; private String password; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }
}

It throws me something like that:

 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.0.RELEASE)
2020-05-27 14:22:09.345 INFO 15080 --- [ restartedMain] com.webservice.Application : Starting Application on DESKTOP-7IMDU22 with PID 15080 (C:\Users\Daniel\eclipse-workspace\MyWebService\target\classes started by Daniel in C:\Users\Daniel\eclipse-workspace\MyWebService)
2020-05-27 14:22:09.348 INFO 15080 --- [ restartedMain] com.webservice.Application : No active profile set, falling back to default profiles: default
2020-05-27 14:22:09.405 INFO 15080 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-05-27 14:22:09.405 INFO 15080 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-05-27 14:22:10.477 INFO 15080 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-05-27 14:22:10.502 INFO 15080 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 16ms. Found 0 JPA repository interfaces.
2020-05-27 14:22:11.549 INFO 15080 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 9090 (http)
2020-05-27 14:22:11.560 INFO 15080 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-05-27 14:22:11.560 INFO 15080 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-05-27 14:22:11.684 INFO 15080 --- [ restartedMain] o.a.c.c.C.[.[localhost].[/springboot] : Initializing Spring embedded WebApplicationContext
2020-05-27 14:22:11.684 INFO 15080 --- [ restartedMain] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2279 ms
2020-05-27 14:22:12.050 INFO 15080 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-27 14:22:12.102 INFO 15080 --- [ task-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-05-27 14:22:12.137 WARN 15080 --- [ restartedMain] 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-05-27 14:22:12.170 INFO 15080 --- [ task-1] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.15.Final
2020-05-27 14:22:12.358 INFO 15080 --- [ task-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-05-27 14:22:12.490 INFO 15080 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-05-27 14:22:12.741 INFO 15080 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-05-27 14:22:12.758 INFO 15080 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-05-27 14:22:13.435 INFO 15080 --- [ task-1] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-05-27 14:22:13.442 INFO 15080 --- [ task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-05-27 14:22:13.462 INFO 15080 --- [ task-2] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
Exception in thread "task-2" org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/C:/Users/Daniel/eclipse-workspace/MyWebService/target/classes/data.sql]: insert into `User`(`user_id`,`user_name`,`password`) values ('user id 1', 'name 1' , 'pass1'), ('user id 2', 'name 2' , 'pass2'); nested exception is java.sql.SQLException: Field 'id' doesn't have a default value at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:622) at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:254) at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:49) at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:203) at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.initSchema(DataSourceInitializer.java:120) at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:91) at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.onApplicationEvent(DataSourceInitializerInvoker.java:38) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403) at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360) at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.publishEventIfRequired(DataSourceInitializedPublisher.java:99) at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher.access$100(DataSourceInitializedPublisher.java:50) at org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher$DataSourceSchemaCreatedPublisher.lambda$postProcessEntityManagerFactory$0(DataSourceInitializedPublisher.java:200) at (ThreadPoolExecutor.java:1167) at (ThreadPoolExecutor.java:641) at (Thread.java:844)
Caused by: java.sql.SQLException: Field 'id' doesn't have a default value at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:764) at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648) at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95) at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:601) ... 17 more
2020-05-27 14:22:13.531 INFO 15080 --- [ restartedMain] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2020-05-27 14:22:13.582 INFO 15080 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 9090 (http) with context path '/springboot'
2020-05-27 14:22:13.583 INFO 15080 --- [ restartedMain] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-05-27 14:22:13.584 INFO 15080 --- [ restartedMain] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-05-27 14:22:13.597 INFO 15080 --- [ restartedMain] com.webservice.Application : Started Application in 4.64 seconds (JVM running for 5.221)

It must generate primary keys but they dont have default value.

Here is my data.sql file:

use springboot;
insert into `User`(`user_id`,`user_name`,`password`) values
('user id 1', 'name 1' , 'pass1'),
('user id 2', 'name 2' , 'pass2');

And aplication properties

server.servlet.contextPath=/springboot
server.port=9090
menagement.security.enabled=false
#Hibernate
spring.datasource.data=classpath*:data.sql
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.initialize=true
spring.datasource.initialization-mode=always
#Jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
6

1 Answer

The following changes worked for me:

Application.properties file:

spring.jpa.hibernate.ddl-auto=create

Entity class:

@Entity
public class User { // extends AbstractPersistable<Long> @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; ..
}

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like