Spring integrates hibernate with Hibernate.cfg.xml

Source: Internet
Author: User

Spring integration hibernate actually encapsulates Hibernate's Sessionfactory object as: Org.springframework.orm.hibernate3.LocalSessionFactoryBeanTo be kept and controlled by oneself. When configuring the Localsessionfactorybean, if you want to use the Hibernate.cfg.xml configuration file, then you need to configure:The Configlocations property, which is called Spring to Configlocations call this configuration when configuring Localsessionfactorybean.
  
 
  1. <bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  2. <!-- 使用hibernate.cfg.xml配置文件 -->
  3. <property name="configLocations">
  4. <value>classpath:hibernate.cfg.xml</value>
  5. </property>
  6. ....
  7. </bean>
If this attribute is not configured, the Hibernate.cfg.xml configuration file will be ignored. That means no effect.

Because spring's applicationcontext.xml file can directly configure everything in the Hibernate.cfg.xml, you do not need to configure Hibernate.cfg.xml. For example:
    • Configure additional configuration of the database in Hibernate.cfg.xml:
  
 
  1. <!-- 设置数据库方言 -->
  2. <property name="connection.dialect">
  3. org.hibernate.dialect.MySQLDialect
  4. </property>
  5. <property name="show_sql">true</property>
  6. <property name="hbm2ddl.auto">update</property>
    • Configure additional configuration of the database in spring's Applicationcontext.xml file:
  
 
  1. <!-- 相当于hibernate.cfg.xml配置的信息 -->
  2. <property name="hibernateProperties">
  3. <props>
  4. <prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
  5. <prop key="hibernate.show_sql">true</prop>
  6. <prop key="hibernate.hbm2ddl.auto">update</prop>
  7. </props>
  8. </property>
#############################################################################
    • To configure the database connection pool in Hibernate.cfg.xml:
  
 
  1. <!-- 设置数据库驱动 -->
  2. <property name="connection.driver_class">com.mysql.jdbc.driver</property>
  3. <!-- 设置数据库链接地址 -->
  4. <property name="connection.url">jdbc:mysql://localhost:3306/oa</property>
  5. <!-- 设置数据库账户 -->
  6. <property name="connection.username">root</property>
  7. <!-- 设置数据库密码 -->
  8. <property name="connection.password"></property>
  9. ......
    • Configure the database connection pool in spring's Applicationcontext.xml file:
  
 
  1. <!-- 配置数据链接池 -->
  2. <property name="dataSource">
  3. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  4. <!-- 从jdbc.properties配置文件中获取值 -->
  5. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/oa"></property>
  6. <!-- 从jdbc.properties配置文件中获取值 -->
  7. <property name="driverClass" value="com.mysql.jdbc.driver"></property>
  8. <!-- 从jdbc.properties配置文件中获取值 -->
  9. <property name="user" value="root"></property>
  10. <!-- 从jdbc.properties配置文件中获取值 -->
  11. <property name="password" value=""></property>
  12. <!-- 其它配置 -->
  13. <!-- 初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default:3 -->
  14. <property name="initialPoolSize" value="3"></property>
  15. .....
  16. </property>

    • To configure a mapping file for an entity object in Hibernate.cfg.xml:
  
 
  1. 在hibernate.cfg.xml中配置实体对象的映射文件:
  2. <mapping resource="cn/czk/oa/domain/Department.hbm.xml" />
  3. <mapping resource="cn/czk/oa/domain/Privilege.hbm.xml" />
  4. <mapping resource="cn/czk/oa/domain/Role.hbm.xml" />
  5. <mapping resource="cn/czk/oa/domain/User.hbm.xml" />
    • To Configure a mapping file for an entity object in spring applicationcontext.xml
  
 
  1. <bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  2. .....
  3. <!-- 配置映射文件 -->
  4. <property name="mappingResources">
  5. <list>
  6. <value>cn/czk/oa/domain/User.hbm.xml</value>
  7. <value>cn/czk/oa/domain/Department.hbm.xml</value>
  8. <value>cn/czk/oa/domain/Role.hbm.xml</value>
  9. <value>cn/czk/oa/domain/Privilege.hbm.xml</value>
  10. </list>
  11. </property>
  12. .....
  13. </bean>




From for notes (Wiz)

Spring integrates hibernate with Hibernate.cfg.xml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.