Rev 2009 | Rev 2019 | Go to most recent revision | Details | Compare with Previous | 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 | //HibernateUtils.getCurrentSession().beginTransaction(); |
||
21 | |||
22 | //HibernateUtils.getCurrentSession().connection().commit(); |
||
23 | |||
24 | //HibernateUtils.getCurrentSession().connection().rollback(); |
||
25 | |||
26 | HibernateUtils.getCurrentSession().beginTransaction(); |
||
27 | Pagina p = new PaginaImpl(); |
||
28 | p.setNome("Pagina de Teste"); |
||
29 | |||
30 | HibernateUtils.getCurrentSession().save(p); |
||
31 | |||
32 | |||
33 | |||
34 | // //HibernateUtils.getCurrentSession().beginTransaction(); |
||
35 | // Pagina p = HibernateUtils.getCurrentSession().load(Pagina.class,1); |
||
36 | // |
||
37 | // |
||
38 | // Seccao seccao = new Seccao(); |
||
39 | // HibernateUtils.getCurrentSession().save(seccao); |
||
40 | // seccao.setTitulo("sdfsdf"); |
||
41 | // p.getSeccoes().add(seccao); |
||
42 | // seccao.setPagina(p); |
||
43 | //HibernateUtils.getCurrentSession().connection().commit(); |
||
44 | |||
45 | |||
46 | Seccao seccao = new SeccaoImpl(); |
||
47 | |||
48 | |||
49 | seccao.setTitulo("Titulo de Teste"); |
||
50 | p.setSeccoes(new 1.5.0/docs/api/java/util/ArrayList.html">ArrayList()); |
||
51 | p.getSeccoes().add(seccao); |
||
52 | seccao.setPagina(p); |
||
53 | |||
54 | |||
55 | |||
56 | |||
57 | |||
58 | HibernateUtils.getCurrentSession().connection().commit(); |
||
59 | |||
60 | |||
61 | Session sess = HibernateUtils.getCurrentSession(); |
||
62 | sess.beginTransaction(); |
||
63 | //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(); |
||
65 | //List<Pagina> paginas = query.list(); |
||
66 | for(Pagina paginaRecuperada: paginas) |
||
67 | { |
||
68 | 1.5.0/docs/api/java/lang/System.html">System.out.println(paginaRecuperada.getId() + "-" + paginaRecuperada.getNome()); |
||
69 | } |
||
70 | sess.close(); |
||
71 | } |
||
72 | } |