Manytomany mapping. Bean problem

Asked

Viewed 164 times

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?

  • @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.

  • @Sérgiomucciaccia In my xhtml I’m calling the lawyer this way: value="#{cadastroPrazoBean.advogado.nome}"

1 answer

1

Within your term object you have a list of attorneys right? To save a deadline along with references to your attorneys just take these attorneys from the database with find() and add them to the list of attorneys of your term object and then save the deadline! When saving the deadline your JPA provider will look at this list and add the appropriate rows in the prazo_lawyer table automatically.

Then just add the lawyers to the list, which can be done with a button on the side of the autocomplete for example:

public void buttonAction(ActionEvent actionEvent) {
    prazo.getAdvogados().add(advogado);
}

This lawyer would be selected in the autocomplete, but for that your p:autocomplete would have to select an entire advocate object and not just the name, so you would have to make a convert. Look at the example Pojo (the autocomplete written Pojo on the left) in the first faces showcase:

http://www.primefaces.org/showcase/ui/input/autoComplete.xhtml

Summarizing in your class Cadastroprazobean must have a lawyer object and a term object. When the client completes the autocomplete the lawyer object will be updated to the chosen and when the button is pressed this lawyer will be included in the list of lawyers of the term object, hence when everything is ready just save the term object.

  • Thanks so far for the help, already helped me a lot. I forgot to mention that I already had a convert. Is giving the following msg: model.Advogado cannot be cast to java.lang.String I’m posting my lawyer converts. Thanks in advance

Browser other questions tagged

You are not signed in. Login or sign up in order to post.