-1
When I click on Include student appears this error but I do not understand why, someone to give me a light, thank you.
Console error
java.lang.Illegalargumentexception: The Given id must not be null! at org.springframework.util.Assert.notNull(Assert.java:198) ~[spring-core-5.2.5.RELEASE. jar:5.2.5.RELEASE] at org.springframework.data.jpa.Repository.support.Simplejparepository.findById(Simplejparepository.java:269) ~[spring-data-jpa-2.2.6.RELEASE. jar:2.2.6.RELEASE] at sun.reflect.Nativemethodaccessorimpl.invoke0(Native Method) ~[na:1.8.0_231] at sun.reflect.Nativemethodaccessorimpl.invoke(Unknown Source) ~[na:1.8.0_231] at sun.reflect.Delegatingmethodaccessorimpl.invoke(Unknown Source) ~[na:1.8.0_231] at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_231]
Entity Aluno
@Entity
public class Aluno {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long codigo;
@NotNull(message = "*O campo Nome é de preenchimento obrigatório")
private String nome;
private String apelido;
@ManyToMany
@JoinTable(
name="AlunosTurmas",
uniqueConstraints = @UniqueConstraint(columnNames = { "codigo_turma", "codigo_aluno" }),
joinColumns = @JoinColumn(name = "codigo_aluno"),
inverseJoinColumns = @JoinColumn(name = "codigo_turma")
)
private List<Turma> turmas;
*....Getters and Setters*
Entity Turma
@Entity
public class Turma {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long codigo;
@NotEmpty(message = "*O campo Nome é de preenchimento obrigatório")
@NotBlank(message = "*O campo Nome é de preenchimento obrigatório")
private String nome;
private String hora_inicio;
private String hora_fim;
private int vagas;
private String professor;
@ManyToMany
@JoinTable(
name="AlunosTurmas", //nome da nova tabela
uniqueConstraints = @UniqueConstraint(columnNames = { "codigo_turma", "id_aluno" }), //id curso e turma são únicos
joinColumns = @JoinColumn(name = "codigo_turma" ), //chave estrangeira de turma
inverseJoinColumns = @JoinColumn(name = "id_aluno") //chave estrangeira de aluno
)
private List<Aluno> alunos;
Controler Turmas
@Controller
@RequestMapping("/turmas")
public class TurmaController {
@PostMapping("/associarAlunoTurma")
public String associarAluno(@ModelAttribute Aluno aluno, @RequestParam Long codigoTurma) {
Turma turma = turmasRepository.getOne(codigoTurma);
aluno = alunosRepository.getOne(aluno.getCodigo());
turma.getAlunos().add(aluno);
turmaService.salvarAlunoTurma(turma);
return "redirect:/turmas/detalhesTurma/" + codigoTurma;
}
@GetMapping("/detalhesTurma/{codigo}")//aqui passa o próprio cód da turma na requisição
public ModelAndView getAlunoDetalhes(@PathVariable(name = "codigo") Long codigo) {
Turma turma = turmasRepository.getOne(codigo);
ModelAndView mv = new ModelAndView("/turma/detalhesTurma");
mv.addObject("turma", turma);
List <Aluno> alunosNaoAssociados = alunosRepository.findAll();
alunosNaoAssociados.removeAll(turma.getAlunos());
mv.addObject("alunos", alunosNaoAssociados);
return mv;
}
highlightsTurma.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{/LayoutPadrao}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Agrestina</title>
</head>
<body >
<section layout:fragment="content">
<!-- partial -->
<div class="content-wrapper" style="margin-top: -70px;">
<div class="page-header">
<h3 class="page-title"> Cadastro de Turmas </h3>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="/turmas">Listar</a></li>
<li class="breadcrumb-item active" aria-current="page">Cadastrar Turma</li>
</ol>
</nav>
</div>
<form class="forms-sample" th:action="@{/turmas/associarAlunoTurma(codigoTurma=${turma.codigo})}" method="post">
<div class="col-12 grid-margin stretch-card">
<div class="card">
<div class="card-body">
<div class="row">
<div class="form-group col-md-10">
<label for="exampleSelectGender">Selecione o aluno para incluir na turma</label>
<select class="form-control" id="exampleSelectGender" th:field="*{alunos}">
<option value="" selected="selected">- Selecione -</option>
<option th:each="aluno : ${alunos}" th:value="${aluno.codigo}"
th:text="${aluno.nome}">Female</option>
</select>
</div>
<input type="submit" class="btn btn-primary"/>
<!-- <i class="mdi mdi-content-save btn-icon-prepend"></i> Incluir </button> -->
</div>
</div>
</div>
</div>
</form>
<!-- TABELA DE ALUNOS DA TURMA -->
<div class="card">
<div class="card-body">
<h4 class="card-title text-success">Turma: #<span th:utext="${turma.codigo}"></span> <span th:utext="${turma.nome}"></span> </h4>
<table class="table table-responsive table-sm">
<thead>
<tr>
<th class="text-dark font-weight-bold"> # </th>
<th class="text-dark font-weight-bold"> Nome </th>
<th class="text-dark font-weight-bold"> Apelido </th>
</tr>
</thead>
<tbody>
<tr th:each="aluno : ${turma.alunos}">
<td th:text="${aluno.codigo}">1</td>
<td th:text="${aluno.nome}"></td>
<td th:text="${aluno.apelido}"></td>
<td class="left">
<!-- <a class="btn btn-danger btn-sm" title="Excluir" -->
<!-- th:href="@{turmas/excluirTurma/{codigo}(codigo=${turma.codigo})}"> -->
<!-- <i class=" mdi mdi-delete"></i> -->
<!-- </a> -->
</td>
</tr>
<tr>
<!-- <td colspan="36" th:if="${#lists.isEmpty(turma.alunos)}">Nenhum aluno foi encontrado na turma</td> -->
</tr>
</tbody>
</table>
</div>
</div>
</div>
</section>
<script type="text/javascript" th:src="@{/js/jquery-3.3.0.min.js}"></script>
<script type="text/javascript" th:src="@{/js/jquery.autocomplete.js}"></script>
</body>
</html>