persistence注意事项
2009-09-09 08:59 Jvpy 阅读(153) 评论(0) 收藏 举报
1. Don't simply return data which was put into cache, make a shallow copy of that.
1 List value = 
2 cache.put(key, value);
3 return new ArrayList(value);

2 cache.put(key, value);
3 return new ArrayList(value);
2.Do not construct query string directly. Use something like:
1 String sqlString = "insert into application_authorization(application_id, security_token)"
2 + " values(:applicationId, :securityToken)";
3 Query query = session.createSQLQuery(sqlString);
4 query.setLong("applicationId", applicationId);
5 query.setString("securityToken", hashSecurityToken);
2 + " values(:applicationId, :securityToken)";
3 Query query = session.createSQLQuery(sqlString);
4 query.setLong("applicationId", applicationId);
5 query.setString("securityToken", hashSecurityToken);
3. To get entity by id, simply use session.get(clazz, id);
4. Code rafactoring!extract the frequently used code to util or helper class!
5. 数据库操作的component单元测试中,要在tearDown中做清理数据库的操作。