package com aciworldwide dao impl import com aciworldwide dao Customer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.aciworldwide.dao.impl;
import com.aciworldwide.dao.CustomerDao;
import com.aciworldwide.entity.Customer;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
import java.util.List;
@Repository
public class CustomerDaoImpl implements CustomerDao{
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public List<Customer> list() {
@SuppressWarnings("unchecked")
List<Customer> customers = (List<Customer>) sessionFactory.getCurrentSession()
.createCriteria(Customer.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return customers;
}
@Override
@Transactional
public Customer get(long id) {
String hql = "from Customer where id=" + id;
Query query = sessionFactory.getCurrentSession().createQuery(hql);
@SuppressWarnings("unchecked")
List<Customer> customers = (List<Customer>) query.list();
if (customers != null && !list().isEmpty()) {
return customers.get(0);
}
return null;
}
@Override
@Transactional
public void saveOrUpdate(Customer user) {
sessionFactory.getCurrentSession().saveOrUpdate(user);
}
@Override
@Transactional
public void delete(long id) {
Customer customerToDelete = new Customer();
customerToDelete.setId(id);
sessionFactory.getCurrentSession().delete(customerToDelete);
}
}