【问题标题】:Multiple select in thymeleaf + hibernate + spring bootthymeleaf + hibernate + spring boot 中的多项选择
【发布时间】:2017-04-27 16:42:42
【问题描述】:

大家好,我正在使用带有 spring boot 和 spring data jpa 的 thymeleaf 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

有什么建议吗?我现在有点迷路了。

地籍.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 + "]";
    }


}

【问题讨论】:

  • 你能把pct_id的代码贴出来吗?我假设这是某个类的属性。这可能有多种原因,但我正在考虑的两个原因是 1)错误地持久化 bean 以使其无法生成正确的 ID 或 2)不正确的 getter/setter
  • 确定...我会编辑原始帖子。

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


【解决方案1】:

问题解决了。似乎如果您更改休眠映射,则必须重新创建数据库。我这样做了,问题就解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2018-04-11
    • 2018-02-04
    • 2017-04-26
    相关资源
    最近更新 更多