多重选择百里香叶+冬眠+春季靴子

时间:2016-12-12 18:31:23

标签: spring-mvc spring-boot spring-data thymeleaf

大家好,我使用带有弹簧靴和弹簧数据jpa的百日咳3。但这是问题所在。当我尝试保存时,我从Hibernate得到了这个错误:

Hibernate: insert into consulta (medico_id) values (?)
Hibernate: insert into consulta_pacientes (consulta_id, pacientes_id) values (?, ?)
2016-12-12 16:06:53.963  WARN 11912 --- [nio-9393-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1364, SQLState: HY000
2016-12-12 16:06:53.963 ERROR 11912 --- [nio-9393-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : Field 'pct_id' doesn't have a default value
2016-12-12 16:06:53.965  INFO 11912 --- [nio-9393-exec-9] o.h.e.j.b.internal.AbstractBatchImpl     : HHH000010: On release of batch it still contained JDBC statements
2016-12-12 16:06:53.976 ERROR 11912 --- [nio-9393-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path 
[] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not execute statement; nested exception is org.hibe
rnate.exception.GenericJDBCException: could not execute statement] with root cause

java.sql.SQLException: Field 'pct_id' doesn't have a default value
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902) ~[mysql-connector-java-5.1.39.jar:5.1.39]
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526) ~[mysql-connector-java-5.1.39.jar:5.1.39]

我已经尝试使用转换器,但没有正确锻炼。试图查看这些帖子* ...但是也没有解决。

* 1 http://forum.thymeleaf.org/th-selected-not-working-on-lt-select-gt-lt-option-gt-td4029201.html

* 2 thymeleaf multiple selected on edit

任何提示?我现在有点迷失。

cadastro.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
... 
    <form action="#" th:action="@{salvar}" th:object="${consulta}" method="POST">
    <div class="form-inline">
    <label for="select-medico-consulta" class="form-group">Medico</label>
        <select th:field="*{medico.id}" id="select-medico-consulta" >
            <div>
            <option th:each="medicoEntry : ${medicos}" 
                    th:value="${medicoEntry.id}"
                        th:text="${medicoEntry.nome}"></option>
            </div>          
        </select>

        <div class="form-group">
         <label id="paciente-label" for="select-paciente" > Paciente</label>
         <select th:field="*{pacientes}" id="select-paciente" size="5" multiple="multiple"  >
            <div>
            <option th:each="pacienteEntry : ${listaPacientes}"
                        th:field="*{pacientes}"
                        th:value="${pacienteEntry.id}"
                        th:text="${pacienteEntry.nome}"></option>
            </div>          
        </select>
        </div>                      
        </div>

   <div class="form-group">
  <label for="comment">Consulta</label>
  <textarea class="form-control" rows="5" id="comment"></textarea>
    </div> 


  <button type="submit" class="btn btn-default">Salvar</button>
</form>
</div>    

...

consultaController.java

package
and imports...

@Controller
@RequestMapping("/medclin")
public class ConsultaController {

    @Autowired
    private ConsultaDao consultadao;

    @Autowired
    private MedicoDao medicoDao;

    @Autowired
    private PacienteDao pacienteDao;



    @RequestMapping("/consulta")
    public ModelAndView Consulta() {

        ModelAndView modelAndView = new ModelAndView("consulta/consulta");

        ArrayList<Medico> medicos = (ArrayList<Medico>) medicoDao.findAll();

        ArrayList<Paciente> pacientes = (ArrayList<Paciente>) pacienteDao.findAll();

        modelAndView.addObject("medicos", medicos);
        modelAndView.addObject("listaPacientes", pacientes);


        modelAndView.addObject("consulta", new Consulta());


        return modelAndView;
    }



    @RequestMapping(value = "/salvar", method = RequestMethod.POST)
    public String salvar(@ModelAttribute Consulta consulta) {


        consultadao.save(consulta);

        return "redirect:medclin/home";
    }

}

consultaDao.java

package br.com.medclin.boot.daos;

import java.io.Serializable;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import br.com.medclin.boot.models.Consulta;

@Repository
public interface ConsultaDao  extends CrudRepository<Consulta , Integer>
{

}

编辑: 按@bphilipnyc的要求

Paciente.java

@Entity
public class Paciente {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String nome;
    private Calendar dataNascimento;
    private String endereco;

    @NotNull
    private String cpf;

    @ManyToOne
    private Plano planoDeSaude;

    public Paciente(String nome, Calendar dataNascimento, String endereco, String cpf, Plano plano) {
        super();
        this.nome = nome;
        this.dataNascimento = dataNascimento;
        this.endereco = endereco;
        this.cpf = cpf;
        this.planoDeSaude = plano;
    }

    public Paciente(String nome, Calendar dataNascimento, String endereco, String cpf) {
        super();
        this.nome = nome;
        this.dataNascimento = dataNascimento;
        this.endereco = endereco;
        this.cpf = cpf;
    }

    public Paciente(String pctCpf) {
        this.cpf = pctCpf;
    }

    public Paciente() {

    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Calendar getDataNascimento() {
        return this.dataNascimento;
    }

    public void setDataNascimento(Calendar dataNascimento) {
        this.dataNascimento = dataNascimento;
    }

    public String getEndereco() {
        return this.endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public String getCpf() {
        return this.cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Plano getPlanoDeSaude() {
        return planoDeSaude;
    }

    public void setPlanoDeSaude(Plano planoDeSaude) {
        this.planoDeSaude = planoDeSaude;
    }

    public boolean equals(Paciente pct) {
        if (this.id == pct.id)
            return true;
        else
            return false;
    }

    @Override
    public int hashCode() {

        return super.hashCode();
    }

    @Override
    public String toString() {
        return "Paciente [id=" + id + ", nome=" + nome + ", dataNascimento=" + dataNascimento + ", endereco=" + endereco
                + ", cpf=" + cpf + ", planoDeSaude=" + planoDeSaude + "]";
    }


}

1 个答案:

答案 0 :(得分:0)

问题解决了。看来,如果更改hibernate映射,则必须重新创建数据库。我做到了,问题解决了。