关于在Spring Boot中插入ManyToOne关系中的数据感到困惑。

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

Confused about Inserting Data in a ManyToOne Relationship in Spring Boot

问题

Language:

import jakarta.persistence.*;

@Entity
public class Language {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String langName;
    private String paradigm;
}

User:

import jakarta.persistence.*;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    @ManyToOne(cascade = CascadeType.ALL)
    private Language language;
}

我已经创建了这两个实体。我有几名程序员,每名程序员都有一个喜爱的编程语言。多名程序员可以有相同的喜爱语言。

我正试图创建一个微服务,通过一个端点将数据发送到其中,并将其存储在数据库中。

我对如何发送我的数据感到困惑。我应该先填充 Language 实体,然后开始填充 User 实体,并在每次添加一个用户对象时,检查语言实体中是否存在相应的语言?

还是应该直接从 User 实体开始,将所有字段连同其语言一起发送,检查它是否存在于 Language 实体中,如果存在,则获取其id并将其分配给 User 外键,如果不存在,则将其插入并与 Person 对象一起插入?

我正在使用 JpaRepository 接口进行 CRUD 操作。

英文:

I have created These two Entities

Language:

import jakarta.persistence.*;

@Entity
public class Language {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String langName;
    private String paradigm;
}

User:

import jakarta.persistence.*;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;

    @ManyToOne(cascade = CascadeType.ALL)
    private Language language;
}

So we have several programmers and each programmer has a favourite Language. Multiple Programmers can have same favourite language.

I am trying to create a microService which I will send data to(via an endpoint) and get stored in a database.

I am confused as to how should I be sending my data. Should I first populate the Language Entity, then start populating the User Entity and everytime I add a Users object, I check whether the corresponding language is present in Language Entity or not?

Or should I directly start with the User Entity sending all the fields along with his/her language, make a check whether it exists in the Language Entity, if it exists fetch its id and assign it to the User foreign key, if it does not exist, then insert it along with the Person Object?

I am using the JpaRepository interface for CRUD.

答案1

得分: 1

你应该先加载语言如果你使用UI那么你必须有一些预定义的语言所以如果语言已加载你可以在UI中显示它们

在发送用户时你只需传递IDHibernate 将检查该ID是否存在然后将该ID映射到数据库中的对象而无需加载整个对象

`Language`类中添加带有ID参数的构造函数
```java
public Language(long id) {
  this.id = id;
}

示例用户JSON。

{
  "name": "Tony",
  "language": 1
}

或者也可以使用以下格式:

{
  "name": "Tony",
  "language": {
    "id": 1
  }
}

当你保存User实体时,它会自动将ID为1的语言映射到Language对象上。
当你发送JSON时,你的控制器将接收到对象。Spring JacksonMapper 将把JSON转换为对象。

@RestController
public class UserController {

   @PostMapping(value = "/users")
    public ResponseEntity<Void> createUser(@RequestBody @Valid User userVO) {
    }
}
英文:

You should load language first. If you using UI then you must have some predefined languages so if language is loaded then you can show them in ui.

While sending user you can pass only id and hibernate will check if id exists then map that id without loading whole object from db.

Add constructor with argument id Language class.

public Language(long id) {
  this.id = id;
}

Sample user JSON.

{
  &quot;name&quot;: &quot;Tony&quot;,
  &quot;language&quot;: 1
}

OR will also work

{
  &quot;name&quot;: &quot;Tony&quot;,
  &quot;language&quot;: {
    &quot;id&quot;: 1
  }
}

When you save User entity then it will automatically map Language with id 1.
When you send JSON your controller will receive object. Spring JacksonMapper will convert json to object.

@RestController
public class UserController {

   @PostMapping(value = &quot;/users&quot;)
    public ResponseEntity&lt;Void&gt; createUser(@RequestBody @Valid User userVO) {
    }
}

huangapple
  • 本文由 发表于 2023年4月10日 20:15:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977037.html
匿名

发表评论

匿名网友

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

确定