You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2006/04/14 22:20:51 UTC
svn commit: r394187 -
/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java
Author: agilliland
Date: Fri Apr 14 13:20:48 2006
New Revision: 394187
URL: http://svn.apache.org/viewcvs?rev=394187&view=rev
Log:
adding new storeAndCommit() and removeAndCommit() methods for hierarchical objects.
Modified:
incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java
Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java?rev=394187&r1=394186&r2=394187&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java (original)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernatePersistenceStrategy.java Fri Apr 14 13:20:48 2006
@@ -68,9 +68,6 @@
* it will return the already open Session.
*/
protected Session getSession() {
-
- log.debug("Getting hibernate session");
-
return sessionFactory.getCurrentSession();
}
@@ -433,6 +430,36 @@
/**
+ * Store hierarchical object and commit it.
+ *
+ * If the commit fails then we rollback and release the session.
+ */
+ protected void storeAndCommit(HierarchicalPersistentObject obj) throws RollerException {
+
+ try {
+ // begin transaction
+ getSession().beginTransaction();
+
+ this.store(obj);
+
+ // commit changes
+ getSession().getTransaction().commit();
+ } catch (Throwable ex) {
+
+ try {
+ getSession().getTransaction().rollback();
+ } catch(Throwable he) {
+ log.error("Error doing rollback", he);
+ }
+
+ release();
+
+ throw new RollerException(ex);
+ }
+ }
+
+
+ /**
* Store hierarchical object.
*
* NOTE: if the object has proper cascade setting then is all this necessary?
@@ -444,13 +471,29 @@
throw new HibernateException("Cannot save null object");
}
+ log.info("storing hierarchical object "+obj);
+
Session session = getSession();
HierarchicalPersistentObject mNewParent = obj.getNewParent();
boolean fresh = (obj.getId() == null || "".equals(obj.getId()));
- // TODO BACKEND: saveOrUpdate()? check if transient?
- session.save(obj);
+ // TODO BACKEND: saveOrUpdate()?
+ if (fresh) {
+ // Object has never been written to database, so save it.
+ // This makes obj into a persistent instance.
+ session.save(obj);
+ }
+
+ if(!session.contains(obj)) {
+
+ // Object has been written to database, but instance passed in
+ // is not a persistent instance, so must be loaded into session.
+ HierarchicalPersistentObject vo =
+ (HierarchicalPersistentObject)session.load(obj.getClass(),obj.getId());
+ vo.setData(obj);
+ obj = vo;
+ }
if (fresh) {
// Every fresh cat needs a parent assoc
@@ -460,6 +503,8 @@
} else if (null != mNewParent) {
// New parent must be added to parentAssoc
Assoc parentAssoc = obj.getParentAssoc();
+ if(parentAssoc == null)
+ log.error("parent assoc is null");
parentAssoc.setAncestor(mNewParent);
this.store(parentAssoc);
}
@@ -520,6 +565,40 @@
/**
+ * Remove hierarchical object and commit.
+ *
+ * If the commit fails then we rollback and release.
+ */
+ protected void removeAndCommit(HierarchicalPersistentObject obj) throws RollerException {
+
+ if(obj == null) {
+ throw new RollerException("Cannot remove null object");
+ }
+
+ try {
+ // begin transaction
+ getSession().beginTransaction();
+
+ this.remove(obj);
+
+ // commit changes
+ getSession().getTransaction().commit();
+ } catch (Throwable ex) {
+
+ try {
+ getSession().getTransaction().rollback();
+ } catch(Throwable he) {
+ log.error("Error doing rollback", he);
+ }
+
+ release();
+
+ throw new RollerException(ex);
+ }
+ }
+
+
+ /**
* Remove hierarchical object.
*
* NOTE: if the object has proper cascade setting then is all this necessary?
@@ -529,6 +608,8 @@
if(obj == null) {
throw new RollerException("Cannot remove null object");
}
+
+ log.debug("Removing hierarchical object "+obj.getId());
// loop to remove all descendents and associations
List toRemove = new LinkedList();