Now using JPA with a container managed entity manager.
[utils] / mythtv / monitor / src / main / java / org / wamblee / mythtv / RecordingDatabase.java
index 7ff517096e2659c5bf7d6a6855c3d670e311b06c..e4badfba898e40f01857b15d5ef86a8fdec18112 100644 (file)
@@ -21,6 +21,9 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.HibernateException;
@@ -28,11 +31,13 @@ import org.hibernate.Session;
 import org.hibernate.criterion.Expression;
 import org.springframework.orm.hibernate3.HibernateCallback;
 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
+import org.springframework.orm.jpa.JpaTemplate;
+import org.springframework.orm.jpa.support.JpaDaoSupport;
 
 /**
  * 
  */
-public class RecordingDatabase extends HibernateDaoSupport {
+public class RecordingDatabase extends JpaDaoSupport {
     
     private static final Log LOG = LogFactory.getLog(RecordingDatabase.class);
 
@@ -50,14 +55,12 @@ public class RecordingDatabase extends HibernateDaoSupport {
     }
     
     public Recording findRecording(final String aName) {
-        List<Recording> result = (List<Recording>) getHibernateTemplate().execute(new HibernateCallback() {
-           /* (non-Javadoc)
-             * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session)
-             */
-            public Object doInHibernate(Session aSession) throws HibernateException, SQLException {
-                return aSession.createCriteria(Recording.class).add(Expression.eq("_basename", aName)).list();
-            } 
-        });
+        JpaTemplate jpaTemplate = getJpaTemplate();
+        EntityManager entityManager = jpaTemplate.getEntityManager();
+        Query query = entityManager.createQuery(
+                "from org.wamblee.mythtv.Recording where basename = ?1");
+        query.setParameter(1, aName);
+        List<Recording> result = query.getResultList();
         if ( result.size() > 1 ) { 
             throw new RuntimeException("More than two recordings returned");
         }
@@ -68,6 +71,8 @@ public class RecordingDatabase extends HibernateDaoSupport {
     }
     
     public void update(Recording aRecording) { 
-        getHibernateTemplate().update(aRecording);
+        // Update is not required since the whole task of updating the 
+        // directory structure occurs within a single transaction. 
+        // Therefore, modifications to recordings are automatically persisted. 
     }
 }