Saving dynamic data in the same Java identity

Asked

Viewed 175 times

0

I’m using Spring MVC, Thymeleaf and for me with all routes built he did automatic but not this working, Entity, Repository, controller and etc.

I have a relation of N=N of course to Turn, of that relation created the table cursoTurno the fields pain turn and inserted directly in the database and already this listing on front-end. The problem is in the table courseTurno and has only the course and shift id has an Attribute Number of Vacancies "Quantityevagas" and the values are registered through the course form. and an attribute within an object within another object. The course object fields are saving beauty in the course table but the fields of the other table besides being multiple ditches can be saved n this saving in the course table.

Codes of Entity and the form in front-end a the null validation field are of the same value and I am solving this error as I search through course.curso.quantityThymeleaf is not accepting 2 objects.

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

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "curso_id_seq")
@SequenceGenerator(name = "curso_id_seq", sequenceName = "curso_id_seq", allocationSize = 1)
@Column(name = "id")
private long id;

@NotNull
@Enumerated(EnumType.ORDINAL)
@Column(name = "tipo_curso")
private TipoCursoEnum tipoCurso;

@NotEmpty
@Size(max = 50)
@Column(name = "nome")
private String nome;

@Enumerated(EnumType.ORDINAL)
@Column(name = "situacao")
private SituacaoEnum situacao = SituacaoEnum.ATIVO;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "curso")
private List<CursoTurno> cursoTurno;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "unidade_id", referencedColumnName = "id")
private Unidade unidade;

Getters and setters are set to Cursoturno:

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

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "curso_turno_id_seq")
@SequenceGenerator(name = "curso_turno_id_seq", sequenceName = "curso_turno_id_seq", allocationSize = 1)
@Column(name = "id")
private long id;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "curso_id", referencedColumnName = "id")
private Curso curso;

@NotNull
@Column(name = "quantidade_vagas")
private int quantidadeVagas;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "turno_id", referencedColumnName = "id")
private Turno turno;

Javascript form to add and remove is working:

<div class="row" >
    <!-- INICIO - Adiciona quantidade vagas -->
    <div id="curso-vagas-container">
        <div class="curso-vagas-item">
            <div class="col-sm-5">
                <div class="form-group" th:classappend="${#fields.hasErrors('cursoTurno')} ? has-error">
                    <label class="control-label required" for="quantidadeVagas" th:text="#{quantidadeVagas}">Quantidade de Vagas</label>
                    <input type="text" class="form-control" id="quantidadeVagas" th:field="*{cursoTurno}" maxlength="50" />
                    <label class="error" th:if="${#fields.hasErrors('cursoTurno')}" th:errors="*{cursoTurno}"></label>
                </div>
            </div>

        <div class="col-sm-5">
            <div class="form-group" th:classappend="${#fields.hasErrors('cursoTurno')} ? has-error">
                <label class="control-label required" for="turno" th:text="#{turno}">Turno</label>
                <select  class="form-control populate" id="turno" th:field="*{cursoTurno}" data-plugin-selectTwo="">
                    <option selected="selected" value=""></option>
                    <option th:each="turno : ${listaTurnos}" th:value="${turno.id}" th:text="${turno.nome}"></option>
                </select>

                <label class="error" th:if="${#fields.hasErrors('cursoTurno')}" th:errors="*{cursoTurno}"></label>
            </div>
        </div>

        <div class="col-sm-2">
            <div class="remove-vaga-container"></div>
        </div>
    </div>
</div>

<div class="col-sm-2">
    <a class="btn btn-primary padding-buttons" role="button" id="btnAddCurso">
        <i class="fa fa-plus" style="color: white;"></i>
    </a>
</div>

It’s very dynamic I can add or remove multiple and the counter is validating as well.

inserir a descrição da imagem aqui

  • It is difficult to understand what the question is. I suggest to clarify what the question is, besides adjusting accents and spelling.

  • spelling was by laziness msm, summarize everything and what my doubt is to save in the bank in the related table in the case Onetomany course with coursesTurno

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.