Olá,gostaria de agradecer a todos pelas respostas!!! consegui resolver o problema,segue a minha solução pra quem precise:
1- defini no sgbd a constraint unique para o cpf/cnpj
2-colooquei essa constaint no meu mapeamento
@Column(name = "cpf_cnpj",unique=true)
private String cpf;
3- como dito em uma das respostas deixei para a camada de negocio lançar a exception em caso de duplicidade,eu pego o cnpj antigo e comparo com o novo, se forem diferentes eu mando buscar no banco para saber se já existe
public void update(Cliente cliente) throws BusinessException,InfraException {
Cliente clienteToCompare = clienteDAO.find(cliente.getId());
long ocorrenciasCPFCNPJ = clienteDAO.checkUniqueCpfCnpj(cliente.getCpf());
long ocorrenciasRG = clienteDAO.checkUniqueRg(cliente.getRg());
if(!cliente.getCpf().equals(clienteToCompare.getCpf())){
if(ocorrenciasCPFCNPJ !=0){
throw new BusinessException("O CPF CNPJ já está cadastrado");
}
}
if(!cliente.getRg().equals(clienteToCompare.getRg())){
if(ocorrenciasRG !=0){
throw new BusinessException("RG já cadastrado");
}
}
clienteDAO.update(cliente);
}
4- por fim acrescentei um validator para saber se o formato do cpf/cnpj(no caso cpf) é válido
<p:inputText value="#{clienteBean.cliente.cpf}" required="true"
requiredMessage="Campo CPF Obrigatorio" >
<f:validator
validatorId="validators.CpfValidator" /></p:inputText>
@FacesValidator("validators.CpfValidator")
public class CpfValidator implements Validator {
private static final int[] pesoCPF = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
private static int calcularDigito(String str, int[] peso) {
int soma = 0;
for (int indice=str.length()-1, digito; indice >= 0; indice-- ) {
digito = Integer.parseInt(str.substring(indice,indice+1));
soma += digito*peso[peso.length-str.length()+indice];
}
soma = 11 - soma % 11;
return soma > 9 ? 0 : soma;
}
@Override
public void validate(FacesContext arg0, UIComponent arg1, Object arg2)throws ValidatorException {
String cpf = arg2.toString();
if ((cpf==null) || (cpf.length()!=11)){
FacesMessage msg = new FacesMessage("O CPF deve ser formado por 11 dígitos.", "O CPF deve ser formado por 11 dígitos.");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}else{
Integer digito1 = calcularDigito(cpf.substring(0,9), pesoCPF);
Integer digito2 = calcularDigito(cpf.substring(0,9) + digito1, pesoCPF);
boolean isValido = cpf.equals(cpf.substring(0,9) + digito1.toString() + digito2.toString());
if(!isValido){
FacesMessage msg = new FacesMessage("O CPF informado é inválido.", "O CPF informado é inválido.");
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}
}
Vlw pela força galera!