• Hibernate Core Reference Manual学习笔记——Chapter 1. Tutorial


    Hibernate Core Reference Manual

    Chapter 1. Tutorial

    Table of Contents

    1.1. Part 1 - The first Hibernate Application
    1.1.1. Setup
    1.1.2. The first class
    1.1.3. The mapping file
    1.1.4. Hibernate configuration
    1.1.5. Building with Maven
    1.1.6. Startup and helpers
    1.1.7. Loading and storing objects
    1.2. Part 2 - Mapping associations
    1.2.1. Mapping the Person class
    1.2.2. A unidirectional Set-based association
    1.2.3. Working the association
    1.2.4. Collection of values
    1.2.5. Bi-directional associations
    1.2.6. Working bi-directional links
    1.3. Part 3 - The EventManager web application
    1.3.1. Writing the basic servlet
    1.3.2. Processing and rendering
    1.3.3. Deploying and testing
    1.4. Summary


    1.1. Part 1 - The first Hibernate Application

    JavaBean类有getter和setter方法,是一种推荐使用的设计模式。但对Hibernate而言并不是必须的。Hibernate可以直接访问类的属性。Hibernate可以访问public、private和protected修饰的getter和setter方法,就像它可以直接访问public、private和protected修饰的属性。

    所有要被持久化的类都需要由无参的构造函数,因为Hibernate会使用反射来为你创建对象。构造函数可以是private的。however package or public visibility is required for runtime proxy generation and efficient data retrieval without bytecode instrumentation.

    Hibernate需要知道如何加载和保存持久化类的对象。这就是Hibernate配置文件的作用。配置文件告诉Hibernate去访问数据库中的哪张表,哪些列。

    在Hibernate配置文件里使用的type不是java数据类型,也不是SQL数据类型,而是Hibernate映射类型。转换器可以从java类型转换到SQL类型,也可以反过来。如果没有配置type,Hibernate会尝试确定正确的映射类型。在某些场合下,这种使用java反射技术的自动检测并不能得出你期望的结果。比如说java.util.Date类型,Hibernate不知道是应该映射到SQL的date、timestamp还是time类型。同时这种自动检测会耗费时间和资源,如果注重性能,最好明确的配置type属性。

    SessionFactory是一个全局的工厂,代表一个特定的数据库。如果你有好几个数据库,你应该在配置文件里配置多个SessionFactory。

    If you give the org.hibernate.SessionFactory a name in your configuration, Hibernate will try to bind it to JNDI under that name after it has been built. Another, better option is to use a JMX deployment and let the JMX-capable container instantiate and bind a HibernateService to JNDI. Such advanced options are discussed later.涉及到很多陌生的名词,先看概念吧。


    The getCurrentSession() method always returns the "current" unit of work.还记得我们在hibernate.cfg.xml文件中的配置吧

    Xml代码 复制代码 收藏代码
    1. <?xml version='1.0' encoding='utf-8'?>
    2. <!DOCTYPE hibernate-configuration PUBLIC
    3. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    4. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    5. <hibernate-configuration>
    6. <session-factory>
    7. <!-- 省略了无关内容 -->
    8. <!-- Enable Hibernate's automatic session context management -->
    9. <property name="current_session_context_class">thread</property>
    10. </session-factory>
    11. </hibernate-configuration>

    Hibernate会根据上面的配置把当前工作单元的上下文与当前的java线程绑定。


    当你第一次调用getCurrentSession()时,会生成一个 org.hibernate.Session,然后,Hibernate将Session绑定到当前线程。当事务结束时,不论是提交还是回滚,Hibernate会自动将Session与当前线程解除绑定,并关闭Session。如果你再一次调用getCurrentSession(),你会得到一个新的org.hibernate.Session,并可以开始一个新的工作单元。

    org.hibernate.Session的scope非常灵活,你不应该为每一个数据库操作都创建一个新的org.hibernate.Session。

    1.2. Part 2 - Mapping associations

    The design questions you have to deal with are: directionality, multiplicity, and collection behavior.

    Hibernate automatically detects that the Object has been modified and needs to be updated. This is called automatic dirty checking.只要对象还处在persistent状态,比如说通过session.load()方法加载的对象

    Java代码 复制代码 收藏代码
    1. /**
    2. * Return the persistent instance of the given entity class with the given identifier,
    3. * assuming that the instance exists. This method might return a proxied instance that
    4. * is initialized on-demand, when a non-identifier method is accessed.
    5. * <br><br>
    6. * You should not use this method to determine if an instance exists (use <tt>get()</tt>
    7. * instead). Use this only to retrieve an instance that you assume exists, where non-existence
    8. * would be an actual error.
    9. *
    10. * @param entityName a persistent class
    11. * @param id a valid identifier of an existing persistent instance of the class
    12. * @return the persistent instance or proxy
    13. * @throws HibernateException
    14. */
    15. public Object load(String entityName, Serializable id) throws HibernateException;

    也就是说对象还绑定在某个特定的 org.hibernate.Session上,Hibernate监测任何改变,并后台执行SQL。将内存和数据库进行同步的过程,通常发生在一个工作单元的最后(比如说commit或rollback),称为flushing。


    如果一个处于persistent状态的对象与 org.hibernate.Session断开绑定,这种状态称为detached。在detached状态所做的任何改变,可以通过update方法进行持久化。

    Hibernate把所有JDK类型看成值类型(value types)。你也可以创建自己的值类型。要注意值类型集合与实体类型集合在映射上的区别。

  • 相关阅读:
    【记录】JS正则表达式(学习笔记2)现学现卖还帮美女解决了个问题。
    【分享】封装获取dom元素那些讨厌的位置
    【记录】简单去除数组重复项
    【记录】JS 预加载图片
    【分享】详解js函数调用的4中方法!
    【记录】JavaScript版 快速排序,还请高手指教。
    【分享】封装window.showModalDialog让他更好用(弹出窗口)
    【记录】正则表达式学习第3天(正则学习笔记),又解决了个实际问题。
    【分享】LazyLoad延迟加载(按需加载)
    PPT幻灯片放映不显示备注,只让备注显示在自己屏幕上
  • 原文地址:https://www.cnblogs.com/bjanzhuo/p/3575974.html
Copyright © 2020-2023  润新知