Olá, Tenho um modelo Level que tem um relacionamento ManyToMany para Modules, o relacionamento segue abaixo nos modelos e o Hibernate criou corretamente a tabela de relacionamento module_level:
Model Level
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.MERGE},
mappedBy = "levels",
targetEntity = Module.class
)
private Collection <Module> modules;
//Gets e Sets
Model Module
@ManyToMany(
targetEntity=Level.class,
cascade={CascadeType.PERSIST, CascadeType.MERGE}
)
@JoinTable(
name="module_level",
joinColumns=@JoinColumn(name="module_id"),
inverseJoinColumns=@JoinColumn(name="level_id")
)
private Collection <Level> levels;
//Gets e Sets
Controller Level
@Put("/levels")
public void update(Level level) {
validator.validate(level);
validator.onErrorUsePageOf(this).edit(level);
repository.update(level);
result.redirectTo(this).index();
}
Formulário Level
<form action="${pageContext.request.contextPath}/levels" method="post">
<c:if test="${not empty level.id}">
<input type="hidden" name="level.id" value="${level.id}"/>
<input type="hidden" name="_method" value="put"/>
</c:if>
<div class="field">
Description:<br />
<input type="text" name="level.description" value="${level.description}"/>
</div>
<div class="field">
<c:forEach items="${modules}" var="module">
<input type="checkbox" name="level.modules" value="${module.id}"> ${module.name}<br>
</c:forEach>
</div>
<div class="actions">
<button type="submit">send</button>
</div>
</form>
A dúvida é, como devo fazer para conseguir persistir as informações dos módulos que estão relacionados aos níveis, alguém tem um exemplo completo?
Agradeço