Saving Dynamic Data in the Same Java Entity

0

I'm using Spring MVC, thymeleaf and for me with all built routes it did automatic but not working, entity, repository, controller and etc.

I have a relationship of N = N of course to shift, from that relationship created the course table to the shift shift fields and entered directly into the database and is already listing in the front end . The problem is in the courseTurno table and has only the course id and shift has an Attribute Number of Vacancies "Vacancies" and the values are registered through the course form. and an attribute within an object within another object. The field of the course object is saving beauty in the course table but the fields of the other table besides being multiple values can be saved not saving in the courseTurno table.

entity and the form in the front-end to the null validation field are of the same value and I am solving this error as I search through course. .quantidadeVagas the thymeleaf 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;

The getters and setters are set to CourseTurn:

@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 pretty dynamic so I can add or remove multiples and the counter is validating too.

    
asked by anonymous 21.02.2017 / 12:39

0 answers