Spring中Hibernate配置笔记

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.



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值