gpt4 book ai didi

mysql - 如何在 Java 中使用 hibernate 在 MySQL 数据库中创建连接表?

转载 作者:行者123 更新时间:2023-11-29 05:51:18 25 4
gpt4 key购买 nike

我有两个名为预约和服务的实体表。一次预约可以提供多项服务。服务已经存储在数据库中。能新追加预约。添加新约会时,我们从下拉菜单中选择添加的服务。一次预约可以添加多项服务。我需要在约会表上存储新记录,并将相关的约会 ID 和服务 ID 存储在另一个连接表上。这里附上我的问题的图片。

enter image description here

我已经尝试了很多方法来做到这一点。下面是一种尝试。

这是预约类

@Entity
@Table(name="appointment")
public class Appointment extends AbstractPersistable<Long> implements Serializable {

@OneToMany
@JoinTable(name = "service_x_appointment", joinColumns = @JoinColumn(name = "appointment_id"),
inverseJoinColumns = @JoinColumn(name = "beautyservice_id"))
Set<BeautyService> beautyServices;

private String type;
private Date date;
private String time;
private String description;
private int approval;

@ManyToOne
@JoinColumn(name = "userid")
private User user;

//getter and setter

}

这是 BeautyService 类

@Entity
@Table(name="beauty_service")
public class BeautyService extends AbstractPersistable<Long> {


private String serviceName;
private String timeDuration;
private String amount;

//getter and setter
}

这是约会 Controller 类代码,

@RequestMapping(value="/createAppointment",method = RequestMethod.POST)
public String createAppointment(@RequestBody Appointment appointment){

String response = null;
response = appointmentService.save(appointment);
return response;
}

这里是预约服务类代码

public String save(Appointment appointment) {
appoinmentRepository.save(appointment);
return "Appointment added successfully";
}

这是我的请求正文。

{   
"type":"Type02",
"date":null,
"time":"20:56",
"description":"Hellow World",
"approval":0,
"user":{
"id":2,
"name" : "Alex",
"telephone" : "0774466886",
"age":21,
"email": null
},

"beautyServices" : [

{
"id":1,
"serviceName":"hair strate",
"timeDuration" : "02 Hours",
"amount" : 5000
},
{
"id":2,
"serviceName":"Eye brows",
"timeDuration" : "02 Hours",
"amount" : 5000
},
{

"id":3,
"serviceName":"Near cutting",
"timeDuration" : "02 Hours",
"amount" : 5000
}
]

}

为什么不记录在连接表中呢?只有约会表。

最佳答案

您可以通过多种方式进行。一个已经指定@Aritra Paul,它实际上是OneToMany 映射的Bidirectional 表示。我认为您想使用 UniDirectional 表示。在这种情况下,您不必使用 mappedBy

只需像下面这样创建您的实体:

@Entity
@Table(name="appointment", schema="ADMIN")
public class Appointment implements Serializable {


@OneToMany
@JoinColumn(name = "appointment_id")
@JoinTable(name = "service_appointment", joinColumns = @JoinColumn(name = "appointment_id"),
inverseJoinColumns = @JoinColumn(name = "service_id"))
Set<Service> services;
}


@Entity
public class Service {
// Some Properties. No need to specify reference of Appointment here.

}

如果你这样定义你的实体,你将有一个这样的连接表

+----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------+------+-----+---------+-------+
| appointment_id | bigint(20) | NO | MUL | NULL | |
| service_id | bigint(20) | NO | PRI | NULL | |
+----------------+------------+------+-----+---------+-------+

希望这对您有所帮助!

关于mysql - 如何在 Java 中使用 hibernate 在 MySQL 数据库中创建连接表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53901604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com