Changeset 9119
- Timestamp:
- 06/14/08 23:44:01 (4 months ago)
- Files:
-
- sandbox/blog-ng_hibernate/README.txt (modified) (1 diff)
- sandbox/blog-ng_hibernate/app/controllers/blog.js (modified) (1 diff)
- sandbox/blog-ng_hibernate/app/main.js (modified) (3 diffs)
- sandbox/blog-ng_hibernate/app/mappings/Article.hbm.xml (modified) (1 diff)
- sandbox/blog-ng_hibernate/app/mappings/User.hbm.xml (modified) (1 diff)
- sandbox/blog-ng_hibernate/build (added)
- sandbox/blog-ng_hibernate/build/db.sql (moved) (moved from sandbox/blog-ng_hibernate/config/db.sql)
- sandbox/blog-ng_hibernate/build/hibernate.properties (moved) (moved from sandbox/blog-ng_hibernate/config/db.properties)
- sandbox/blog-ng_hibernate/config/ehcache.xml (added)
- sandbox/blog-ng_hibernate/config/hibernate.properties (added)
- sandbox/blog-ng_hibernate/lib/hibernate.js (modified) (7 diffs)
- sandbox/blog-ng_hibernate/lib/hibernate/c3p0-0.9.1.jar (added)
- sandbox/blog-ng_hibernate/lib/hibernate/ehcache-1.2.3.jar (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/blog-ng_hibernate/README.txt
r9096 r9119 10 10 11 11 1. Start your MySQL server and create the DB & user with: 12 blog-ng_hibernate/ config/db.sql12 blog-ng_hibernate/build/db.sql 13 13 14 14 2. Issue, e.g., the following command in the blog-ng_hibernate directory: 15 15 16 java -jar ../helma-ng/run.jar app lib 16 java -jar ../helma-ng/run.jar app lib config 17 17 18 18 3. Then point your browser to: sandbox/blog-ng_hibernate/app/controllers/blog.js
r9097 r9119 8 8 9 9 function 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); 11 12 var context = { 12 13 loginLink: function (macrotag, skin) { sandbox/blog-ng_hibernate/app/main.js
r9096 r9119 1 1 importModule('helma.app', 'app'); 2 importModule('helma.rhino', 'rhino'); 2 3 importFromModule('helma.simpleweb', 'handleRequest'); 3 4 importModule('helma.logging', 'logging'); 4 5 var log = logging.getLogger(__name__); 6 var txn; 5 7 6 8 importModule('hibernate', 'db'); … … 12 14 app.start({ staticDir: '../static' }); 13 15 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 }); 14 25 } 15 26 … … 17 28 function main_action() { 18 29 if (db.all('User').size() == 0) { 19 res.redirect('account');30 res.redirect('account'); 20 31 } 21 32 res.redirect('blog'); sandbox/blog-ng_hibernate/app/mappings/Article.hbm.xml
r9091 r9119 6 6 7 7 <class entity-name="Article" table="BLOG_ARTICLES"> 8 <cache usage="read-write"/> 8 9 9 10 <id name="id" sandbox/blog-ng_hibernate/app/mappings/User.hbm.xml
r9069 r9119 6 6 7 7 <class entity-name="User" table="BLOG_USERS"> 8 <cache usage="read-write"/> 8 9 9 10 <id name="id" sandbox/blog-ng_hibernate/lib/hibernate.js
r9096 r9119 13 13 importJar('hibernate/hibernate3.jar'); 14 14 importJar('hibernate/jta.jar'); 15 importJar('hibernate/c3p0-0.9.1.jar'); 16 importJar('hibernate/ehcache-1.2.3.jar'); 15 17 importJar('hibernate/mysql-connector-java-5.1.6-bin.jar'); 16 18 … … 20 22 var __shared__ = true; 21 23 22 // used to determine whether config should be re-built on each new DB session24 // used to determine whether config should be re-built on each request 23 25 var isDevEnvironment = false; 24 26 25 27 // used to get path of db.properties and mapping files 26 var configPropsRelativePath = ' ../config/db.properties';28 var configPropsRelativePath = 'hibernate.properties'; 27 29 var configDirRelativePath = 'mappings'; 28 30 … … 35 37 var sessionFactory; 36 38 var sess; 39 40 41 this.getSession = function() { 42 if (this.isDevEnvironment || !isConfigured) { 43 this.setConfig(); 44 } 45 return sessionFactory.getCurrentSession(); 46 } 47 37 48 38 49 /** … … 57 68 isConfigured = true; 58 69 log.info('Configuration set successfully.'); 70 sessionFactory = config.buildSessionFactory(); 59 71 60 72 return; … … 67 79 */ 68 80 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(); 76 82 77 83 // do the actual operation … … 102 108 sess['delete(java.lang.Object)'](params.object); 103 109 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; 104 119 default: 105 120 break; 106 121 } 107 122 108 txn.commit();109 //sess.close();110 //sessionFactory.close();111 112 123 return result || null; 113 124 }; … … 157 168 } catch (e) { 158 169 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()); 159 179 return; 160 180 }