前提
Springを用いてMySQLにデータを挿入し、挿入したデータを表示させようとしています。
おそらくCrudRepositoryのCRUDメソッドのsaveを使用したら以下のエラーが発生しました。
Failed to update entity [Sample(id=4, name=Okada, gender=0, age=40)]. Id [4] not found in database.
上記の(id,name,gender,age)をデータベース内に挿入したいのですが、Id(4)がデータベースに見つかりませんと出てしました。
実現したいこと
- MySQLにEclipse上で定義した値を挿入する
- 挿入した値を表示する
発生している問題・エラーメッセージ
Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.data.relational.core.conversion.DbActionExecutionException: Failed to execute DbAction.UpdateRoot(entity=Sample(id=4, name=Okada, gender=0, age=40)) at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:95) at org.springframework.data.jdbc.core.AggregateChangeExecutor.lambda$execute$0(AggregateChangeExecutor.java:52) at java.base/java.util.ArrayList.forEach(ArrayList.java:1541) at org.springframework.data.relational.core.conversion.DefaultAggregateChange.forEachAction(DefaultAggregateChange.java:127) at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:52) at org.springframework.data.jdbc.core.JdbcAggregateTemplate.store(JdbcAggregateTemplate.java:360) at org.springframework.data.jdbc.core.JdbcAggregateTemplate.save(JdbcAggregateTemplate.java:161) at org.springframework.data.jdbc.repository.support.SimpleJdbcRepository.save(SimpleJdbcRepository.java:75) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289) at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:530) at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:286) at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:640) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164) at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:139) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) at com.sun.proxy.$Proxy66.save(Unknown Source) at com.example.demo.SpringMySqlSampleApplication.executeInsert(SpringMySqlSampleApplication.java:27) at com.example.demo.SpringMySqlSampleApplication.execute(SpringMySqlSampleApplication.java:22) at com.example.demo.SpringMySqlSampleApplication.main(SpringMySqlSampleApplication.java:15) ... 5 more Caused by: org.springframework.dao.IncorrectUpdateSemanticsDataAccessException: Failed to update entity [Sample(id=4, name=Okada, gender=0, age=40)]. Id [4] not found in database. at org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.updateWithoutVersion(JdbcAggregateChangeExecutionContext.java:316) at org.springframework.data.jdbc.core.JdbcAggregateChangeExecutionContext.executeUpdateRoot(JdbcAggregateChangeExecutionContext.java:109) at org.springframework.data.jdbc.core.AggregateChangeExecutor.execute(AggregateChangeExecutor.java:72) ... 39 more
該当のソースコード
エンティティクラス
Sample.java
1package com.example.demo.entity; 2 3import org.springframework.data.annotation.Id; 4 5import lombok.AllArgsConstructor; 6import lombok.Data; 7import lombok.NoArgsConstructor; 8 9@Data 10@NoArgsConstructor 11@AllArgsConstructor 12public class Sample { 13 @Id 14 private Integer id; 15 private String name; 16 private Integer gender; 17 private Integer age; 18}
レポジトリーインターフェース
SampleCrudRepository
1package com.example.demo.repository; 2 3import org.springframework.data.repository.CrudRepository; 4 5import com.example.demo.entity.Sample; 6 7public interface SampleCrudRepository extends CrudRepository<Sample, Integer> { 8}
処理内容
SpringMySqlSampleApplication.java
1package com.example.demo; 2 3import org.springframework.beans.factory.annotation.Autowired; 4import org.springframework.boot.SpringApplication; 5import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7import com.example.demo.entity.Sample; 8import com.example.demo.repository.SampleCrudRepository; 9 10@SpringBootApplication 11public class SpringMySqlSampleApplication { 12 13 public static void main(String[] args) { 14 SpringApplication.run(SpringMySqlSampleApplication.class, args) 15 .getBean(SpringMySqlSampleApplication.class).execute(); 16 } 17 18 @Autowired 19 SampleCrudRepository repository; 20 21 private void execute() { 22 executeInsert(); 23 } 24 25 private void executeInsert() { 26 Sample sample = new Sample(4, "Okada", 0, 40); 27 sample = repository.save(sample); 28 System.out.println(sample); 29 } 30}
初期設定(bulid gradleのdependencies部分)
bulid.gradle
1dependencies { 2 implementation 'org.springframework.boot:spring-boot-starter-data-jdbc' 3 implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' 4 implementation 'org.springframework.boot:spring-boot-starter-web' 5 compileOnly 'org.projectlombok:lombok' 6 developmentOnly 'org.springframework.boot:spring-boot-devtools' 7 runtimeOnly 'mysql:mysql-connector-java' 8 annotationProcessor 'org.projectlombok:lombok' 9 testImplementation 'org.springframework.boot:spring-boot-starter-test' 10}
application.properties
1spring.datasource.url=jdbc:mysql://localhost/sampledb 2spring.datasource.username=root 3spring.datasource.password=******** 4spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
試したこと
補足情報(FW/ツールのバージョンなど)
MySQL ver8.0?(最新版です)
Spring Boot ver2.7.4
タイプ Gradle
回答1件
あなたの回答
tips
プレビュー