英文:
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中显示它们。
在发送用户时,你只需传递ID,Hibernate 将检查该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.
{
"name": "Tony",
"language": 1
}
OR will also work
{
"name": "Tony",
"language": {
"id": 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 = "/users")
public ResponseEntity<Void> createUser(@RequestBody @Valid User userVO) {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论