zoukankan      html  css  js  c++  java
  • hibernate重要知识点总结

    一、使用注解方式-----实体和表之间的映射

    配置spring的applicationContext.xml文件:

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
      <property name="packagesToScan" value="cn.com.entry*"/>
      <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>
    实体类的注解:

       1、注解实体与表的映射,在定义类的上方

            @Entity
            @Table
            @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)//这个是二级缓存的注解

      2、注解主键,在定义属性的上方

            @Id
            @GenericGenerator(name="inc50",strategy="increment")
            @GeneratedValue(generator="inc50")

    二、开启二级缓存

    配置hibernate.cfg.xml文件

    <property name="hibernate.cache.use_second_level_cache">true</property>
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

    实体类注解

    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)//这个是二级缓存的注解

    三、连接池的配置

    配置hibernate.cfg.xml文件

    <property name="hibernate.c3p0.min_size">8</property>
    <property name="hibernate.c3p0.max_size">10</property>
    <property name="hibernate.c3p0.timeout">5000</property>
    <property name="hibernate.c3p0.max_statements">10</property>
    <property name="hibernate.c3p0.idle_test_period">30000</property>
    <property name="hibernate.c3p0.acquire_increment">2</property>
    <property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>

    四、事务管理

    配置spring的applicationContext.xml文件:

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                  http://www.springframework.org/schema/tx
                  http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
      <property name="sessionFactory">
        <ref bean="sessionFactory"/>
      </property>
    </bean>
    <!-- 注解方式 -->
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    直接在要使用事务的方法上面或者类的上面注解,如下:

    @Transactional(rollbackFor = Exception.class)//在类的上方注解表示整个类中的所有方法都使用事务管理。

    注意:使用事务管理的方法中绝对不能catch异常,应该往外抛出异常,不然事务管理将失去作用

  • 相关阅读:
    Binary Tree Inorder Traversal
    Populating Next Right Pointers in Each Node
    Minimum Depth of Binary Tree
    Majority Element
    Excel Sheet Column Number
    Reverse Bits
    Happy Number
    House Robber
    Remove Linked List Elements
    Contains Duplicate
  • 原文地址:https://www.cnblogs.com/wbjgogogo/p/7552268.html
Copyright © 2011-2022 走看看