Hibernate 对象操作 和 HQL
来源:互联网 发布:eva防水板js是什么意思 编辑:程序博客网 时间:2024/06/06 14:00
今天进行了最简单的Hibernate对象操作的CRUD
和get set 方法, 构造方法 等来提供Spring注入
今天就先到这了~
不过,不像以前单独使用Hibernate,而是使用在Spring托管下的Hibernate的。而且也不用Hibernate本身了,而是HibernateTemplate 这个封装好的类。
简单说来,其实就两个方面。
一、对象
这个相当的熟悉,又有不同之处:由继承HibernateDaoSupport的一个类(一般还实现一个接口),等待Spring容器的注入,
<bean id="idaoToolService"class="com.sms.idao.impl.DaoToolServiceImpl"><!-- property 的名字必须和ref 一样???? --><property name="sessionFactory"><ref bean="sessionFactory" /></property></bean>
其实还是有点不明白为什么注入了sessionFactory的。不过,源码解释了一切
可以清楚的发现,当DaoToolServiceImpl extends HibernateDaoSupport implements IDaoToolService 时,
它有了
private HibernateTemplate hibernateTemplate;
和get set 方法, 构造方法 等来提供Spring注入
/* * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.orm.hibernate3.support;import org.hibernate.HibernateException;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.springframework.dao.DataAccessException;import org.springframework.dao.DataAccessResourceFailureException;import org.springframework.dao.support.DaoSupport;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.orm.hibernate3.SessionFactoryUtils;/** * Convenient super class for Hibernate-based data access objects. * * <p>Requires a {@link org.hibernate.SessionFactory} to be set, providing a * {@link org.springframework.orm.hibernate3.HibernateTemplate} based on it to * subclasses through the {@link #getHibernateTemplate()} method. * Can alternatively be initialized directly with a HibernateTemplate, * in order to reuse the latter's settings such as the SessionFactory, * exception translator, flush mode, etc. * * <p>This base class is mainly intended for HibernateTemplate usage but can * also be used when working with a Hibernate Session directly, for example * when relying on transactional Sessions. Convenience {@link #getSession} * and {@link #releaseSession} methods are provided for that usage style. * * <p>This class will create its own HibernateTemplate instance if a SessionFactory * is passed in. The "allowCreate" flag on that HibernateTemplate will be "true" * by default. A custom HibernateTemplate instance can be used through overriding * {@link #createHibernateTemplate}. * * @author Juergen Hoeller * @since 1.2 * @see #setSessionFactory * @see #getHibernateTemplate * @see org.springframework.orm.hibernate3.HibernateTemplate */public abstract class HibernateDaoSupport extends DaoSupport {private HibernateTemplate hibernateTemplate;/** * Set the Hibernate SessionFactory to be used by this DAO. * Will automatically create a HibernateTemplate for the given SessionFactory. * @see #createHibernateTemplate * @see #setHibernateTemplate */public final void setSessionFactory(SessionFactory sessionFactory) {if (this.hibernateTemplate == null || sessionFactory != this.hibernateTemplate.getSessionFactory()) {this.hibernateTemplate = createHibernateTemplate(sessionFactory);}}/** * Create a HibernateTemplate for the given SessionFactory. * Only invoked if populating the DAO with a SessionFactory reference! * <p>Can be overridden in subclasses to provide a HibernateTemplate instance * with different configuration, or a custom HibernateTemplate subclass. * @param sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for * @return the new HibernateTemplate instance * @see #setSessionFactory */protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory) {return new HibernateTemplate(sessionFactory);}/** * Return the Hibernate SessionFactory used by this DAO. */public final SessionFactory getSessionFactory() {return (this.hibernateTemplate != null ? this.hibernateTemplate.getSessionFactory() : null);}/** * Set the HibernateTemplate for this DAO explicitly, * as an alternative to specifying a SessionFactory. * @see #setSessionFactory */public final void setHibernateTemplate(HibernateTemplate hibernateTemplate) {this.hibernateTemplate = hibernateTemplate;}/** * Return the HibernateTemplate for this DAO, * pre-initialized with the SessionFactory or set explicitly. * <p><b>Note: The returned HibernateTemplate is a shared instance.</b> * You may introspect its configuration, but not modify the configuration * (other than from within an {@link #initDao} implementation). * Consider creating a custom HibernateTemplate instance via * <code>new HibernateTemplate(getSessionFactory())</code>, in which * case you're allowed to customize the settings on the resulting instance. */public final HibernateTemplate getHibernateTemplate() { return this.hibernateTemplate;}protected final void checkDaoConfig() {if (this.hibernateTemplate == null) {throw new IllegalArgumentException("'sessionFactory' or 'hibernateTemplate' is required");}}/** * Obtain a Hibernate Session, either from the current transaction or * a new one. The latter is only allowed if the * {@link org.springframework.orm.hibernate3.HibernateTemplate#setAllowCreate "allowCreate"} * setting of this bean's {@link #setHibernateTemplate HibernateTemplate} is "true". * <p><b>Note that this is not meant to be invoked from HibernateTemplate code * but rather just in plain Hibernate code.</b> Either rely on a thread-bound * Session or use it in combination with {@link #releaseSession}. * <p>In general, it is recommended to use HibernateTemplate, either with * the provided convenience operations or with a custom HibernateCallback * that provides you with a Session to work on. HibernateTemplate will care * for all resource management and for proper exception conversion. * @return the Hibernate Session * @throws DataAccessResourceFailureException if the Session couldn't be created * @throws IllegalStateException if no thread-bound Session found and allowCreate=false * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean) */protected final Session getSession() throws DataAccessResourceFailureException, IllegalStateException {return getSession(this.hibernateTemplate.isAllowCreate());}/** * Obtain a Hibernate Session, either from the current transaction or * a new one. The latter is only allowed if "allowCreate" is true. * <p><b>Note that this is not meant to be invoked from HibernateTemplate code * but rather just in plain Hibernate code.</b> Either rely on a thread-bound * Session or use it in combination with {@link #releaseSession}. * <p>In general, it is recommended to use * {@link #getHibernateTemplate() HibernateTemplate}, either with * the provided convenience operations or with a custom * {@link org.springframework.orm.hibernate3.HibernateCallback} that * provides you with a Session to work on. HibernateTemplate will care * for all resource management and for proper exception conversion. * @param allowCreate if a non-transactional Session should be created when no * transactional Session can be found for the current thread * @return the Hibernate Session * @throws DataAccessResourceFailureException if the Session couldn't be created * @throws IllegalStateException if no thread-bound Session found and allowCreate=false * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory, boolean) */protected final Session getSession(boolean allowCreate) throws DataAccessResourceFailureException, IllegalStateException {return (!allowCreate ? SessionFactoryUtils.getSession(getSessionFactory(), false) :SessionFactoryUtils.getSession(getSessionFactory(),this.hibernateTemplate.getEntityInterceptor(),this.hibernateTemplate.getJdbcExceptionTranslator()));}/** * Convert the given HibernateException to an appropriate exception from the * <code>org.springframework.dao</code> hierarchy. Will automatically detect * wrapped SQLExceptions and convert them accordingly. * <p>Delegates to the * {@link org.springframework.orm.hibernate3.HibernateTemplate#convertHibernateAccessException} * method of this DAO's HibernateTemplate. * <p>Typically used in plain Hibernate code, in combination with * {@link #getSession} and {@link #releaseSession}. * @param ex HibernateException that occured * @return the corresponding DataAccessException instance * @see org.springframework.orm.hibernate3.SessionFactoryUtils#convertHibernateAccessException */protected final DataAccessException convertHibernateAccessException(HibernateException ex) {return this.hibernateTemplate.convertHibernateAccessException(ex);}/** * Close the given Hibernate Session, created via this DAO's SessionFactory, * if it isn't bound to the thread (i.e. isn't a transactional Session). * <p>Typically used in plain Hibernate code, in combination with * {@link #getSession} and {@link #convertHibernateAccessException}. * @param session the Session to close * @see org.springframework.orm.hibernate3.SessionFactoryUtils#releaseSession */protected final void releaseSession(Session session) {SessionFactoryUtils.releaseSession(session, getSessionFactory());}}
当然还可以继续深入看HibernateTemplate 这个类的源代码~不过那对于现在的我没什么意义了~。
至此,Spring为何将sessionFactory注入继承了继承了HibernateDaoSupport类,和如何注入,都解决了
二、HQL
HibernateDaoSupport 操作数据库的方式基本就是以上的,和HQL这两种了,
对象操作存在很大的局限性~~HQL是很方便的,不过语法不同于普通sql而已
//-------------------------------------------------------------------------// Convenience finder methods for HQL strings//-------------------------------------------------------------------------public List find(String queryString) throws DataAccessException {return find(queryString, (Object[]) null);}
今天就先到这了~
- Hibernate 对象操作 和 HQL
- Hibernate中的主要类别和HQL操作
- Hibernate中Hql操作
- Hibernate(hql语句操作)
- HibernateのHQL操作
- hibernate 实体对象有三种状态和HQL语句
- Hibernate的HQL和原生SQL返回简单对象
- hibernate中hql分页操作
- 记录Hibernate的hql操作
- Hibernate关系映射和HQL
- Hibernate关系映射和HQL
- Hibernate之HQL和QBC
- Hibernate二级缓存相关和HQL
- Hibernate中HQL和QBC
- hibernate 使用 HQL 和 SQL
- Hibernate HQL之动态构造实例对象
- Hibernate HQL 返回非对象集合
- 【Hibernate】Hibernate 操作对象
- 函数对象
- 源码安装MYSQL 5.5+版本全攻略
- Web服务
- 好用的工具函数
- oracle 中触发器,commit 的问题
- Hibernate 对象操作 和 HQL
- CDC *pDC HDC
- Android OpenGL ES 分析与实践
- volatile关键字的意义和应用
- usb 分析(1)
- Linux系统使用Tips
- 字节长度,字节对齐以及类,对象的长度
- java输入问题
- bootloader的编写步骤