关闭

Spring中Hibernate配置笔记

标签: hibernate
292人阅读 评论(1) 收藏 举报
分类:

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27893次
    • 积分:159
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:20篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论