HibernateUtil
public class NewHibernateUtil {
private static final
SessionFactory sessionFactory;
static {
try {
// Create the
SessionFactory from standard (hibernate.cfg.xml)
// config
file.
sessionFactory = new Configuration().configure("/lk/edu/ijse/hp4/config/hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable
ex) {
// Log the
exception.
System.err.println("Initial SessionFactory creation failed." +
ex);
throw new
ExceptionInInitializerError(ex);
}
}
public static
SessionFactory getSessionFactory()
{
return
sessionFactory;
}
}
View
private CustomerControler customerControler=new CustomerControlerImpl();
Entity
@Entity
public class OrderDetail
{
private double up;
private int qty;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumns(@JoinColumn(name = "id",referencedColumnName =
"id",insertable = false,updatable = false))
private Orders order;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumns(@JoinColumn(name="id",referencedColumnName =
"id" ,insertable = false,updatable = false))
private Item item;
@EmbeddedId
private OrderDetail_pk orderDetailDto_pk;
}
@Entity
public class Orders implements
Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String date;
@ManyToOne(cascade =
CascadeType.ALL)
private Customer
customer;
}
CustomerControlerImpl
public class CustomerControlerImpl implements CustomerControler{
CustomerServise
customerServise=new CustomerSreviceImpl();
@Override
public boolean addCustomer(CustomerDto customerDto) throws
Exception {
return
customerServise.registerCustomer(new
Customer(customerDto.getName(),customerDto.getAddress(),customerDto.getTel()));
}
@Override
public boolean updateCustomer(CustomerDto customerDto) throws
Exception {
return customerServise.modifyCustomer(new
Customer(customerDto.getId(),customerDto.getName(),customerDto.getAddress(),customerDto.getTel()));//throw
new UnsupportedOperationException("Not supported yet."); //To change
body of generated methods, choose Tools | Templates.
}
@Override
public boolean deleteCustomer(int id) throws Exception {
return
customerServise.removeCustomer(id);
}
@Override
public CustomerDto searchCustomer(int id) throws Exception {
Customer
customer=customerServise.findCustomer(id);
List<Customer>list=new ArrayList<>();
list.add(customer);
CustomerDto
customerDto=null;
for (Customer
customer1 : list) {
if(customer!=null){
return
new
CustomerDto(customer.getId(),customer.getName(),customer.getAddress(),customer.getTel());
}
}
return null;
}
@Override
public
List<CustomerDto> getAllCustomer()
throws Exception {
List<CustomerDto>list=new ArrayList<>();
List<Customer> customer1 =customerServise.getAllCustomer();
CustomerDto
s=null;
for (Customer
customer : customer1) {
if(customer!=null){
s=new
CustomerDto(customer.getId(),customer.getName(),customer.getAddress(),customer.getTel());
list.add(s);
}
}
return list;
}
}
CustomerServiceImpl
public class CustomerSreviceImpl implements CustomerServise{
CustomerDao
customerDao=new CustomerDaoImpl();
Customer c=new
Customer();
@Override
public boolean registerCustomer(Customer customer) throws
Exception {
SessionFactory sessionFactory=NewHibernateUtil.getSessionFactory();
Session
session=sessionFactory.openSession();
Transaction
tx=null;
try{
tx=session.beginTransaction();
customerDao.setSession(session);
boolean
result=customerDao.addCustomer(customer);
if(result){
tx.commit();
return
true;
}else{
tx.rollback();
return
false;
}
}catch(HibernateException exception){
tx.rollback();
exception.printStackTrace();
return false;
}finally{
session.close();
}
}
@Override
public boolean modifyCustomer(Customer customer) throws
Exception {
SessionFactory
sessionFactory=NewHibernateUtil.getSessionFactory();
Session
session=sessionFactory.openSession();
Transaction
tx=null;
try{
tx=session.beginTransaction();
customerDao.setSession(session);
boolean
result=customerDao.updateCustomer(customer);
if(result){
tx.commit();
return
true;
}else{
tx.rollback();
return
false;
}
}catch(HibernateException exception){
tx.rollback();
exception.printStackTrace();
return false;
}finally{
session.close();
}
}
@Override
public boolean removeCustomer(int id) throws Exception {
SessionFactory
sf=NewHibernateUtil.getSessionFactory();
Session
session=sf.openSession();
Transaction tr=null;
try{
tr=session.beginTransaction();
Customer customer=(Customer) session.get(Customer.class,id);
session.delete(customer);
tr.commit();
return true;
}catch(HibernateException he){
if(tr!=null)
tr.rollback();
he.printStackTrace();
return
false;
}finally{
session.close();
}
}
@Override
public Customer findCustomer(int id) throws Exception {
SessionFactory
sessionFactory=NewHibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession();
Transaction
tx=null;
try{
tx=session.beginTransaction();
customerDao.setSession(session);
Customer
result=customerDao.searchCustomer(id);
return result;
}catch(HibernateException exception){
tx.rollback();
exception.printStackTrace();
return null;
}finally{
session.close();
}
}
@Override
public
List<Customer> getAllCustomer()
throws Exception {
SessionFactory
sessionFactory=NewHibernateUtil.getSessionFactory();
Session
session=sessionFactory.openSession();
Transaction
tx=null;
try{
tx=session.beginTransaction();
customerDao.setSession(session);
List<Customer> result=customerDao.getAllCustomer();
return
result;
}catch(HibernateException exception){
tx.rollback();
exception.printStackTrace();
return null;
}finally{
session.close();
}
}
}
CustomerDaoImpl
public class CustomerDaoImpl implements CustomerDao{
private Session
session;
@Override
public void setSession(Session session) {
this.session=session;
}
@Override
public boolean addCustomer(Customer customer) throws
Exception {
Serializable
saveCustomer=session.save(customer);
if(saveCustomer!=null){
return true;
}
return false;
}
@Override
public boolean updateCustomer(Customer customer) throws
Exception {
session.update(customer);
return true;
}
@Override
public boolean deleteCustomer(int id) throws Exception {
Customer
customer=(Customer) session.get(Customer.class, id);
session.delete(customer);
return true;
}
@Override
public Customer searchCustomer(int id) throws Exception {
Customer customer =
(Customer) session.get(Customer.class,id);
return customer;
}
@Override
public
List<Customer> getAllCustomer()
throws Exception {
Criteria criteria=
session.createCriteria(Customer.class);
List<Customer>list=criteria.list();
return list;
}
}
OrderDetail_pk
@Embeddable
public class OrderDetail_pk implements Serializable{
private int id;
private int oid;
public
OrderDetail_pk() {
}
public
OrderDetail_pk(int id, int oid) {
this.id = id;
this.oid = oid;
}
}
No comments:
Post a Comment