Changeset 9119

Show
Ignore:
Timestamp:
06/14/08 23:44:01 (4 months ago)
Author:
robert.thurnher
Message:

applies Marius' patches (http://helma.org/pipermail/helma-ng/2008-June/000107.html) plus adds some further polishing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/blog-ng_hibernate/README.txt

    r9096 r9119  
    1010 
    11111. Start your MySQL server and create the DB & user with: 
    12    blog-ng_hibernate/config/db.sql 
     12   blog-ng_hibernate/build/db.sql 
    1313 
    14142. Issue, e.g., the following command in the blog-ng_hibernate directory: 
    1515 
    16    java -jar ../helma-ng/run.jar app lib 
     16   java -jar ../helma-ng/run.jar app lib config 
    1717 
    18183. Then point your browser to: 
  • sandbox/blog-ng_hibernate/app/controllers/blog.js

    r9097 r9119  
    88 
    99function main_action() { 
    10    var articles = db.find('from Article a order by a.createTime desc'); 
     10   //var articles = db.find('from Article a order by a.createTime desc'); 
     11   var articles = db.list('Article', 50); 
    1112   var context = { 
    1213      loginLink: function (macrotag, skin) { 
  • sandbox/blog-ng_hibernate/app/main.js

    r9096 r9119  
    11importModule('helma.app', 'app'); 
     2importModule('helma.rhino', 'rhino'); 
    23importFromModule('helma.simpleweb', 'handleRequest'); 
    34importModule('helma.logging', 'logging'); 
    45var log = logging.getLogger(__name__); 
     6var txn; 
    57 
    68importModule('hibernate', 'db'); 
     
    1214   app.start({ staticDir: '../static' }); 
    1315   log.info('Welcome to Blog NG! ^^'); 
     16 
     17   rhino.addCallback('onRequest', 'beginTxn', function () { 
     18      var sess = db.getSession(); 
     19      txn = sess.beginTransaction(); 
     20   }); 
     21 
     22   rhino.addCallback('onResponse', 'commitTxn', function () { 
     23      txn.commit(); 
     24   }); 
    1425} 
    1526 
     
    1728function main_action() { 
    1829   if (db.all('User').size() == 0) { 
    19       res.redirect('account'); 
     30      res.redirect('account'); 
    2031   } 
    2132   res.redirect('blog'); 
  • sandbox/blog-ng_hibernate/app/mappings/Article.hbm.xml

    r9091 r9119  
    66 
    77    <class entity-name="Article" table="BLOG_ARTICLES"> 
     8        <cache usage="read-write"/> 
    89 
    910        <id name="id" 
  • sandbox/blog-ng_hibernate/app/mappings/User.hbm.xml

    r9069 r9119  
    66 
    77    <class entity-name="User" table="BLOG_USERS"> 
     8        <cache usage="read-write"/> 
    89 
    910        <id name="id" 
  • sandbox/blog-ng_hibernate/lib/hibernate.js

    r9096 r9119  
    1313importJar('hibernate/hibernate3.jar'); 
    1414importJar('hibernate/jta.jar'); 
     15importJar('hibernate/c3p0-0.9.1.jar'); 
     16importJar('hibernate/ehcache-1.2.3.jar'); 
    1517importJar('hibernate/mysql-connector-java-5.1.6-bin.jar'); 
    1618 
     
    2022var __shared__ = true; 
    2123 
    22 // used to determine whether config should be re-built on each new DB session 
     24// used to determine whether config should be re-built on each request 
    2325var isDevEnvironment = false; 
    2426 
    2527// used to get path of db.properties and mapping files 
    26 var configPropsRelativePath = '../config/db.properties'; 
     28var configPropsRelativePath = 'hibernate.properties'; 
    2729var configDirRelativePath   = 'mappings'; 
    2830 
     
    3537   var sessionFactory; 
    3638   var sess; 
     39 
     40 
     41   this.getSession = function() { 
     42      if (this.isDevEnvironment || !isConfigured) { 
     43         this.setConfig(); 
     44      } 
     45      return sessionFactory.getCurrentSession(); 
     46   } 
     47 
    3748 
    3849   /** 
     
    5768      isConfigured = true; 
    5869      log.info('Configuration set successfully.'); 
     70      sessionFactory = config.buildSessionFactory(); 
    5971 
    6072      return; 
     
    6779    */ 
    6880   this.getHibernateTemplate = function (params) { 
    69       if (this.isDevEnvironment || !isConfigured) { 
    70          this.setConfig(); 
    71          sessionFactory = config.buildSessionFactory(); 
    72          sess = sessionFactory.openSession(); 
    73       } 
    74  
    75       var txn = sess.beginTransaction(); 
     81      var sess = this.getSession(); 
    7682 
    7783      // do the actual operation 
     
    102108            sess['delete(java.lang.Object)'](params.object); 
    103109            break; 
     110         case 'list': 
     111            var crit = sess['createCriteria(java.lang.String)'](params.type); 
     112            crit.setCacheable(true); 
     113            crit.setMaxResults(params.count); 
     114            var result = new ScriptableList(crit.list()); 
     115            for (var i in result) { 
     116               result[i] = new ScriptableMap(result[i]); 
     117            } 
     118            break; 
    104119         default: 
    105120            break; 
    106121      } 
    107122 
    108       txn.commit(); 
    109       //sess.close(); 
    110       //sessionFactory.close(); 
    111  
    112123      return result || null; 
    113124   }; 
     
    157168      } catch (e) { 
    158169         log.error('in "remove": ' + e.toString()); 
     170         return; 
     171      } 
     172   }; 
     173 
     174   this.list = function (type, count) { 
     175      try { 
     176         return this.getHibernateTemplate({ method: 'list', type: type, count: count }); 
     177      } catch (e) { 
     178         log.error('in "list": ' + e.toString()); 
    159179         return; 
    160180      }