隨筆 - 16  文章 - 0  trackbacks - 0
          <2012年6月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          常用鏈接

          留言簿

          隨筆檔案

          友情鏈接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          第一步、配置實體Bean:
          @Entity
          @Table(name = "t_bd_city")
          public class City extends BaseObject {
             
              /**
               * 省份
               */
              @ManyToOne(fetch = FetchType.LAZY)
              @JoinColumn(name = "fprovinceid")
              private Province province;
          }

          第二步、手動提交保存方法:

          public abstract class CoreDaoHibernate<Entity extends CoreObject> implements CoreObjectDao<Entity> {

              /**
               * Log variable for all child classes. Uses LogFactory.getLog(getClass())
               * from Commons Logging
               */
              protected final Log log = LogFactory.getLog(getClass());
              private Class<Entity> persistentClass ;
              private HibernateTemplate hibernateTemplate;
              private SessionFactory sessionFactory;


              public CoreDaoHibernate(){
                  Class<?> c = this.getClass();
                  Type t = c.getGenericSuperclass();
                  if(t instanceof ParameterizedType){
                     this.persistentClass =  (Class<Entity>)((ParameterizedType) t).getActualTypeArguments()[0];
                  }
              }

              public HibernateTemplate getHibernateTemplate() {
                  return this.hibernateTemplate;
              }

              public SessionFactory getSessionFactory() {
                  return this.sessionFactory;
              }

              @Autowired
              @Required
              public void setSessionFactory(SessionFactory sessionFactory) {
                  this.sessionFactory = sessionFactory;
                  this.hibernateTemplate = new HibernateTemplate(sessionFactory);
              }

              /**
               * {@inheritDoc}
               */
              @Override
              public Entity save(Entity object) {
                  Entity result =  hibernateTemplate.merge(object);
                  hibernateTemplate.flush();
                  return result;
              }
          }

          第三步、配置web.xml
             <filter>
                  <filter-name>lazyLoadingFilter</filter-name>
                  <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
                  <init-param>
                      <param-name>sessionFactoryBeanName</param-name>
                      <param-value>sessionFactory</param-value>
                  </init-param>
                  <init-param>
                      <param-name>singleSession</param-name>
                      <param-value>true</param-value>           
                  </init-param>
                  <init-param>
                      <param-name>flushMode</param-name>
                      <param-value>AUTO</param-value>        
                  </init-param>
              </filter>
            <filter-mapping>
                  <filter-name>lazyLoadingFilter</filter-name>
                  <url-pattern>/*</url-pattern>
              </filter-mapping>

          posted on 2012-06-18 23:24 民工二代 閱讀(314) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 长春市| 西青区| 大方县| 淄博市| 秦皇岛市| 库车县| 增城市| 璧山县| 策勒县| 建阳市| 共和县| 长子县| 历史| 依兰县| 绥芬河市| 呼和浩特市| 浦江县| 浮山县| 康保县| 澄迈县| 延津县| 霸州市| 南华县| 建宁县| 巴里| 班戈县| 浦东新区| 谢通门县| 磐安县| 海丰县| 徐水县| 开封市| 定边县| 汉沽区| 伊春市| 古蔺县| 易门县| 永善县| 中山市| 马尔康县| 兴宁市|