View Javadoc

1   // Created on 13.12.2003
2   package com.pnpconsult.zeiterfassung.helper;
3   
4   import java.util.List;
5   
6   import net.sf.hibernate.Hibernate;
7   import net.sf.hibernate.HibernateException;
8   import net.sf.hibernate.Session;
9   import net.sf.hibernate.Transaction;
10  
11  import com.pnpconsult.zeiterfassung.model.Customer;
12  import com.pnpconsult.zeiterfassung.model.Project;
13  
14  /***
15   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen</a>
16   * @version $Id: CustomerManager.java,v 1.7 2004/06/24 20:54:18 powerpete Exp $
17   */
18  public class CustomerManager extends AbstractObjectManager2
19  {
20  	public List loadAll() throws HibernateException
21  	{
22          return super.loadAll(Customer.class);
23  	}
24  
25  	public void delete(long id) throws HibernateException
26  	{
27  		deleteProjects(id);
28  		super.delete(Customer.class, new Long(id));
29  	}
30  
31  	/***
32  	 * Workaround: If all objects are connected using cascading="all", mysql
33  	 * will fail. So, customer is not connected with cascading="all", but with
34  	 * cascading="none" and the projects are deleted manually.
35  	 * 
36  	 * @param id
37  	 */
38  	private void deleteProjects(long id) throws HibernateException
39  	{
40  		Session session = SessionLocator.getSession();
41          Transaction tx = session.beginTransaction();
42  		try
43  		{
44  			session.delete(
45  				"FROM " + Project.class.getName()
46  					+ " AS project where project.customer.id=?",
47  				new Long(id),
48  				Hibernate.LONG);
49  			tx.commit();
50  		}
51  		catch (HibernateException e)
52  		{
53              tx.rollback();
54              throw e;
55  		}
56          finally
57          {
58              session.close();
59          }
60  	}
61  
62  	public Customer load(long id) throws HibernateException
63  	{
64  		return (Customer) super.load(Customer.class, new Long(id));
65  	}
66  
67  	public void save(Customer customer) throws HibernateException
68  	{
69  		Long key = (Long) super.save(customer);
70  		customer.setId(key.longValue());
71  	}
72  }