Spring中Hibernate配置笔记

原创 2015年07月06日 19:14:12

ApplicatonContext.xml中的配置:

 	<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/global/ds" />
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
			<property name="configLocation">
				<value>classpath:hibernate/hibernate.cfg.xml</value>	
			</property>
			<property name="dataSource">
				<ref bean="dataSource" />
			</property>
	</bean>
	
	<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    	<property name="dataSource">
    		<ref bean="dataSource" />
    	</property>    	
    	<property name="sessionFactory">
    		<ref bean="sessionFactory" />
    	</property>
    </bean>
 	
	<bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
		   <property name="transactionManager" ref="hibernateTransactionManager"/>
		   <property name="transactionAttributes">
		     <props>
		     	<prop key="insert*">PROPAGATION_REQUIRED</prop>
		     	<prop key="save*">PROPAGATION_REQUIRED</prop>
		     	<prop key="update*">PROPAGATION_REQUIRED</prop>
		     	<prop key="delete*">PROPAGATION_REQUIRED</prop>
		        <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
		     </props>
		   </property>
	</bean>
	
	<bean id="myHibernateManage" parent="transactionProxy">
		<property name="target">
			<bean id="hibernateManage" class="com.web.dao.hibernate.MyHibernateManage">
				<property name="sessionFactory">
					<ref bean="sessionFactory" />
				</property>
			</bean>
		</property>
		<property name="proxyTargetClass">
			<value>true</value>
		</property>		
	</bean>

主要Hibernate操作对象Session

还需hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
         <!-- 
        <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/test</property>
        <property name="hibernate.connection.username">test</property>
        <property name="hibernate.connection.password">test</property>
		 -->
        <!-- JDBC connection pool (use the built-in) -->
        <!-- 
        <property name="hibernate.connection.pool_size">5</property>
		 -->
        <!-- SQL dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="hibernate.current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
		<!-- 
		<property name="hibernate.cache.use_query_cache">true</property>
		-->
        <!-- Echo all executed SQL to stdout -->
        <property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hibernate.generate_statistics">true</property>
		<!-- Mapping -->
        <mapping resource="com/web/dao/hibernate/entity/TestTable.hbm.xml"/>
				
    </session-factory>

</hibernate-configuration>

具体配置说明参考hibernate API,configuration章节

映射文件配置也参考hibernate API,Persistence Object章节

HQL语法提要:

14.6. The select clause

The select clause picks which objects and properties to return in the query result set. Consider:

select mate
from Cat as cat
    inner join cat.mate as mate

The query will select mates of other Cats. Actually, you may express this query more compactly as:

select cat.mate from Cat cat

Queries may return properties of any value type including properties of component type:

select cat.name from DomesticCat cat
where cat.name like 'fri%'
select cust.name.firstName from Customer as cust

Queries may return multiple objects and/or properties as an array of type Object[],

select mother, offspr, mate.name
from DomesticCat as mother
    inner join mother.mate as mate
    left outer join mother.kittens as offspr

or as a List,

select new list(mother, offspr, mate.name)
from DomesticCat as mother
    inner join mother.mate as mate
    left outer join mother.kittens as offspr

or as an actual typesafe Java object,

select new Family(mother, mate, offspr)
from DomesticCat as mother
    join mother.mate as mate
    left join mother.kittens as offspr

assuming that the class Family has an appropriate constructor.

You may assign aliases to selected expressions using as:

select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat

This is most useful when used together with select new map:

select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
from Cat cat

This query returns a Map from aliases to selected values.



SpringMVC+Spring+hibernate配置

  • 2015年03月30日 21:38
  • 11.09MB
  • 下载

spring和hibernate配置的例子

  • 2009年05月29日 20:54
  • 5.76MB
  • 下载

使用Spring集成Hibernate配置,根据实体类自动创建表

applicationContext.xml xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://w...

为Spring集成的Hibernate配置二级缓存

  • 2016年10月23日 00:31
  • 17.05MB
  • 下载

Struts+Spring+Hibernate配置用包.part2.rar

  • 2008年08月01日 12:30
  • 2.58MB
  • 下载

Spring管理Struts与Hibernate配置文件!

xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/XMLSchema...

struts+spring+hibernate配置

  • 2011年03月18日 11:38
  • 774B
  • 下载

没用的Struts+spring+Hibernate配置

  • 2009年03月16日 08:47
  • 253KB
  • 下载

Spring+Hibernate配置多数据源

 配置说明         在实际应用中,经常会用到读写分离,这里就这种情况进行Spring+Hibernate的多数据源配置。此处的配置只是让读的方法操作一个数据库,写的方法操作另外一个数据库。  ...
  • leadergg
  • leadergg
  • 2011年04月15日 09:18
  • 19035

Spring结合Hibernate配置C3P0数据源

WEB-INF/config/mapping/base/ WEB-INF/config/mapping/jbpm/ org.hibernate.dialect.Orac...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Spring中Hibernate配置笔记
举报原因:
原因补充:

(最多只允许输入30个字)