Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
- http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
- version="2.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/persistence">
- <persistence-unit name="persistence_unit_juntos" transaction-type="RESOURCE_LOCAL">
- <provider>org.hibernate.ejb.HibernatePersistence</provider>
- <properties>
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
- <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
- <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/juntosHibernate"/>
- <property name="javax.persistence.jdbc.user" value="root"/>
- <property name="javax.persistence.jdbc.password" value="root"/>
- <property name="hibernate.show_sql" value="true"/>
- <property name="hibernate.format_sql" value="true"/>
- <property name="hibernate.hbm2ddl.auto" value="create"/>
- </properties>
- </persistence-unit>
- </persistence>
- @GET
- @Produces("application/json; charset=UTF-8")
- @Path("/lista")
- public List<Pessoa> ListarTudo(){
- List<Pessoa> pessoas = new ArrayList<Pessoa>();
- List<Pessoa> listaEntityPessoas = repository.ListarTudo();
- for (Pessoa entity : listaEntityPessoas) {
- pessoas.add(new Pessoa(entity.getId(), entity.getNome(), entity.getSobrenome(), entity.getEmail(),
- entity.getSenha(), entity.getTelefone(), entity.getCep(), entity.getLogin(),
- entity.getRegistro(), entity.getGacesso_id(), entity.isBanAtivo(),
- entity.getLocalizacaoAtual()));
- }
- return pessoas;
- }
- public class PessoaRepository {
- private final EntityManagerFactory entityManagerFactory;
- private final EntityManager entityManager;
- public PessoaRepository(){
- /*CRIANDO O NOSSO EntityManagerFactory COM AS PORPRIEDADOS DO ARQUIVO persistence.xml */
- this.entityManagerFactory = Persistence.createEntityManagerFactory("persistence_unit_juntos");
- this.entityManager = this.entityManagerFactory.createEntityManager();
- }
- /**
- * CRIA UM NOVO REGISTRO NO BANCO DE DADOS
- * */
- public void Salvar(Pessoa pessoaEntity){
- this.entityManager.getTransaction().begin();
- this.entityManager.persist(pessoaEntity);
- this.entityManager.getTransaction().commit();
- }
- /**
- * ALTERA UM REGISTRO CADASTRADO
- * */
- public void Alterar(Pessoa pessoaEntity){
- this.entityManager.getTransaction().begin();
- this.entityManager.merge(pessoaEntity);
- this.entityManager.getTransaction().commit();
- }
- /**
- * RETORNA TODAS AS PESSOAS CADASTRADAS NO BANCO DE DADOS
- * */
- @SuppressWarnings("unchecked")
- public List<Pessoa> ListarTudo(){
- return this.entityManager.createQuery("SELECT p FROM Pessoa p ORDER BY p.nome").getResultList();
- }
- /**
- * CONSULTA UMA PESSOA CADASTRA PELO CÓDIGO
- * */
- public Pessoa GetPessoa(Integer id){
- return this.entityManager.find(Pessoa.class, id);
- }
- /**
- * EXCLUINDO UM REGISTRO PELO CÓDIGO
- **/
- public void Excluir(Integer id){
- Pessoa pessoa = this.GetPessoa(id);
- this.entityManager.getTransaction().begin();
- this.entityManager.remove(pessoa);
- this.entityManager.getTransaction().commit();
- }
- public Pessoa Login(String email){
- return (Pessoa) this.entityManager
- .createQuery("SELECT p FROM Pessoa p WHERE p.email = :pemail")
- .setParameter("pemail", email).getSingleResult();
- }
- public boolean HabilitarConta(Pessoa pessoa){
- return false;
- }
- }
- @Entity
- @Table(name="pessoa")
- public class Pessoa {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name="id")
- private int id;
- @Column(name="nome")
- private String nome;
- @Column(name="sobrenome")
- private String sobrenome;
- @Column(name="email")
- private String email;
- @Column(name="senha")
- private String senha;
- @Column(name="telefone")
- private String telefone;
- @Column(name="cep")
- private Cep cep;
- @Column(name="login")
- private Timestamp login;
- @Column(name="registro")
- private Timestamp registro;
- @Column(name="gacesso_id")
- private Permissao gacesso_id;
- @Column(name="banAtivo")
- private boolean banAtivo;
- @Column(name="localizacaoAtual")
- private Localizacao localizacaoAtual;
- public Pessoa() {
- }
- public Pessoa(int id, String nome, String sobrenome, String email, String senha,
- String telefone, Cep cep, Timestamp login, Timestamp registro,
- Permissao gacesso_id, boolean banAtivo, Localizacao localizacaoAtual) {
- super();
- this.id = id;
- this.nome = nome;
- this.sobrenome = sobrenome;
- this.email = email;
- this.senha = senha;
- this.telefone = telefone;
- this.cep = cep;
- this.login = login;
- this.registro = registro;
- this.gacesso_id = gacesso_id;
- this.banAtivo = banAtivo;
- this.localizacaoAtual = localizacaoAtual;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public String getSobrenome() {
- return sobrenome;
- }
- public void setSobrenome(String sobrenome) {
- this.sobrenome = sobrenome;
- }
- public String getSenha() {
- return senha;
- }
- public void setSenha(String senha) {
- this.senha = senha;
- }
- public String getTelefone() {
- return telefone;
- }
- public void setTelefone(String telefone) {
- this.telefone = telefone;
- }
- public Cep getCep() {
- return cep;
- }
- public void setCep(Cep cep) {
- this.cep = cep;
- }
- public Timestamp getLogin() {
- return login;
- }
- public void setLogin(Timestamp login) {
- this.login = login;
- }
- public Timestamp getRegistro() {
- return registro;
- }
- public void setRegistro(Timestamp registro) {
- this.registro = registro;
- }
- public Permissao getGacesso_id() {
- return gacesso_id;
- }
- public void setGacesso_id(Permissao gacesso_id) {
- this.gacesso_id = gacesso_id;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public Localizacao getLocalizacaoAtual() {
- return localizacaoAtual;
- }
- public void setLocalizacaoAtual(Localizacao localizacaoAtual) {
- this.localizacaoAtual = localizacaoAtual;
- }
- public boolean isBanAtivo() {
- return banAtivo;
- }
- public void setBanAtivo(boolean banAtivo) {
- this.banAtivo = banAtivo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement