复合Id(spring + hibernate)的基本CRUD操作

时间:2014-05-19 08:32:34

标签: spring hibernate

我试图使用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()";
    } 

Patient_Relative class

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

PatientRelativeId

@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添加一个列,但我想我不能再这样做了。 提前致谢

1 个答案:

答案 0 :(得分:0)

我希望这有助于某人。 首先,我的Patient_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";
    }