summaryrefslogtreecommitdiff
path: root/orkbasej/java/net/sf/oreka/srvc/ObjectServiceHbn.java
blob: d23bc30cc9cc41911874d2adabf468b8788c5c68 (plain)
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
61
62
63
64
65
66
67
package net.sf.oreka.srvc;

import net.sf.oreka.HibernateManager;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ObjectServiceHbn implements ObjectService {

	static Logger logger = Logger.getLogger(ObjectServiceHbn.class);	
	
	public Object getObjectById(java.lang.Class cl, int id) {
		
		Session hbnSession = null;
		Object obj = null;
		
		logger.debug("Retrieving " + cl.getName() +  " object with id:" + id);
		
		try
		{
			hbnSession = HibernateManager.instance().getSession();
			obj = hbnSession.get(cl, id);
		}
		catch ( HibernateException he ) {
			logger.error("getObjectById: exception:" + he.getClass().getName());
		}
		catch (Exception e)
		{
			logger.error("getObjectById: exception:", e);
		}
		finally {
			if(hbnSession != null) {hbnSession.close();}
		}
		return obj;
	}

	public void saveObject(Object obj) {
		
		Session hbnSession = null;
		Transaction tx = null;
		
		logger.debug("Saving " + obj.getClass().getName());
		
		try
		{
			hbnSession = HibernateManager.instance().getSession();
			tx = hbnSession.beginTransaction();
			hbnSession.saveOrUpdate(obj);	// was merge
			tx.commit();
		}
		catch ( HibernateException he ) {
			logger.log(Level.ERROR, he.toString());
			he.printStackTrace();
		}
		catch (Exception e)
		{
			logger.error(e);
			e.printStackTrace();
		}
		finally {
			if(hbnSession != null) {hbnSession.close();}
		}
	}
}