英文:
one to one relationship JPA with multipartfile
问题
我显然不知道如何使用JPA进行一对一关系。
文章实体
@Entity
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "files_id", referencedColumnName = "id")
private MultipartFile file;
文件实体
@Entity
@Table(name = "files")
public class DBFile {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
private String fileName;
private String fileType;
@Lob
private byte[] data;
@OneToOne(mappedBy = "file")
private Article article;
终端错误信息
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.test.webservices.article.Article.file references an unknown entity: org.springframework.web.multipart.MultipartFile
我尝试将String更改为Long来表示dbfileEntity,但在构建uri的路径时遇到了一些问题...我不明白为什么不起作用,因为我使用了@Table注释。
有人能解释一下吗?
谢谢
英文:
I apparently don't know how to do a oneToOne relationship with JPA.
Article entity
@Entity
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String content;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "files_id", referencedColumnName = "id")
private MultipartFile file;
File Entity
@Entity
@Table(name = "files")
public class DBFile {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
private String fileName;
private String fileType;
@Lob
private byte[] data;
@OneToOne(mappedBy = "files")
private Article article;
Terminal
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.test.webservices.article.Article.file references an unknown entity: org.springframework.web.multipart.MultipartFile
I tried to change String by Long to dbfileEntity but I have some trouble to build the uri's path after... I don't understand why it doesn't work since I used @Table annotation.
Could someone explain me?
Thx
答案1
得分: 2
这是一个拼写错误。在这里,您说正在通过files
进行映射:
@OneToOne(mappedBy = "files")
private Article article;
而字段名称实际上是file
:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "files_id", referencedColumnName = "id")
private MultipartFile file;
您应该修复mappedBy
属性:
@OneToOne(mappedBy = "file")
private Article article;
此外,类型错误,应该是:
private DBFile file;
英文:
It's a typo. Here you're saying you're mapping by files
:
@OneToOne(mappedBy = "files")
private Article article;
Where the field name is file
:
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "files_id", referencedColumnName = "id")
private MultipartFile file;
You should fix the mappedBy
property:
@OneToOne(mappedBy = "file")
private Article article;
Also the type is wrong, it should be:
private DBFile file;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论