如何在Java中使用Hibernate在MySQL数据库中创建Join表?

时间:2018-12-23 06:08:31

标签: mysql hibernate spring-boot

我有两个称为约会和服务的实体表。一个约会可以有很多服务。服务已存储在数据库中。我们可以新添加约会。添加新约会时,我们从下拉菜单中选择了已添加的服务。可以一次约会添加许多服务。我需要在约会表上存储新记录,并在另一个联接表上存储相关的约会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
}

这是约会控制器类代码,

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

}

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

3 个答案:

答案 0 :(得分:1)

您绝对不应该创建Joined表实体,因为它是基础数据库表示形式而不是面向对象的数据库表示形式。

您可以通过定义以下内容来获得联接表:

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

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

根据您的表使用关系映射ManyToOne或OneToMany。

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

@ManyToOne
Appointment appointment;

如果要明确设置列名,可以使用

  

@JoinColumn

注释。

答案 1 :(得分:1)

您可以通过多种方式进行操作。已指定一个@Aritra Paul,它实际上是 <WhatsappShareButton url={shareUrl} title= {`${project.title} ${project.pictureUrl}`} media={project.pictureUrl} className="m-2"> <WhatsappIcon size={32} round={true} /> </WhatsappShareButton> <LinkedinShareButton url={shareUrl} title={project.title} media={project.pictureUrl} className="m-2"> <LinkedinIcon size={32} round={true} /> </LinkedinShareButton> <EmailShareButton url={shareUrl} title={project.title} media={project.pictureUrl} className="Demo__some-network__share-button m-2"> <EmailIcon size={32} round={true} /> </EmailShareButton> </div> </div> </div> ) } } 映射的Bidirectional表示。 我认为您想使用OneToMany表示形式。在这种情况下,您不必使用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.

} 

希望这会有所帮助!

答案 2 :(得分:0)

能否请您检查beautyServices是否确实与@RequestBody绑定。

属性名称为beautyServices,而在Json中则为"service"。 在Json中,应该说"beautyServices"而不是"service"

此外,请检查beautyServices表中是否已经存在ID 1、2、3。 如果不是,则希望通过保存约会也将其插入,则需要添加CASCADETYPE.PERSIST