やりたい事
MultipartFileクラスを使うと保持できると知り、JPAを使ってデータベースに画像を保存させて見たく試しているところです。
しかし、org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resourceというエラーが発生し止まってしまっている状態です。
調べた事
entityManagerFactory のエラーが出る場合という事で、https://qiita.com/syunchanp/items/a3be70ca10af18c2db26を参考にさせて頂きましたが、importも間違っていないようでした。
パッケージの管理がおかしいのか、JPAの仕様がそもそもMultipartFileができないのか、Bean保持ができていないのか。
どうか原因と訂正についてご教授ください。
pom
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.1.8.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.example</groupId> 12 <artifactId>Image_Upload_Test</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>Image_Upload_Test</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-data-jpa</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-thymeleaf</artifactId> 29 </dependency> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-web</artifactId> 33 </dependency> 34 35 <dependency> 36 <groupId>org.springframework.boot</groupId> 37 <artifactId>spring-boot-devtools</artifactId> 38 <scope>runtime</scope> 39 <optional>true</optional> 40 </dependency> 41 <dependency> 42 <groupId>mysql</groupId> 43 <artifactId>mysql-connector-java</artifactId> 44 <scope>runtime</scope> 45 </dependency> 46 <dependency> 47 <groupId>org.projectlombok</groupId> 48 <artifactId>lombok</artifactId> 49 <optional>true</optional> 50 </dependency> 51 <dependency> 52 <groupId>org.springframework.boot</groupId> 53 <artifactId>spring-boot-starter-test</artifactId> 54 <scope>test</scope> 55 </dependency> 56 </dependencies> 57 58 <build> 59 <plugins> 60 <plugin> 61 <groupId>org.springframework.boot</groupId> 62 <artifactId>spring-boot-maven-plugin</artifactId> 63 </plugin> 64 </plugins> 65 </build> 66 67</project>
applicationproperties
1spring.datasource.url=jdbc:mysql://localhost:3306/sampledb 2spring.datasource.username=root 3spring.datasource.password=XXX 4spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver 5spring.jpa.database=MYSQL 6spring.jpa.hibernate.ddl-auto=update 7logging.level.org.springframework.web = trace
FileUploadForm
1package com.example.demo.domain; 2 3import javax.persistence.Entity; 4import javax.persistence.GeneratedValue; 5import javax.persistence.GenerationType; 6import javax.persistence.Id; 7 8import org.springframework.web.multipart.MultipartFile; 9 10import lombok.Getter; 11import lombok.Setter; 12 13@Getter 14@Setter 15@Entity 16public class FileUploadForm { 17 @Id 18 @GeneratedValue(strategy = GenerationType.IDENTITY) 19 private Long id; 20 private MultipartFile fileData; 21 22 23}
HomeController
1ackage com.example.demo.controller; 2 3 4import org.springframework.beans.factory.annotation.Autowired; 5import org.springframework.stereotype.Controller; 6import org.springframework.web.bind.annotation.ModelAttribute; 7import org.springframework.web.bind.annotation.PostMapping; 8import org.springframework.web.bind.annotation.RequestMapping; 9 10import com.example.demo.domain.FileUploadForm; 11import com.example.demo.service.HomeService; 12 13 14 15@Controller 16public class HomeController { 17 @Autowired 18 private HomeService homeService; 19 20 @RequestMapping("/a") 21 public String index() { 22 return "index"; 23 } 24 25 @PostMapping(value = "/upload") 26 public String create(@ModelAttribute FileUploadForm file) { 27 homeService.save(file); 28 return "redirect:/index"; 29 } 30}
HomeService
1package com.example.demo.service; 2 3import org.springframework.beans.factory.annotation.Autowired; 4 5import com.example.demo.domain.FileUploadForm; 6import com.example.demo.repository.HomeRepository; 7 8public class HomeService { 9 10 @Autowired 11 private HomeRepository homeRepository; 12 public FileUploadForm save(FileUploadForm form) { 13 14 return homeRepository.save(form); 15 } 16}
HomeRepository
1package com.example.demo.repository; 2 3 4import org.springframework.data.jpa.repository.JpaRepository; 5import org.springframework.stereotype.Repository; 6 7import com.example.demo.domain.FileUploadForm; 8 9@Repository 10public interface HomeRepository extends JpaRepository<FileUploadForm, Long> { 11} 12
index
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4<meta charset="UTF-8"/> 5<title>Insert title here</title> 6</head> 7<body> 8 <form method="post" enctype="multipart/form-data" th:action="@{/upload}"> 9 <input name="fileData" type="file" /> 10 <input type="submit" /> 11 </form> 12</body> 13</html>
エラー内容
rror starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-17 18:09:46.821 ERROR 5041 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: org.springframework.web.multipart.MultipartFile, at table: file_upload_form, for columns: [org.hibernate.mapping.Column(file_data)]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
回答1件
あなたの回答
tips
プレビュー