java – ManyToMany mapping. bean problem

Question:

After doing the ManyToMany relationship between tables I don't know how to save to my bean. I have a deadline registry class that has one or more lawyers taking care of that deadline. The lawyer may also be responsible for more than one term. I want to make sure that when I enter this lawyer's record, it shows the deadlines that the lawyer is responsible for, as well as the deadline. In the future these lawyers will be alerted by email of the deadline date.

I did the following mapping in my prazo class:

@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "prazo_advogado", joinColumns = @JoinColumn(name = "prazo_id"),
        inverseJoinColumns = @JoinColumn(name = "advogado_id"))
private List<Advogado> advogados;

advogado class:

@ManyToMany(mappedBy = "advogados")
private List<Prazo> prazos;

In my deadline registration xhtml I have the lawyer field. I don't know how to refer to the lawyer (best doubt):

 <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;
}

}

dao class:

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 form I'm developing is the best form, that's my big question. I've read some JPA mapping books but they don't have 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 "";
}

}

Answer:

Within your term object have a list of lawyers right? To save a deadline along with references for your lawyers just get those lawyers from the database with find() and add them to your deadline object's lawyers list and then save the deadline! When saving the deadline, your JPA provider will look at this list and add the appropriate lines in the lawyer_term table automatically.

So just add the lawyers to the list, which can be done with a button next to the autocomplete, for example:

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

That lawyer would be selected in autocomplete, but for that your p:autoComplete would have to select an entire lawyer object and not just the name, so you would have to do a convert. Look at the Pojo example (the autocomplete spelled Pojo on the left) in the primefaces showcase:

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

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

Scroll to Top
AllEscort