休眠:使用外键保存实体 [英] Hibernate: saving an entity with foreign primary key

查看:41
本文介绍了休眠:使用外键保存实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个实体( Product Feedback )的数据库.反馈有一个引用Product的外键,其主键是一个整数.因此,它们之间是一对一的关系.

I have a database with two entities (Product and Feedback). Feedback has a single foreign key that refers to Product, whose primary key is an integer. They are therefore in a one-to-one relationship.

我想通过Web应用程序向数据库添加一个新的 Feedback 条目.为此,我尝试从Angular客户端应用程序向Spring服务器执行 PUT请求.

I want to add a new Feedback entry to the database with my web application. To do so I'm trying to perform a PUT request to my Spring server from my Angular client application.

客户端具有 Feedback 类的Object,具有以下属性( Feedback.ts 的内容)

The client has an Object of class Feedback with the following attributes (content of Feedback.ts)

export class Feedback {
  constructor(
    public productId: number,
    public score: number,
    public preferred: boolean,
    public textNote: string
  ) {
  }
}

服务器端的类 Feedback.java :

@Entity
@Table(name = "feedback")
public class Feedback implements Serializable {
    @Id
    @JoinColumn(name = "product", unique = true)
    @OneToOne(cascade = CascadeType.ALL)
    private Product product;

    @Column(name = "score")
    private double score;

    @Column(name = "preferred")
    private boolean preferred;

    @Column(name = "textnote")
    private String textNote;

    // ... getters, setters, constructor

}

如您所见,存在一个不匹配的属性: productId 是一个 number ,而 product Product类.如何为Spring应用程序提供正确的对象,以便在数据库中保存?

As you can see there is a non-matching attribute: productId is a number, while product is of class Product. How can I give the Spring application the correct object in order to save it in the database?

我正在尝试遵循Hibernate准则,据我所知,在类型为 int 的java类中使用属​​性而不是 Product 是一个不好的做法.代码>.我该怎么办?

I'm trying to follow the Hibernate guidelines and as far as I understood it would be a bad practice to use an attribute in the java class of type int instead of Product. What should I do?

推荐答案

您使用FE中的productId创建了一个新产品.现在,您可以创建一个设置了所有类型的新反馈对象.然后可以将其存储在数据库中.

You create a new Product with the productId from the FE. Now you can create a new Feedback object with all types set. This you can then store in the database.

通常,后端中有数据传输对象(DTO).顾名思义,这些将表明传输数据没有其他作用.后端中的端点将始终获得与FE中相同的DTO.在您的情况下,创建一个FeedbackDto.

Usually, you have data transfer object (DTO)in the backend. Those will as the name suggests transport data do nothing else. Endpoints in the backend will always get DTOs which are the same as in the FE. In your case create a FeedbackDto.

public class FeedbackDto {
   Long productId;
   Double score;
   Boolean preferred;
   String textNote;
}

端点将接收到此对象,该对象的字段与来自FE的反馈相同.Spring将根据请求的JSON正文中的值创建并填充您的对象

The Endpoint will receive this Object which has the same fields as the Feedback from the FE. Spring will create and fill you the object from the values in the JSON body of the request

从FeedbackD到您现在创建一个Feedback实体:

From the FeedbackDto you create now a Feedback entity:

new Feedback(new Product(feedbackDto.productId), feedbackDto.score, feedbackDto.preferred, feedbackDto.textNote)

现在,您可以将此反馈实体保存到数据库中.

Now you can save this Feedback entity into your database.

这篇关于休眠:使用外键保存实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆