18
18
package de .dominikschadow .webappsecurity .daos ;
19
19
20
20
import de .dominikschadow .webappsecurity .domain .Customer ;
21
-
22
- import org .hibernate .Criteria ;
23
- import org .hibernate .Query ;
24
- import org .hibernate .Session ;
25
- import org .hibernate .Transaction ;
21
+ import org .hibernate .*;
26
22
import org .hibernate .criterion .Restrictions ;
27
23
import org .slf4j .Logger ;
28
24
import org .slf4j .LoggerFactory ;
29
25
26
+ import java .util .ArrayList ;
30
27
import java .util .List ;
31
28
32
29
import static de .dominikschadow .webappsecurity .daos .HibernateUtil .getSessionFactory ;
@@ -41,37 +38,43 @@ public class CustomerDAO {
41
38
private static final Logger LOGGER = LoggerFactory .getLogger (CustomerDAO .class );
42
39
43
40
public List <Customer > getAllCustomers () {
44
- Session session = getSessionFactory ().openSession ();
45
- Query query = session .createQuery ("FROM Customer" );
46
- @ SuppressWarnings ("unchecked" )
47
- List <Customer > customers = query .list ();
41
+ List <Customer > customers = new ArrayList <>();
48
42
49
- LOGGER .info ("Found {} customers" , customers .size ());
43
+ try (Session session = getSessionFactory ().openSession ()) {
44
+ Query query = session .createQuery ("FROM Customer" );
45
+ customers = query .list ();
50
46
51
- session .close ();
47
+ LOGGER .info ("Found {} customers" , customers .size ());
48
+ } catch (HibernateException ex ) {
49
+ LOGGER .error (ex .getMessage (), ex );
50
+ }
52
51
53
52
return customers ;
54
53
}
55
54
56
55
public void createCustomer (Customer customer ) {
57
- Session session = getSessionFactory ().openSession ();
58
- Transaction tx = session .beginTransaction ();
59
- session .persist (customer );
60
- tx .commit ();
61
- session .close ();
56
+ try (Session session = getSessionFactory ().openSession ()) {
57
+ Transaction tx = session .beginTransaction ();
58
+ session .persist (customer );
59
+ tx .commit ();
60
+ } catch (HibernateException ex ) {
61
+ LOGGER .error (ex .getMessage (), ex );
62
+ }
62
63
}
63
64
64
65
public List <Customer > findCustomers (Customer customer ) {
65
- Session session = getSessionFactory ().openSession ();
66
- Criteria criteria = session .createCriteria (Customer .class );
67
- criteria .add (Restrictions .like ("name" , "%" + customer .getName ()+ "%" ));
66
+ List <Customer > customers = new ArrayList <>();
68
67
69
- @ SuppressWarnings ("unchecked" )
70
- List <Customer > customers = criteria .list ();
68
+ try (Session session = getSessionFactory ().openSession ()) {
69
+ Criteria criteria = session .createCriteria (Customer .class );
70
+ criteria .add (Restrictions .like ("name" , "%" + customer .getName () + "%" ));
71
71
72
- LOGGER . info ( "Found {} customers" , customers . size () );
72
+ customers = criteria . list ( );
73
73
74
- session .close ();
74
+ LOGGER .info ("Found {} customers" , customers .size ());
75
+ } catch (HibernateException ex ) {
76
+ LOGGER .error (ex .getMessage (), ex );
77
+ }
75
78
76
79
return customers ;
77
80
}
0 commit comments