- 浏览: 22020 次
- 性别:
- 来自: 北京
文章分类
最新评论
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 version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 省略了无关内容 --> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> </session-factory> </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()方法加载的对象
/** * Return the persistent instance of the given entity class with the given identifier, * assuming that the instance exists. This method might return a proxied instance that * is initialized on-demand, when a non-identifier method is accessed. * <br><br> * You should not use this method to determine if an instance exists (use <tt>get()</tt> * instead). Use this only to retrieve an instance that you assume exists, where non-existence * would be an actual error. * * @param entityName a persistent class * @param id a valid identifier of an existing persistent instance of the class * @return the persistent instance or proxy * @throws HibernateException */ 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)。你也可以创建自己的值类型。要注意值类型集合与实体类型集合在映射上的区别。
<set name="participants" table="PERSON_EVENT" inverse="true"> <key column="EVENT_ID"/> <many-to-many column="PERSON_ID" class="Person"/> </set>
映射文件中,inverse属性是用来干什么的呢?对于你,对于Java,一个双向连接就是一个简单的双方的引用。但是对Hibernate而言,它并没有足够的信息来正确组织SQL INSERT和UPDATE语句(从而避免约束冲突)。使某一侧的关联inverse,会告诉Hibernate将其看作是另一侧的镜像。这就是Hibernate为了解决从一个关系模型到数据库模式转换过程中出现的问题所需要的全部信息。
1.3. Part 3 - The EventManager web application
在一个request中使用一个Hibernate Session。而不是每一个数据库操作都使用一个Session。
the session-per-request pattern. Instead of the transaction demarcation code in every servlet, you could also write a servlet filter. See the Hibernate website and Wiki for more information about this pattern called Open Session in View. You will need it as soon as you consider rendering your view in JSP, not in a servlet.
发表评论
-
《Struts 2 in Action》读书笔记——part 2——核心概念之OGNL和类型转换
2013-02-04 17:00 1549第五章 数据转移:OGNL和类型转换 5.1 数据转移和 ... -
《Struts 2 in Action》读书笔记——part 2——核心概念之拦截器
2013-02-02 16:25 1115第四章 使用拦截器追加工作流 4.1 为什么要拦截 ... -
《Struts 2 in Action》读书笔记——part 2——核心概念之Action
2013-01-20 18:36 586第三章 使用Struts 2 Action3.1 Stru ... -
《Struts 2 in Action》读书笔记——part 1——Struts 2:一个全新的框架
2013-01-18 12:32 898第一章Struts 2 :现代Web框架1.1 Web应 ... -
Hibernate Core Reference Manual学习笔记——Chapter 4. Persistent Classes
2013-01-14 20:19 1024Chapter 4. Persistent ClassesT ... -
Hibernate Core Reference Manual学习笔记——Chapter 3. Configuration
2013-01-01 15:34 918Hibernate Core Reference Manual ... -
Hibernate Core Reference Manual学习笔记——Chapter 2. Architecture
2012-12-25 15:49 1249Hibernate Core Reference Manual ... -
JMX JNDI JTA
2012-12-23 08:02 0JMX(Java Management Extension ... -
Bean named 'tmeMerchandiseInfoDAO' must be of type [com.lyg.oss.dao.TmeMerchandi
2012-12-12 15:14 0今天自己搭建框架的时候,出了这个问题,出现这个异常,sp ... -
classpath
2012-12-08 11:13 0User Liberary加到Eclipse中,只是ec ... -
JAVA持久化框架选择:EJB?JPA?Hibernate?TopLink?
2012-12-07 15:23 1634采用 Java 持久化框架:选择、时机和优缺点?这篇文章比较了 ... -
JDBC基础知识
2012-12-04 17:12 1214一、概念JDBC(Java Data Ba ...
相关推荐
Title: C++ Language Tutorial For Beginner: Learn C++ in 7 days Author: Sharam Hekmat Length: 282 pages Edition: 1 Language: English Publication Date: 2015-05-23 ISBN-10: B00Y5V7MHE C++ (pronounced ...
package tutorial; service Greeter { rpc SayHello (HelloRequest) returns (HelloResponse) {} } message HelloRequest { string name = 1; } message HelloResponse { string message = 1; } ``` 3. 生成...
《STL Tutorial and Reference Guide》这本书将引导你深入探索这些概念,通过实例和详细解释,帮助你更好地理解和应用STL。无论你是初学者还是经验丰富的开发者,这本书都将是你提升C++编程技能的宝贵资源。通过系统...
你可以通过在线资源如《Python编程:从入门到实践》(http://c.biancheng.net/python/)或Runoob的Python3教程(https://www.runoob.com/python3/python3-tutorial.html)来学习Python的基础知识,包括变量、数据类型、...
Addison.Wesley.C++.Standard Library,The.A.Tutorial.and.Reference
《C++.Standard Library The.A.Tutorial.and.Reference》是一本深入探讨C++标准模板库(STL)的权威指南。STL是C++编程中的核心部分,它提供了高效、可重用且模式化的容器、迭代器、算法和函数对象,极大地提高了...
Chapter 5 Core Classes Chapter 6 Inheritance Chapter 7 Error Handling Chapter 8 Numbers and Dates Chapter 9 Interfaces and Abstract Classes Chapter 10 Enums Chapter 11 The Collections Framework ...
### C Reference Manual 知识点解析 #### 一、引言与背景介绍 - **C 语言起源**:C 语言基于早期的 B 语言发展而来。与 B 语言相比,C 语言引入了类型的概念,并定义了相应的语法和语义。 - **编译器差异**:在 ...
The C++ Standard Library: A Tutorial and Reference, Second Edition, describes this library as now incorporated into the new ANSI/ISO C++ language standard (C++11). The book provides comprehensive ...
Each chapter is based on the contents of the previous ones, introducing as few new concepts as possible. It is recommended that the book is read in linear fashion, without skipping chapters if ...
PP.Telerik.WPF.Controls.Tutorial.Feb.2014
在压缩包的文件列表中,"OpenGL.Superbible.Comprehensive.Tutorial.and.Reference.7th.Edition.0672337479"很可能是一个PDF电子书文件,包含了书籍的全部内容。这个文件可能包含了详细的章节讲解、示例代码的解释、...
An iOS 8 Graphics Tutorial using Core Graphics and Core Image Chapter 60. Basic iOS 8 Animation using Core Animation Chapter 61. iOS 8 UIKit Dynamics – An Overview Chapter 62. An iOS 8 UIKit ...
MySQL.Tutorial.eBook
Blender.Tutorial.Guide
Chapter 1. Start Here Chapter 2. Joining the Apple Developer Program Chapter 3. Installing Xcode 7 and the iOS 9 SDK Chapter 4. A Guided Tour of Xcode 7 Chapter 5. An Introduction to Xcode 7 ...
Chapter 1. Overview Chapter 2. Environment Setup Chapter 3. Architecture Chapter 4. Application Components Chapter 5. Hello World Example Chapter 6. Resources Organizing & Accessing Chapter 7. ...
The C++ standard library provides a set of common classes and interfaces that greatly extend the core C++ language. The library, however, is not self-explanatory. To make full use of its components - ...