Could not determine type for: org.springframework.web.multipart.MultipartFile

不念不忘少年蓝@ 2024-04-23 21:18 166阅读 0赞

SpringBoot启动时报错:

Caused by: 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: attachment, for columns: [org.hibernate.mapping.Column(file)]

原因是有一个实体类里用MultipartFile类型定义了一个文件变量,本意是想用来存储文件的,代码如下:

  1. /**
  2. * file
  3. */
  4. @Column(nullable = false)
  5. private MultipartFile file;

这里重点关注这句话:

  1. Could not determine type for: org.springframework.web.multipart.MultipartFile

意思是自动建表的时候编译器不能确定MultipartFile这个类型在数据库中应该对应什么类型,而文件在数据库中是以二进制存储的,所以把实体类的代码稍作修改:

  1. /**
  2. * file
  3. */
  4. @Column(nullable = false,columnDefinition = "MediumBlob")
  5. private byte[] file;

然后启动,问题解决。

tips:

MySQL的四种BLOB类型

类型 大小(单位:字节)
TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G

发表评论

表情:
评论列表 (有 0 条评论,166人围观)

还没有评论,来说两句吧...

相关阅读