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

pes8fvy9  于 2021-06-15  发布在  Mysql
关注(0)|答案(3)|浏览(299)

我有两个实体表,叫做预约和服务。一次预约可以提供多种服务。服务已存储在数据库中。我们可以新增约会。添加新预约时,我们从下拉菜单中选择了“添加的服务”。可以通过一次预约添加多个服务。我需要在约会表中存储新记录,并在另一个联接表中存储相关的约会id和服务id。这里附上我的问题图片。

我已经试过很多方法了。吼叫是一种尝试。
这是预约课

@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

}

这是美容服务班

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

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

    //getter and setter
}

这是预约控制器类代码,

@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
        }
        ]

}

为什么不在联接表中记录?只有预约表。

5jdjgkvh

5jdjgkvh1#

你可以用多种方法来做。其中一个被指定为@aritra paul,实际上是 Bidirectional 代表 OneToMany Map。我想你想用 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    |       |
+----------------+------------+------+-----+---------+-------+

希望这有帮助!!

j2qf4p5b

j2qf4p5b2#

您绝对不应该像您提到的那样创建联接表实体,因为它更多地是底层数据库表示,而不是面向对象的表示。
可以通过定义以下内容来实现联接表:

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

@OneToMany(mappedBy="appointment")
@JoinTable(name="Join_Table")
Set <ServiceT> service;

根据您的表使用关系Mapmanytone或onetomany。

@Entity
@Table(name="service", schema="ADMIN")
public class ServiceT implements Serializable {
//...

@ManyToOne
Appointment appointment;

如果要明确设置列名,可以使用
@连接柱
注解。

vh0rcniy

vh0rcniy3#

你能检查一下 beautyServices 实际上是从@requestbody正确绑定的。
属性名为 beautyServices 在json中,它说 "service" . 在json中,应该说 "beautyServices" 而不是 "service" .
另外,检查beautyservices表上是否已经预先存在ids 1、2、3。如果没有,如果您希望通过保存约会来插入这些内容,则需要添加 CASCADETYPE.PERSIST .

相关问题