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