没有合适的资源?快使用搜索试试~ 我知道了~
hibernate annotations详解
需积分: 10 48 下载量 145 浏览量
2009-04-16
11:57:50
上传
评论
收藏 251KB PDF 举报
温馨提示
试读
60页
详细介绍了hibernate annotations的使用,非常不错的一本书,不敢独享,只能拿出来了。
资源推荐
资源详情
资源评论
Hibernate Annotations
Reference Guide
Version: 3.4.0.GA
Table of Contents
Preface ............................................................................................................................................iv
1. Setting up an annotations project ................................................................................................1
1.1. Requirements .....................................................................................................................1
1.2. Configuration .....................................................................................................................1
1.3. Properties ..........................................................................................................................2
1.4. Logging ............................................................................................................................. 3
2. Entity Beans ................................................................................................................................ 4
2.1. Intro ..................................................................................................................................4
2.2. Mapping with EJB3/JPA Annotations .................................................................................. 4
2.2.1. Declaring an entity bean ..........................................................................................4
2.2.1.1. Defining the table .........................................................................................4
2.2.1.2. Versioning for optimistic locking ...................................................................5
2.2.2. Mapping simple properties .......................................................................................5
2.2.2.1. Declaring basic property mappings ................................................................ 5
2.2.2.2. Declaring column attributes ........................................................................... 7
2.2.2.3. Embedded objects (aka components) ..............................................................7
2.2.2.4. Non-annotated property defaults .................................................................... 9
2.2.. Mapping identifier properties .....................................................................................9
2.2.4. Mapping inheritance .............................................................................................. 12
2.2.4.1. Table per class ............................................................................................ 13
2.2.4.2. Single table per class hierarchy .................................................................... 13
2.2.4.3. Joined subclasses ........................................................................................ 13
2.2.4.4. Inherit properties from superclasses ............................................................. 14
2.2.5. Mapping entity bean associations/relationships ........................................................ 15
2.2.5.1. One-to-one ................................................................................................. 15
2.2.5.2. Many-to-one ............................................................................................... 17
2.2.5.3. Collections ................................................................................................. 18
2.2.5.4. Transitive persistence with cascading ........................................................... 23
2.2.5.5. Association fetching ................................................................................... 24
2.2.6. Mapping composite primary and foreign keys ......................................................... 24
2.2.7. Mapping secondary tables ...................................................................................... 25
2.3. Mapping Queries .............................................................................................................. 26
2.3.Mapping JPAQL/HQL queries. Mapping JPAQL/HQL queries .................................... 26
2.3.2. Mapping native queries .......................................................................................... 27
2.4. Hibernate Annotation Extensions ...................................................................................... 30
2.4.1. Entity ................................................................................................................... 30
2.4.Identifier. Identifier ................................................................................................... 32
2.4.Identifier.1. Generators ...................................................................................... 32
2.4.Identifier.2. @NaturalId .................................................................................... 33
2.4.3. Property ................................................................................................................ 33
2.4.3.1. Access type ................................................................................................ 33
2.4.3.2. Formula ..................................................................................................... 35
2.4.3.3. Type .......................................................................................................... 35
2.4.3.4. Index ......................................................................................................... 36
2.4.3.5. @Parent ..................................................................................................... 36
2.4.3.6. Generated properties ................................................................................... 36
2.4.3.7. @Target ..................................................................................................... 36
2.4.3.8. Optimistic lock ........................................................................................... 37
Hibernate 3.4.0.GA ii
2.4.4. Inheritance ............................................................................................................ 37
2.4.5. Single Association related annotations .................................................................... 37
2.4.5.1. Lazy options and fetching modes ................................................................. 38
2.4.5.2. @Any ........................................................................................................ 39
2.4.6. Collection related annotations ................................................................................ 39
2.4.6.1. Enhance collection settings .......................................................................... 40
2.4.6.2. Extra collection types .................................................................................. 40
2.4.7. Cascade ................................................................................................................ 45
2.4.8. Cache ................................................................................................................... 45
2.4.9. Filters ................................................................................................................... 46
2.4.10. Queries ............................................................................................................... 46
2.4.11. Custom SQL for CRUD operations ....................................................................... 47
2.4.12. Tuplizer .............................................................................................................. 48
Overriding metadata through XML. Overriding metadata through XML .................................... 50
Overriding metadata through XML.1. Principles ....................................................................... 50
Overriding metadata through XML.1.1. Global level metadata .......................................... 50
Overriding metadata through XML.1.2. Entity level metadata ........................................... 50
Overriding metadata through XML.1.3. Property level metadata ........................................ 53
Overriding metadata through XML.1.4. Association level metadata ................................... 53
4. Additional modules ................................................................................................................... 55
4.1. Hibernate Validator .......................................................................................................... 55
4.1.1. Description ........................................................................................................... 55
4.1.2. Integration with Hibernate Annotations ................................................................... 55
4.2. Hibernate Search .............................................................................................................. 56
4.2.1. Description ........................................................................................................... 56
4.2.2. Integration with Hibernate Annotations ................................................................... 56
Hibernate Annotations
Hibernate 3.4.0.GA iii
Preface
Hibernate, like all other object/relational mapping tools, requires metadata that governs the transformation of
data from one representation to the other. In Hibernate 2.x mapping metadata is most of the time declared in
XML text files. Alternatively XDoclet can be used utilizing Javadoc source code annotations together with a
compile time preprocessor.
The same kind of annotation support is now available in the standard JDK, although more powerful and with
better tools support. IntelliJ IDEA and Eclipse for example, support auto-completion and syntax highlighting of
JDK 5.0 annotations which are compiled into the bytecode and read at runtime using reflection. No external
XML files are needed.
The EJB3 specification recognizes the interest and the success of the transparent object/relational mapping
paradigm. It standardizes the basic APIs and the metadata needed for any object/relational persistence mechan-
ism. Hibernate EntityManager implements the programming interfaces and lifecycle rules as defined by the
EJB3 persistence specification and together with Hibernate Annotations offers a complete (and standalone)
EJB3 persistence solution on top of the mature Hibernate core. You may use a combination of all three togeth-
er, annotations without EJB3 programming interfaces and lifecycle, or even pure native Hibernate, depending
on the business and technical needs of your project. At all times you cann fall back to Hibernate native APIs, or
if required, even to native JDBC and SQL.
This release of Hibernate Annotations is based on the final release of the EJB 3.0 / JPA specification (aka JSR-
220 [http://jcp.org/en/jsr/detail?id=220]) and supports all its features (including the optional ones). Hibernate
specific features and extensions are also available through unstandardized, Hibernate specific annotations.
While the Hibernate feature coverage is high, some can not yet be expressed via annotations. The eventual goal
is to cover all of them. See the JIRA road map section for more informations.
If you are moving from previous Hibernate Annotations versions, please have a look at Java Persistence migra-
tion guide [http://www.hibernate.org/398.html].
Hibernate 3.4.0.GA iv
Chapter 1. Setting up an annotations project
1.1. Requirements
• Download [http://www.hibernate.org/6.html] and unpack the Hibernate Annotations distribution from the
Hibernate website.
• This release requires Hibernate Core 3.3 and above.
• This release is known to work on Hibernate Core 3.3.0.SP1
• Make sure you have JDK 5.0 installed or above. You can of course continue using XDoclet and get some of
the benefits of annotation-based metadata with older JDK versions. Note that this document only describes
JDK 5.0 annotations and you have to refer to the XDoclet documentation for more information.
1.2. Configuration
First, set up your classpath (after you have created a new project in your favorite IDE):
• Copy all Hibernate3 core and required 3rd party library files (see lib/README.txt in Hibernate).
• Copy hibernate-annotations.jar, lib/hibernate-comons-annotations.jar and lib/
ejb3-persistence.jar from the Hibernate Annotations distribution to your classpath as well.
If you wish to use Hibernate Validator [http://validator.hibernate.org], download it from the Hibernate website
and add hibernate-validator.jar in your classpath.
If you wish to use Hibernate Search [http://search.hibernate.org], download it from the Hibernate website and
add hibernate-search.jar and lucene-core-x.y.z.jar in your classpath.
We also recommend a small wrapper class to startup Hibernate in a static initializer block, known as Hibern-
ateUtil. You might have seen this class in various forms in other areas of the Hibernate documentation. For
Annotation support you have to enhance this helper class as follows:
package hello;
import org.hibernate.*;
import org.hibernate.cfg.*;
import test.*;
import test.animals.Dog;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration()
configure().buildSessionFactory();
} catch (Throwable ex) {
// Log exception!
throw new ExceptionInInitializerError(ex);
}
}
Hibernate 3.4.0.GA 1
剩余59页未读,继续阅读
资源评论
wuyaowen2000
- 粉丝: 11
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功