简体   繁体   English

如何使用 Hibernate & Spring Data JPA 正确注释两个实体之间的这种关系?

[英]How can I properly annotate this relationship between two entities using Hibernate & Spring Data JPA?

Goal:目标:

I am building a web app where there is an overview.我正在构建一个有概述的网络应用程序。 In this overview, I load classifieds from a backend.在本概述中,我从后端加载分类广告。 Each classified has exactly one thumbnail.每个分类都有一个缩略图。

First, the overview loads the classified, then it attempts to look up the associated thumbnail in the relevant Spring Data JPA Repository using the ID of the Classified.首先,概览加载分类,然后尝试使用分类的 ID 在相关的 Spring Data JPA 存储库中查找关联的缩略图。

Current state: The Classified table looks OK.当前状态:分类表看起来不错。 The Thumbnail table is missing a reference to the Classified.缩略图表缺少对分类的引用。

Desired state: I want the Thumbnail table to contain a reference to the ID of the classified, so that I can query the Thumbnail of a Classified.期望状态:我希望 Thumbnail 表包含对分类 ID 的引用,以便我可以查询分类的 Thumbnail。

ERD(~ish): ERD(~ish):

在此处输入图片说明

Entity classes:实体类:

Classified:分类:

import javax.persistence.*;

@Entity
public class Classified {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String summary;
    private String description;

    public Classified() { }

    public Classified(String summary, String description) {
        this.summary = summary;
        this.description = description;
    }

    public long getId() {
        return id;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

Thumbnail:缩略图:

import javax.persistence.*;

@Entity
public class Thumbnail {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @OneToOne
    @JoinColumn(name = "id")
    private Classified classified;

    @Column(name="bytes", columnDefinition="VARBINARY(10000)")
    private byte[] bytes;

    public Thumbnail() {
    }

    public Thumbnail(Classified classified, byte[] bytes) {
        this.classified = classified;
        this.bytes = bytes;
    }
}

Tables (initiated with some sample data):表格(以一些样本数据开始):

Classified:分类: 在此处输入图片说明

Thumbnail:缩略图: 在此处输入图片说明

You should change only the name of JoinColumn in the thumbnail class, for example:您应该只更改缩略图类中 JoinColumn 的名称,例如:

@JoinColumn(name = "classifiedId",referencedColumnName = "id")
private Classified classified;

to give a name for the column为列命名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring Boot JPA在这两个类之间建立一对一的关系?另外,如何保存实体? - How can i establish a one to one relationship between this two classes using spring boot jpa?, also, how do i save the entities? 不同方案的两个实体之间的关系 - Spring Boot Data JPA - Relationship between two entities of different schemes - Spring Boot Data JPA 如何正确注释休眠实体 - How properly annotate hibernate entities 如何在Spring Data JPA或Hibernate中存储实体 - How to store entities in Spring Data JPA or hibernate Spring 数据 JPA:如何使用注释连接两个实体 - Spring Data JPA: How to join two entities using annotations 使用 Spring 引导,如何在新表上创建 2 个实体之间的关系并为其提供额外的列? - Using Spring Boot, how can I create the relationship between 2 entities on a new table and give it extra columns? 如何在多对多关系中使用hibernate和JPA删除孤立实体? - How do I delete orphan entities using hibernate and JPA on a many-to-many relationship? Spring / JPA / Hibernate如何在一个存储库中执行两个实体的联接 - Spring/JPA/Hibernate How to Perform Join of Two Entities In One Repository 如何使用 REST 和 JPA/Hibernate 正确更新实体 - How to properly update entities using REST and JPA/Hibernate Spring数据jpa和具有某种关系的实体 - Spring data jpa and entities with some kind of relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM