3
After doing the Manytomany relationship between tables I don’t know how to save on my bean. I have a class schedule that has one or more lawyers taken care of this term. The lawyer can also be responsible for more than one term. I want to make sure that when I enter the register of this lawyer he shows the deadlines that the lawyer is responsible, in the same way on time. In the future these lawyers will be notified by e-mail of the deadline date.
I did the following mapping in my class prazo
:
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "prazo_advogado", joinColumns = @JoinColumn(name = "prazo_id"),
inverseJoinColumns = @JoinColumn(name = "advogado_id"))
private List<Advogado> advogados;
Class advogado
:
@ManyToMany(mappedBy = "advogados")
private List<Prazo> prazos;
In my xhtml
term registration have the field lawyer. I do not know with reference to the lawyer(major doubts):
<p:outputLabel value="Advogado" for="advogado" />
<p:autoComplete id="advogado" size="40" dropdown="true"
value="#{cadastroPrazoBean.advogado.nome}" completeMethod="#{cadastroPrazoBean.completarAdvogado}"
var="advogado" itemLabel="#{advogado.nome}" itemValue="advogado" forceSelection="true" />
My bean:
public class CadastroPrazoBean implements Serializable {
private static final long serialVersionUID = 1L;
private Prazo prazo;
private Advogado advogado;
private List<Prazo> listPrazos;
@Inject
private Clientes clientes;
@Inject
private Prazos prazos;
@Inject
private Advogados advogados;
@Inject
private CadastroPrazoService cadastroPrazoService;
public CadastroPrazoBean() {
prazo = new Prazo();
advogado = new Advogado();
}
public String salvar() {
this.prazo = cadastroPrazoService.salvar(this.prazo);
FacesUtil.addInfoMessage("Prazo salvo com sucesso!");
return "/pesquisa/pesquisaPrazo.xhtml";
}
public List<Cliente> completarCliente(String nome) {
return this.clientes.porNome(nome);
}
public List<Advogado> completarAdvogado(String nome) {
return this.advogados.porNome(nome);
}
public List listagemPrazo(){
listPrazos = prazos.listaPrazo();
return this.listPrazos;
}
public Prazo getPrazo() {
return prazo;
}
public void setPrazo(Prazo prazo) {
this.prazo = prazo;
}
public Advogado getAdvogado() {
return advogado;
}
public void setAdvogado(Advogado advogado) {
this.advogado = advogado;
}
}
Class dao
:
public List<Advogado> porNome(String nome) {
return this.manager.createQuery("from Advogado "
+ "where upper(nome) like :nome", Advogado.class)
.setParameter("nome", nome.toUpperCase() + "%")
.getResultList();
}
I don’t know if the way I’m developing is the best way, that’s my big question. I’ve read some mapping books with JPA but there are no examples with page interaction.
Convert class
@FacesConverter(forClass = Advogado.class)
public class AdvogadoConverter implements Converter {
@Inject
private Advogados advogados;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Advogado retorno = null;
if (StringUtils.isNotBlank(value)) {
retorno = this.advogados.porId(new Long(value));
}
return retorno;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value != null) {
Long codigo = ((Advogado) value).getId();
String retorno = (codigo == null ? null : codigo.toString());
return retorno;
}
return "";
}
}
I don’t understand your question. Shouldn’t the Cadastroprazobean have a deadline instead of a lawyer? The Manytomany mapping seems to be right. The problem is how to save a deadline in the database, that’s it?
– Sérgio Mucciaccia
@That’s right, as saved in the bank. My doubt how I do the interaction in the bean with relationship between tables and as I call in my xhtml. My bean has the term and attorney.
– Sidnei Ferreira
@Sérgiomucciaccia In my xhtml I’m calling the lawyer this way:
value="#{cadastroPrazoBean.advogado.nome}"
– Sidnei Ferreira