随笔 - 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 民工二代 阅读(308) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: