---
title: "Light-commons 加入orm模块"
author: "jasongreen"
date: "2008-06-15T15:36:33Z"
tags: ["Hibernate", "ORM", "SVN", "Spring", "maven"]
categories: ["企业架构"]
slug: "light-commons-orm"
draft: false
lastmod: "2008-06-15T15:36:33Z"
id: 203478
url: "https://jasongreen.iteye.com/blog/203478"
---

light-commons 计划创建一个ORM通用接口层，并提供一个jdbc实现和一个hibernate实现。今天创建了接口以及Hibernate的实现。 1. DaoTemplate \[code="java"] HibernateDaoTemplate daoTemplate = new HibernateDaoTemplate(sessionFactory); //与spring的 hibernateTemplate 类似 //MyPojo mypojo = (MyPojo) hibernateTemplate.get(MyPojo.class,1L); //与spring的HibernateTemplate不同的是，这里不需要类型转换 MyPojo mypojo = daoTemplate.get(MyPojo.class,1L); \[/code] 2. Dao \[code="java"] public class MyPojoDao extends HibernateDao{ //必要的参数在构造函数里传入，而不是用set方法注入，保证thread-safe public MyPojoDao(HibernateDaoTemplate daoTemplate){ super(daoTemplate); } //that's all,当然，你也可以在这里加入自己的方法。 } ... MyPojoDao myPojoDao = new MyPojoDao(hibernateDaoTemplate); //不需要类型转换 MyPojo myPojo = myPojoDao.get(1L); \[/code] 3. DaoFactory \[code="java"] HibernateDaoFactory hibernateDaoFactory=new HibernateDaoFactory(sessionFactory); //指定Entity Class, 指定 ID Class Dao myPojoDao= hibernateDaoFactory.getDaoOf(MyPojo.class); //以get方法为例 //参数仅允许 ID Class ，上面指定的是Long 型 //返回值不再需要类型转换 MyPojo mypojo = myPojoDao.get(1L);\[/code] Project Home: \[url]http://light-commons.googlecode.com\[/url] maven dependency: \[code="xml"] light-commons http://light-commons.googlecode.com/svn/repository org.lightcommons lightcommons 0.1.0-beta ... \[/code]