Hibernate卡在一个数据条目上

时间:2018-09-13 21:07:46

标签: java mysql spring hibernate spring-boot

我正在构建一个医院管理应用程序(使用spring boot,hibernate和mySql),当尝试为患者添加约会时,hibernate只创建一个条目,然后它会不断更新它,而不是添加新的。 这是我的Visits实体:

@Entity
@Table(name = "visits", uniqueConstraints = {@UniqueConstraint(columnNames = {"date", "doctor_id"})})
public class DoctorVisit {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Enumerated(EnumType.STRING)
    private Cause cause;

    @DateTimeFormat(pattern="yyyy-MM-dd HH:mm")
    private LocalDateTime date = LocalDateTime.now();

    @Enumerated(EnumType.STRING)
    private Status status;
    private String diagnosis;
    private String prescription;
    private  String referalls;
    @ManyToOne
    private Patient patient;
    @ManyToOne
    private Doctor doctor;

这里是控制器,第一个接收者请求针对患者和医生专业的参数,并创建一个新的DoctorVisit实例:

@GetMapping("/add")
    public String add(@RequestParam String spec,@RequestParam Long id, Model model){
        List<Doctor> doctors =  doctorRepository.getAllBySpeciality(spec);
        Patient patient = patientRepositoryt.findOne(id);

        model.addAttribute("visit", new DoctorVisit());
        model.addAttribute("doctors", doctors);
        model.addAttribute("spec", spec);
        model.addAttribute("patient", patient);
        return "visitForm";
    }

它将属性传递给此jsp绑定形式:

<form:form modelAttribute="visit" method="POST" >
    <form:input path="patient.id" value="${patient.id}" readonly="true"/>
    ${spec}:<form:select path="doctor" multiple="false">
    <c:forEach var="doctor" items="${doctors}">
        <form:option value="${doctor}" label="${doctor.surname}"/>
    </c:forEach>
</form:select>

    Date and time:<form:input id="timePicker" path="date"/>
    Cause: <form:select path="cause" multiple="false">
    <c:forEach var="cause" items="${causes}">
        <form:option value="${cause}" label="${cause.description}"/>
    </c:forEach>
</form:select>
    <input type="submit" value="Submit"/>
</form:form>

然后将产品发送到此控制器:

 @PostMapping("/add")
    @ResponseBody
    public String save(@Valid  DoctorVisit visit){
      visitRepository.save(visit);
      return "success";


    }

为什么不创建新的数据库条目?它似乎停留在第一个条目上,并且无法创建新条目,只能更新最后一个条目。

0 个答案:

没有答案