Advertisement
Guest User

Untitled

a guest
Sep 4th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persistence xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
  3. http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
  4. version="2.0"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns="http://java.sun.com/xml/ns/persistence">
  7.  
  8. <persistence-unit name="persistence_unit_juntos" transaction-type="RESOURCE_LOCAL">
  9. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  10. <properties>
  11. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
  12. <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  13. <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/juntosHibernate"/>
  14. <property name="javax.persistence.jdbc.user" value="root"/>
  15. <property name="javax.persistence.jdbc.password" value="root"/>
  16. <property name="hibernate.show_sql" value="true"/>
  17. <property name="hibernate.format_sql" value="true"/>
  18. <property name="hibernate.hbm2ddl.auto" value="create"/>
  19. </properties>
  20. </persistence-unit>
  21. </persistence>
  22.  
  23. @GET
  24. @Produces("application/json; charset=UTF-8")
  25. @Path("/lista")
  26. public List<Pessoa> ListarTudo(){
  27.  
  28. List<Pessoa> pessoas = new ArrayList<Pessoa>();
  29.  
  30. List<Pessoa> listaEntityPessoas = repository.ListarTudo();
  31.  
  32. for (Pessoa entity : listaEntityPessoas) {
  33.  
  34. pessoas.add(new Pessoa(entity.getId(), entity.getNome(), entity.getSobrenome(), entity.getEmail(),
  35. entity.getSenha(), entity.getTelefone(), entity.getCep(), entity.getLogin(),
  36. entity.getRegistro(), entity.getGacesso_id(), entity.isBanAtivo(),
  37. entity.getLocalizacaoAtual()));
  38. }
  39.  
  40. return pessoas;
  41. }
  42.  
  43. public class PessoaRepository {
  44.  
  45. private final EntityManagerFactory entityManagerFactory;
  46.  
  47. private final EntityManager entityManager;
  48.  
  49. public PessoaRepository(){
  50.  
  51. /*CRIANDO O NOSSO EntityManagerFactory COM AS PORPRIEDADOS DO ARQUIVO persistence.xml */
  52. this.entityManagerFactory = Persistence.createEntityManagerFactory("persistence_unit_juntos");
  53.  
  54. this.entityManager = this.entityManagerFactory.createEntityManager();
  55. }
  56.  
  57. /**
  58. * CRIA UM NOVO REGISTRO NO BANCO DE DADOS
  59. * */
  60. public void Salvar(Pessoa pessoaEntity){
  61.  
  62. this.entityManager.getTransaction().begin();
  63. this.entityManager.persist(pessoaEntity);
  64. this.entityManager.getTransaction().commit();
  65. }
  66.  
  67. /**
  68. * ALTERA UM REGISTRO CADASTRADO
  69. * */
  70. public void Alterar(Pessoa pessoaEntity){
  71.  
  72. this.entityManager.getTransaction().begin();
  73. this.entityManager.merge(pessoaEntity);
  74. this.entityManager.getTransaction().commit();
  75. }
  76.  
  77. /**
  78. * RETORNA TODAS AS PESSOAS CADASTRADAS NO BANCO DE DADOS
  79. * */
  80. @SuppressWarnings("unchecked")
  81. public List<Pessoa> ListarTudo(){
  82.  
  83. return this.entityManager.createQuery("SELECT p FROM Pessoa p ORDER BY p.nome").getResultList();
  84. }
  85.  
  86. /**
  87. * CONSULTA UMA PESSOA CADASTRA PELO CÓDIGO
  88. * */
  89. public Pessoa GetPessoa(Integer id){
  90.  
  91. return this.entityManager.find(Pessoa.class, id);
  92. }
  93.  
  94. /**
  95. * EXCLUINDO UM REGISTRO PELO CÓDIGO
  96. **/
  97. public void Excluir(Integer id){
  98.  
  99. Pessoa pessoa = this.GetPessoa(id);
  100.  
  101. this.entityManager.getTransaction().begin();
  102. this.entityManager.remove(pessoa);
  103. this.entityManager.getTransaction().commit();
  104.  
  105. }
  106.  
  107. public Pessoa Login(String email){
  108.  
  109. return (Pessoa) this.entityManager
  110. .createQuery("SELECT p FROM Pessoa p WHERE p.email = :pemail")
  111. .setParameter("pemail", email).getSingleResult();
  112. }
  113.  
  114. public boolean HabilitarConta(Pessoa pessoa){
  115. return false;
  116. }
  117.  
  118. }
  119.  
  120. @Entity
  121. @Table(name="pessoa")
  122. public class Pessoa {
  123.  
  124. @Id
  125. @GeneratedValue(strategy = GenerationType.IDENTITY)
  126. @Column(name="id")
  127. private int id;
  128.  
  129. @Column(name="nome")
  130. private String nome;
  131.  
  132. @Column(name="sobrenome")
  133. private String sobrenome;
  134.  
  135. @Column(name="email")
  136. private String email;
  137.  
  138. @Column(name="senha")
  139. private String senha;
  140.  
  141. @Column(name="telefone")
  142. private String telefone;
  143.  
  144. @Column(name="cep")
  145. private Cep cep;
  146.  
  147. @Column(name="login")
  148. private Timestamp login;
  149.  
  150. @Column(name="registro")
  151. private Timestamp registro;
  152.  
  153. @Column(name="gacesso_id")
  154. private Permissao gacesso_id;
  155.  
  156. @Column(name="banAtivo")
  157. private boolean banAtivo;
  158.  
  159. @Column(name="localizacaoAtual")
  160. private Localizacao localizacaoAtual;
  161.  
  162. public Pessoa() {
  163.  
  164. }
  165.  
  166. public Pessoa(int id, String nome, String sobrenome, String email, String senha,
  167. String telefone, Cep cep, Timestamp login, Timestamp registro,
  168. Permissao gacesso_id, boolean banAtivo, Localizacao localizacaoAtual) {
  169.  
  170. super();
  171. this.id = id;
  172. this.nome = nome;
  173. this.sobrenome = sobrenome;
  174. this.email = email;
  175. this.senha = senha;
  176. this.telefone = telefone;
  177. this.cep = cep;
  178. this.login = login;
  179. this.registro = registro;
  180. this.gacesso_id = gacesso_id;
  181. this.banAtivo = banAtivo;
  182. this.localizacaoAtual = localizacaoAtual;
  183.  
  184.  
  185. }
  186.  
  187. public int getId() {
  188. return id;
  189. }
  190. public void setId(int id) {
  191. this.id = id;
  192. }
  193. public String getNome() {
  194. return nome;
  195. }
  196. public void setNome(String nome) {
  197. this.nome = nome;
  198. }
  199. public String getSobrenome() {
  200. return sobrenome;
  201. }
  202. public void setSobrenome(String sobrenome) {
  203. this.sobrenome = sobrenome;
  204. }
  205. public String getSenha() {
  206. return senha;
  207. }
  208. public void setSenha(String senha) {
  209. this.senha = senha;
  210. }
  211. public String getTelefone() {
  212. return telefone;
  213. }
  214. public void setTelefone(String telefone) {
  215. this.telefone = telefone;
  216. }
  217. public Cep getCep() {
  218. return cep;
  219. }
  220. public void setCep(Cep cep) {
  221. this.cep = cep;
  222. }
  223. public Timestamp getLogin() {
  224. return login;
  225. }
  226. public void setLogin(Timestamp login) {
  227. this.login = login;
  228. }
  229. public Timestamp getRegistro() {
  230. return registro;
  231. }
  232. public void setRegistro(Timestamp registro) {
  233. this.registro = registro;
  234. }
  235. public Permissao getGacesso_id() {
  236. return gacesso_id;
  237. }
  238. public void setGacesso_id(Permissao gacesso_id) {
  239. this.gacesso_id = gacesso_id;
  240. }
  241.  
  242. public String getEmail() {
  243. return email;
  244. }
  245. public void setEmail(String email) {
  246. this.email = email;
  247. }
  248.  
  249. public Localizacao getLocalizacaoAtual() {
  250. return localizacaoAtual;
  251. }
  252.  
  253. public void setLocalizacaoAtual(Localizacao localizacaoAtual) {
  254. this.localizacaoAtual = localizacaoAtual;
  255. }
  256.  
  257. public boolean isBanAtivo() {
  258. return banAtivo;
  259. }
  260.  
  261. public void setBanAtivo(boolean banAtivo) {
  262. this.banAtivo = banAtivo;
  263. }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement