第一步、配置实体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) 编辑 收藏