hibernate中的几个问题

对象的3中状态

transient: 没ID,刚new出来的对象(内存中的一个对象,缓存没有,数据库没有)
detached:脱管状态,session close之后,内存的hashmap就没了,数据库有,有id,缓存没有,脱离session的管理就变成detached状态
persistent:save之后就是persistent,数据库有,内存有,缓存有,有id

get和load的区别

hibernate中获取某一个对象可以通过get或load该对象的具体ID来获取,但是使用get方法的时候,是直接发sql语句从数据库中查找到该对象,而load则是生成的代理对象proxy

只有当你真正用到teacher对象的某一个属性的时候才会发出sql语句到数据库查找该对象。当这个对象不存在的时候,get会发出NullPointerException,而load则是

ObjectNotFoundException. 并且如果在session关闭之后拿teacher的属性, get是正常的,因为已经从数据库中拿出来了,而load则会报no Session的错误。

     SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();        
     Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Teacher t = (Teacher)session.load(Teacher.class, 1); //Teacher t1 = (Teacher)session.load(Teacher.class, 1); session.getTransaction().commit(); System.out.println(t.getClass()); //System.out.println(t.getName());


1+N问题

比如有两个类,Category和Topic, 一个分类里有很多个主题,当有很多歌topic时,每向数据库拿一条topic,就会同时发出1条查询category语句,当topic很多的时候,这样

就会产生发出N条查询category语句。解决方法有3种:

1.在manyToOne中设置fetch=FetchType.LAZY.

2.查询时使用表连接的方式查询.

List<Topic> topics = (List<Topic>)session.createCriteria(Topic.class).list();
//List<Topic> topics = (List<Topic>)session.createQuery("from Topic t left join fetch t.category c").list();

3.使用batchSize,通过一次取多条来减少查询次数。

@Entity
//@BatchSize(size=5)
//对于1+n问题,一次性取5条
public class Category {
    private int id;
    private String name;
    @Id
    @GeneratedValue
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

 getCurrentSession和openSession

getCurrentSession是从当前上下文中取session,如果有就用存在的,如果没有,就new一个新的session。当事务提交后,又getCurrentSession,这时就是新的session了

openSession永远都是直接打开一个新的session。

cascade和fetch

cascade表示级联,管理增删改,fetch管读取。

缓存问题

hibernate中有3种缓存。

缓存:数据量有限,改动不大,经常被访问的这些情况才需要使用缓存

session级别的缓存

public void testCache1() {
        Session session = sf.openSession();
        session.beginTransaction();
        Category c = (Category)session.load(Category.class, 1);
        System.out.println(c.getName());
        
        Category c2 = (Category)session.load(Category.class, 1);
        System.out.println(c2.getName());
        session.getTransaction().commit();
        session.close();
        
    }

sessionFactory级别的缓存,要使用二级缓存需要

1.在hibernate.cfg.xml配置

<property name="cache.use_second_level_cache">true</property>
        <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

2.使用ehcache.xml

<ehcache>

    <!-- Sets the path to the directory where cache .data files are created.

         If the path is a Java System Property it is replaced by
         its value in the running VM.

         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="java.io.tmpdir"/>


    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.

        The following attributes are required for defaultCache:

        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->
    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="1200"
        overflowToDisk="true"
        />

    <!--Predefined caches.  Add your cache configuration settings here.
        If you do not have a configuration for your cache a WARNING will be issued when the
        CacheManager starts

        The following attributes are required for defaultCache:

        name              - Sets the name of the cache. This is used to identify the cache. It must be unique.
        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.

        -->

    <!-- Sample cache named sampleCache1
        This cache contains a maximum in memory of 10000 elements, and will expire
        an element if it is idle for more than 5 minutes and lives for more than
        10 minutes.

        If there are more than 10000 elements it will overflow to the
        disk cache, which in this configuration will go to wherever java.io.tmp is
        defined on your system. On a standard Linux system this will be /tmp"
        -->
    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600"
        overflowToDisk="true"
        
        />

    <!-- Sample cache named sampleCache2
        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->
    <cache name="sampleCache2"
        maxElementsInMemory="1000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        /> -->

    <!-- Place configuration for your caches following -->

</ehcache>
    public void testCache2() {
        Session session = sf.openSession();
        session.beginTransaction();
        Category c = (Category)session.load(Category.class, 1);
        System.out.println(c.getName());
        
        
        session.getTransaction().commit();
        session.close();
        
        Session session2 = sf.openSession();
        session2.beginTransaction();
        Category c2 = (Category)session2.load(Category.class, 1);
        System.out.println(c2.getName());
        
        session2.getTransaction().commit();
        session2.close();
    }

查询缓存:只有当两次查询的条件一样时,查询缓存才有效。

public void testQueryCache() {
        Session session = sf.openSession();
        session.beginTransaction();
        List<Category> categories = (List<Category>)session.createQuery("from Category")
                                    .setCacheable(true).list();
        
        
        
        session.getTransaction().commit();
        session.close();
        
        Session session2 = sf.openSession();
        session2.beginTransaction();
        List<Category> categories2 = (List<Category>)session2.createQuery("from Category")
        .setCacheable(true).list();
        
        session2.getTransaction().commit();
        session2.close();
    }

 

posted @ 2014-05-18 21:58  lionZ  阅读(137)  评论(0)    收藏  举报