一对一关系 JPA 与 MultipartFile

huangapple go评论80阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2020年7月23日 08:24:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63045019.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定