Created
November 14, 2018 23:17
-
-
Save andrechavesg/fab6bed4fc5586db47083da8336400c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Optional; | |
public class Buscador{ | |
// metodos e atributos | |
public Optional<Pessoa> buscaPorNome(String nome) { | |
try{ | |
Pessoa pessoa = (Pessoa) entityManager. | |
createQuery("SELECT p FROM Pessoa p WHERE nome LIKE:pessoaNome"). | |
setParameter("pessoaNome", nome).getSingleResult(); | |
Optional<Pessoa> pessoaOp = Optional.of(pessoa); | |
return pessoaOp; | |
} | |
catch(NoResultException e){ | |
return Optional.empty(); | |
} | |
} | |
} | |
public class Rastreio { | |
public static void main(String[] args) { | |
Buscador buscador = new Buscador(); | |
Optional<Pessoa> pessoa = buscador.buscaPorNome("Nome não registrado o banco"); | |
System.out.println("cpf: "+pessoa.get().getCpf()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment