Skip to content

Commit f9972c7

Browse files
committed
ContatoDAO renomeado para ContatoDAOImpl;
Interface ContatoDAO adicionada, ContatoDAOImpl implementando ContatoDAO; Métodos que fazem chamadas ao banco de dados agora iniciam-se abrindo uma nova sessão e fecham essa sessão ao final.
1 parent 6fcf951 commit f9972c7

File tree

8 files changed

+240
-157
lines changed

8 files changed

+240
-157
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/nbproject/private/
2+
/build/

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ Projeto simples desenvolvido no NetBeans utilizando a linguagem Java fazendo int
55
As operações exemplificadas nesse projeto são as de: inserção, leitura, alteração e remoção (CRUD) de contatos no banco de dados MySQL.
66

77
* Downloads: https://github.com/iagocolodetti/JavaHibernateExemplo/releases
8-
* [Arquivo de Script MySQL](https://github.com/iagocolodetti/JavaHibernateExemplo/releases/download/v1.0/contatodb.sql "contatodb.sql")
9-
* [Bibliotecas Necessárias](https://github.com/iagocolodetti/JavaHibernateExemplo/releases/download/v1.0/bibliotecas.zip "bibliotecas.zip")
10-
* [Código-Fonte](https://github.com/iagocolodetti/JavaHibernateExemplo/archive/v1.0.zip "v1.0.zip")
8+
* [Arquivo de Script MySQL](https://github.com/iagocolodetti/JavaHibernateExemplo/releases/download/v1.1/contatodb.sql "contatodb.sql")
9+
* [Bibliotecas Necessárias](https://github.com/iagocolodetti/JavaHibernateExemplo/releases/download/v1.1/bibliotecas.zip "bibliotecas.zip")
10+
* [Código-Fonte](https://github.com/iagocolodetti/JavaHibernateExemplo/archive/v1.1.zip "v1.1.zip")
1111

1212

1313

nbproject/project.properties

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
annotation.processing.enabled=true
22
annotation.processing.enabled.in.editor=false
3-
annotation.processing.processor.options=
43
annotation.processing.processors.list=
54
annotation.processing.run.all.processors=true
65
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
6+
application.title=JavaHibernateExemplo
7+
application.vendor=Iago Colodetti
8+
auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml
79
build.classes.dir=${build.dir}/classes
810
build.classes.excludes=**/*.java,**/*.form
911
# This directory is removed when the project is cleaned:
@@ -26,6 +28,7 @@ dist.archive.excludes=
2628
dist.dir=dist
2729
dist.jar=${dist.dir}/JavaHibernateExemplo.jar
2830
dist.javadoc.dir=${dist.dir}/javadoc
31+
endorsed.classpath=
2932
excludes=
3033
file.reference.antlr-2.7.7.jar=lib/antlr-2.7.7.jar
3134
file.reference.c3p0-0.9.2.1.jar=lib/c3p0-0.9.2.1.jar
@@ -45,7 +48,9 @@ file.reference.mysql-connector-java-5.1.23-bin.jar=lib/mysql-connector-java-5.1.
4548
file.reference.slf4j-api-1.6.1.jar=lib/slf4j-api-1.6.1.jar
4649
file.reference.slf4j-simple-1.6.1.jar=lib/slf4j-simple-1.6.1.jar
4750
includes=**
51+
jar.archive.disabled=${jnlp.enabled}
4852
jar.compress=false
53+
jar.index=${jnlp.enabled}
4954
javac.classpath=\
5055
${file.reference.antlr-2.7.7.jar}:\
5156
${file.reference.c3p0-0.9.2.1.jar}:\
@@ -88,11 +93,29 @@ javadoc.splitindex=true
8893
javadoc.use=true
8994
javadoc.version=false
9095
javadoc.windowtitle=
96+
jnlp.codebase.type=no.codebase
97+
jnlp.descriptor=application
98+
jnlp.enabled=false
99+
jnlp.mixed.code=default
100+
jnlp.offline-allowed=false
101+
jnlp.signed=false
102+
jnlp.signing=
103+
jnlp.signing.alias=
104+
jnlp.signing.keystore=
91105
main.class=main
106+
# Substitui\u00e7\u00e3o opcional do atributo padr\u00e3o Application-Library-Allowable-Codebase que identifica os locais onde se espera que o RIA assinado seja encontrado.
107+
manifest.custom.application.library.allowable.codebase=
108+
# Substitui\u00e7\u00e3o opcional do atributo padr\u00e3o Caller-Allowable-Codebase que identifica os dom\u00ednios dos quais o c\u00f3digo JavaScript pode fazer chamadas para o RIA sem avisos de seguran\u00e7a.
109+
manifest.custom.caller.allowable.codebase=
110+
# Substitui\u00e7\u00e3o opcional do atributo de manifesto da Base de c\u00f3digo; use para impedir que RIAs sejam adaptados
111+
manifest.custom.codebase=
112+
# Substitui\u00e7\u00e3o opcional do atributo de manifesto de Permiss\u00f5es (valores suportados: sandbox, all-permissions)
113+
manifest.custom.permissions=
92114
manifest.file=manifest.mf
93115
meta.inf.dir=${src.dir}/META-INF
94116
mkdist.disabled=false
95117
platform.active=default_platform
118+
project.license=default
96119
run.classpath=\
97120
${javac.classpath}:\
98121
${build.classes.dir}

src/Contato.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
@Entity
1515
@Table(name = "Contato")
1616
public class Contato implements Serializable {
17-
17+
18+
private static final long serialVersionUID = -2319598745423332344L;
19+
1820
@Id
1921
@GeneratedValue(strategy = GenerationType.IDENTITY) // AUTO_INCREMENT
2022
private int id;
@@ -24,15 +26,16 @@ public class Contato implements Serializable {
2426
private String telefone;
2527
@Column
2628
private String email;
27-
28-
public Contato() {}
29-
29+
30+
public Contato() {
31+
}
32+
3033
public Contato(String nome, String telefone, String email) {
3134
this.nome = nome;
3235
this.telefone = telefone;
3336
this.email = email;
3437
}
35-
38+
3639
public Contato(int id, String nome, String telefone, String email) {
3740
this.id = id;
3841
this.nome = nome;

src/ContatoDAO.java

Lines changed: 9 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,26 @@
11

22
import java.util.List;
33
import org.hibernate.HibernateException;
4-
import org.hibernate.Session;
5-
import org.hibernate.Transaction;
64

75
/**
86
*
97
* @author iagocolodetti
108
*/
11-
public class ContatoDAO {
9+
public interface ContatoDAO {
1210

13-
private Session session = null;
11+
public void add(Contato contato) throws HibernateException;
1412

15-
public ContatoDAO() {
16-
session = HibernateUtil.getSessionFactory().openSession();
17-
}
13+
public Contato getContato(int id) throws ContatoNaoExisteException;
1814

19-
// <editor-fold defaultstate="collapsed" desc="Comando(s) de Criação/Escrita (CREATE)">
20-
public void add(Contato contato) throws HibernateException {
15+
public List<Contato> getContatosPorNome(String nome) throws ContatoNaoExisteException, HibernateException;
2116

22-
try {
23-
Transaction tx = session.beginTransaction();
24-
session.save(contato);
25-
tx.commit();
26-
session.flush();
27-
} catch (HibernateException e) {
28-
throw new HibernateException("Não foi possível adicionar o contato.");
29-
}
30-
}
31-
// </editor-fold>
17+
public List<Contato> getContatosPorTelefone(String telefone) throws ContatoNaoExisteException, HibernateException;
3218

33-
// <editor-fold defaultstate="collapsed" desc="Comando(s) de Leitura/Busca (READ)">
34-
public Contato getContato(int id) throws ContatoNaoExisteException {
19+
public List<Contato> getContatosPorEmail(String email) throws ContatoNaoExisteException, HibernateException;
3520

36-
Contato contato = null;
21+
public List<Contato> getContatos() throws ContatoNaoExisteException, HibernateException;
3722

38-
try {
39-
contato = (Contato) session.get(Contato.class, id);
40-
session.flush();
41-
if (contato == null) {
42-
throw new ContatoNaoExisteException("Não existe contato com esse ID.");
43-
}
44-
} catch (HibernateException e) {
45-
throw new HibernateException("Não foi possível buscar o contato.");
46-
}
23+
public void update(Contato contato) throws ContatoNaoExisteException, HibernateException;
4724

48-
return contato;
49-
}
50-
51-
public List<Contato> getContatosPorNome(String nome) throws ContatoNaoExisteException, HibernateException {
52-
53-
List<Contato> contatos = null;
54-
55-
try {
56-
contatos = (List<Contato>) session.createSQLQuery("SELECT * FROM Contato WHERE nome LIKE '%" + nome + "%'").addEntity(Contato.class).list();
57-
session.flush();
58-
if (contatos == null || contatos.isEmpty()) {
59-
throw new ContatoNaoExisteException("Não existem contatos com esse nome ou parte dele.");
60-
}
61-
} catch (HibernateException e) {
62-
throw new HibernateException("Não foi possível buscar os contatos.");
63-
}
64-
65-
return contatos;
66-
}
67-
68-
public List<Contato> getContatosPorTelefone(String telefone) throws ContatoNaoExisteException, HibernateException {
69-
70-
List<Contato> contatos = null;
71-
72-
try {
73-
contatos = (List<Contato>) session.createSQLQuery("SELECT * FROM Contato WHERE telefone LIKE '%" + telefone + "%'").addEntity(Contato.class).list();
74-
session.flush();
75-
if (contatos == null || contatos.isEmpty()) {
76-
throw new ContatoNaoExisteException("Não existem contatos com esse telefone ou parte dele.");
77-
}
78-
} catch (HibernateException e) {
79-
throw new HibernateException("Não foi possível buscar os contatos.");
80-
}
81-
82-
return contatos;
83-
}
84-
85-
public List<Contato> getContatosPorEmail(String email) throws ContatoNaoExisteException, HibernateException {
86-
87-
List<Contato> contatos = null;
88-
89-
try {
90-
contatos = (List<Contato>) session.createSQLQuery("SELECT * FROM Contato WHERE email LIKE '%" + email + "%'").addEntity(Contato.class).list();
91-
session.flush();
92-
if (contatos == null || contatos.isEmpty()) {
93-
throw new ContatoNaoExisteException("Não existem contatos com esse e-mail ou parte dele.");
94-
}
95-
} catch (HibernateException e) {
96-
throw new HibernateException("Não foi possível buscar os contatos.");
97-
}
98-
99-
return contatos;
100-
}
101-
102-
public List<Contato> getContatos() throws ContatoNaoExisteException, HibernateException {
103-
104-
List<Contato> contatos = null;
105-
106-
try {
107-
contatos = (List<Contato>) session.createSQLQuery("SELECT * FROM Contato").addEntity(Contato.class).list();
108-
session.flush();
109-
if (contatos == null || contatos.isEmpty()) {
110-
throw new ContatoNaoExisteException("Não existem contatos.");
111-
}
112-
} catch (HibernateException e) {
113-
throw new HibernateException("Não foi possível buscar os contatos.");
114-
}
115-
116-
return contatos;
117-
}
118-
// </editor-fold>
119-
120-
// <editor-fold defaultstate="collapsed" desc="Comando(s) de Atualização/Alteração (UPDATE)">
121-
public void update(Contato contato) throws ContatoNaoExisteException, HibernateException {
122-
123-
try {
124-
if (session.get(Contato.class, contato.getId()) == null) {
125-
throw new ContatoNaoExisteException("Não existe contato com esse ID.");
126-
}
127-
Transaction tx = session.beginTransaction();
128-
session.update(contato);
129-
tx.commit();
130-
session.flush();
131-
} catch (HibernateException e) {
132-
throw new HibernateException("Não foi possível alterar/atualizar o contato.");
133-
}
134-
}
135-
// </editor-fold>
136-
137-
// <editor-fold defaultstate="collapsed" desc="Comando(s) de Exclusão/Remoção (DELETE)">
138-
public void delete(int id) throws ContatoNaoExisteException, HibernateException {
139-
140-
try {
141-
Transaction tx = session.beginTransaction();
142-
session.delete(getContato(id));
143-
tx.commit();
144-
session.flush();
145-
} catch (ContatoNaoExisteException e) {
146-
throw new ContatoNaoExisteException(e.getMessage());
147-
} catch (HibernateException e) {
148-
throw new HibernateException("Não foi possível excluir/remover o contato.");
149-
}
150-
}
151-
// </editor-fold>
152-
153-
public void close() {
154-
try {
155-
session.close();
156-
} catch (HibernateException e) {
157-
e.printStackTrace();
158-
}
159-
}
25+
public void delete(int id) throws ContatoNaoExisteException, HibernateException;
16026
}

0 commit comments

Comments
 (0)