Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有复合Id的基本CRUD操作(spring和hibernate)_Spring_Hibernate - Fatal编程技术网

具有复合Id的基本CRUD操作(spring和hibernate)

具有复合Id的基本CRUD操作(spring和hibernate),spring,hibernate,Spring,Hibernate,我试图用hibernate和spring进行一个基本的创建操作,但我一直得到一个消息,id为空,而不是空。所以我认为这可能是因为实体使用了复合id,有趣的是,至少对我来说,删除实体没有任何问题 我使用的方法 @RequestMapping(value="addPatientFamilyRelative",method = RequestMethod.POST) public @ResponseBody String addPatientFamilyRelative(@RequestPa

我试图用hibernate和spring进行一个基本的创建操作,但我一直得到一个消息,id为空,而不是空。所以我认为这可能是因为实体使用了复合id,有趣的是,至少对我来说,删除实体没有任何问题

我使用的方法

 @RequestMapping(value="addPatientFamilyRelative",method = RequestMethod.POST)
    public @ResponseBody String addPatientFamilyRelative(@RequestParam(value="idPatient")int idPatient,
                                                             @RequestParam(value="idRelative")int idRelative,
                                                             @RequestParam(value="idRelationship")int idRelationship)
    {
        Patient_Relative patientRelative = new Patient_Relative();
                patientRelative.setIdRelationship(relationshipService.getById(idRelationship));
                patientRelative.setPatient(patientService.getById(idPatient));
                patientRelative.setRelative(relativeService.getRelative(idRelative));


                prService.create(patientRelative);

        return "$('#tblPatientFamilyPatientRelatives').ajax.reload();$('#tblPatientRelativesList').ajax.reload()";
    } 
患者\u相关类别

@Entity
@Table(name="Patient_Relative")
public class Patient_Relative implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -2670460334767266076L;
    @EmbeddedId
    @JoinColumn(name = "idRelative", referencedColumnName = "idRelative", insertable = false, updatable = false)
    @ManyToOne(optional = false)
        @JsonIgnore
    private Relative relative;
    @JoinColumn(name = "idRelationship", referencedColumnName = "idRelationship")
    @ManyToOne
    private Relationship idRelationship;
    @JoinColumn(name = "idPatient", referencedColumnName = "idPatient", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    @JsonIgnore
    private Patient patient;

    public Relative getRelative() {
        return relative;
    }
    public void setRelative(Relative relative) {
        this.relative = relative;
    }

    public Relationship getIdRelationship() {
        return idRelationship;
    }

    public void setIdRelationship(Relationship idRelationship) {
        this.idRelationship = idRelationship;
    }

    public Patient getPatient() {
        return patient;
    }
    public void setPatient(Patient patient) {
        this.patient = patient;
    }   
}
耐心相对

@Embeddable
public class PatientRelativeId implements java.io.Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 2719758608242901070L;

    @Column(name = "idPatient")
    private int patientId;

    @Column(name = "idRelative")
    private int relativeId;


    public PatientRelativeId() {
    }

    public PatientRelativeId(int patientId, int relativeId) {
        this.patientId = patientId;
        this.relativeId = relativeId;
    }

    public int getPatientId() {
        return patientId;
    }
    public void setPatientId(int patientId) {
        this.patientId = patientId;
    }
    public int getRelativeId() {
        return relativeId;
    }
    public void setRelativeId(int relativeId) {
        this.relativeId = relativeId;
    }
}
我希望这足以得到一些想法,我本来想添加一列只是为id,但我想我不能再这样做了。 提前谢谢

我希望这对某人有所帮助。 首先,我的
Patient\u Relative
类是短1变量,用于存储复合id,因此我添加了变量
PatientRelativeId compositeId

其次,在控制器方法中,我所要做的就是设置复合id、患者和亲属的值,然后调用服务来创建对象

@RequestMapping(value="addPatientFamilyRelative",method = RequestMethod.POST)
    public @ResponseBody String addPatientFamilyRelative(@RequestParam(value="idPatient")int idPatient,
                                                             @RequestParam(value="idRelative")int idRelative,
                                                             @RequestParam(value="idRelationship")int idRelationship)
    {
        Patient_Relative patientRelative = new Patient_Relative();
                PatientRelativeId id = new PatientRelativeId(idPatient, idRelative);
                patientRelative.setPatienRelativeId(id);
                patientRelative.setIdRelationship(relationshipService.getById(idRelationship));
                patientRelative.setPatient(patientService.getById(idPatient));
                patientRelative.setRelative(relativeService.getRelative(idRelative));


                prService.create(patientRelative);

        return "addRelative";
    }

您需要为idRelationship、isRelative和IDPatient编写init binder如果使用
@IdClass
策略而不是
@EmbeddedId