Rev 2018 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2018 | Rev 2019 | ||
---|---|---|---|
1 | package pt.estgp.es.exemplos.hibernate; |
1 | package pt.estgp.es.exemplos.hibernate; |
2 | 2 | ||
3 | import org.hibernate.HibernateException; |
3 | import org.hibernate.HibernateException; |
4 | import org.hibernate.Session; |
4 | import org.hibernate.Session; |
5 | import org.hibernate.criterion.Order; |
5 | import org.hibernate.criterion.Order; |
6 | 6 | ||
7 | import java.sql.SQLException; |
7 | import java.sql.SQLException; |
8 | import java.util.ArrayList; |
8 | import java.util.ArrayList; |
9 | import java.util.Iterator; |
9 | import java.util.Iterator; |
10 | import java.util.List; |
10 | import java.util.List; |
11 | 11 | ||
12 | 12 | ||
13 | 13 | ||
14 | public class Main { |
14 | public class Main { |
15 | 15 | ||
16 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws 1.5.0/docs/api/java/sql/SQLException.html">SQLException, HibernateException |
16 | public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws 1.5.0/docs/api/java/sql/SQLException.html">SQLException, HibernateException |
17 | { |
17 | { |
18 | 18 | ||
19 | 19 | ||
20 | //HibernateUtils.getCurrentSession().beginTransaction(); |
20 | //HibernateUtils.getCurrentSession().beginTransaction(); |
21 | 21 | ||
22 | //HibernateUtils.getCurrentSession().connection().commit(); |
22 | //HibernateUtils.getCurrentSession().connection().commit(); |
23 | 23 | ||
24 | //HibernateUtils.getCurrentSession().connection().rollback(); |
24 | //HibernateUtils.getCurrentSession().connection().rollback(); |
25 | 25 | ||
26 | HibernateUtils.getCurrentSession().beginTransaction(); |
26 | HibernateUtils.getCurrentSession().beginTransaction(); |
27 | Pagina p = new PaginaImpl(); |
27 | Pagina p = new PaginaImpl(); |
28 | p.setNome("Pagina de Teste"); |
28 | p.setNome("Pagina de Teste"); |
29 | 29 | ||
30 | HibernateUtils.getCurrentSession().save(p); |
30 | HibernateUtils.getCurrentSession().save(p); |
31 | 31 | ||
32 | 32 | ||
33 | 33 | ||
34 | // //HibernateUtils.getCurrentSession().beginTransaction(); |
34 | // //HibernateUtils.getCurrentSession().beginTransaction(); |
35 | // Pagina p = HibernateUtils.getCurrentSession().load(Pagina.class,1); |
35 | // Pagina p = HibernateUtils.getCurrentSession().load(Pagina.class,1); |
36 | // |
36 | // |
37 | // |
37 | // |
38 | // Seccao seccao = new Seccao(); |
38 | // Seccao seccao = new Seccao(); |
39 | // HibernateUtils.getCurrentSession().save(seccao); |
39 | // HibernateUtils.getCurrentSession().save(seccao); |
40 | // seccao.setTitulo("sdfsdf"); |
40 | // seccao.setTitulo("sdfsdf"); |
41 | // p.getSeccoes().add(seccao); |
41 | // p.getSeccoes().add(seccao); |
42 | // seccao.setPagina(p); |
42 | // seccao.setPagina(p); |
43 | //HibernateUtils.getCurrentSession().connection().commit(); |
43 | //HibernateUtils.getCurrentSession().connection().commit(); |
44 | 44 | ||
45 | 45 | ||
46 | Seccao seccao = new SeccaoImpl(); |
46 | Seccao seccao = new SeccaoImpl(); |
47 | 47 | ||
48 | 48 | ||
49 | seccao.setTitulo("Titulo de Teste"); |
49 | seccao.setTitulo("Titulo de Teste"); |
50 | p.setSeccoes(new 1.5.0/docs/api/java/util/ArrayList.html">ArrayList()); |
50 | p.setSeccoes(new 1.5.0/docs/api/java/util/ArrayList.html">ArrayList()); |
51 | p.getSeccoes().add(seccao); |
51 | p.getSeccoes().add(seccao); |
52 | seccao.setPagina(p); |
52 | seccao.setPagina(p); |
53 | 53 | ||
54 | 54 | ||
55 | - | ||
56 | - | ||
57 | - | ||
58 | HibernateUtils.getCurrentSession().connection().commit(); |
55 | HibernateUtils.getCurrentSession().connection().commit(); |
59 | 56 | ||
60 | 57 | ||
61 | Session sess = HibernateUtils.getCurrentSession(); |
58 | Session sess = HibernateUtils.getCurrentSession(); |
62 | sess.beginTransaction(); |
59 | sess.beginTransaction(); |
63 | //Query query = sess.createQuery("select p from p in class Pagina where p.nome like '%Teste%'"); |
60 | //Query query = sess.createQuery("select p from p in class Pagina where p.nome like '%Teste%'"); |
64 | List<Pagina> paginas = sess.createCriteria(Pagina.class).addOrder(Order.asc("nome")).list(); |
61 | List<Pagina> paginas = sess.createCriteria(Pagina.class).addOrder(Order.asc("nome")).list(); |
65 | //List<Pagina> paginas = query.list(); |
62 | //List<Pagina> paginas = query.list(); |
66 | for(Pagina paginaRecuperada: paginas) |
63 | for(Pagina paginaRecuperada: paginas) |
67 | { |
64 | { |
68 | 1.5.0/docs/api/java/lang/System.html">System.out.println(paginaRecuperada.getId() + "-" + paginaRecuperada.getNome()); |
65 | 1.5.0/docs/api/java/lang/System.html">System.out.println(paginaRecuperada.getId() + "-" + paginaRecuperada.getNome()); |
69 | } |
66 | } |
70 | sess.close(); |
67 | sess.close(); |
71 | } |
68 | } |
72 | } |
69 | } |
73 | 70 |