Boa tarde!
Estou desenvolvendo um projeto no hibernate. E fiz o relacionamento. Entre Sistemas tem muitos Módulos.
@Entity
public class Sistemas {
@Id
@GeneratedValue
private long id;
private String nome;
private String ativo;
private String url;
private String imagem;
@OneToMany(mappedBy = "sistemas",targetEntity= Modulos.class,
fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Modulos>modulos;
//Métodos get e set
@Entity
public class Modulos {
@Id
@GeneratedValue
private long id;
private String nome;
private String ativo;
private String url;
private String imagem;
//Relacionamento
@ManyToOne
@JoinColumn(name="sistemas_id")
private Sistemas sistemas;
//Metodos set e get
Confiri no banco está ok!
Adiciono o sistema assim. Utilizando uma página jsp.
<form id="sistemasForm" action="<c:url value="/sistemas"/>"
method="POST">
<fieldset>
<legend>Adicionar produtos</legend>
<label for="nome">Nome:</label>
<input id="nome" type="text" name="sistemas.nome"
value="${sistemas.nome }"/>
<label for="ativo">Ativo:</label>
<textarea id="ativo"
name="sistemas.ativo">
${sistemas.ativo }
</textarea>
<label for="url">Url:</label>
<input id="url" type="text"
name="sistemas.url" value="${sistemas.url }"/>
<label for="url">Imagem:</label>
<input id="imagem" type="text"
name="sistemas.imagem" value="${sistemas.imagem }"/>
<button type="submit">Enviar</button>
</fieldset>
</form>
Minha dúvida, e como eu coloco o nome do ID sistema no módulo.
Olha como está.
<form id="modulosForm" action="<c:url value="/modulos"/>"
method="POST">
<fieldset>
<legend>AdicionarModulos</legend>
//Como pegar o id do sistemas
<label for="sistemas">IdSistema:</label>
<input id="sistemas" type="text" name="sistemas"
value="${modulos.sistemas }"/>
//Continuação do form
<label for="nome">Nome:</label>
<input id="nome" type="text" name="modulos.nome"
value="${modulos.nome }"/>
<label for="ativo">Ativo:</label>
<textarea id="ativo"
name="modulos.ativo">
${modulos.ativo }
</textarea>
<label for="url">Url:</label>
<input id="url" type="text"
name="modulos.url" value="${modulos.url }"/>
<label for="url">Imagem:</label>
<input id="imagem" type="text"
name="modulos.imagem" value="${modulos.imagem }"/>
<button type="submit">Enviar</button>
</fieldset>
</form>
Está funcionando, mas o campo idsistemas esta indo NULL.
Como eu referencio esse campo idsistemas no meu form.
Valeu Galera!