JBoss Enterprise Application Platform JBoss Enterprise Application Platform 5 Hibernate Annotations

  • View
    13

  • Download
    0

Embed Size (px)

Text of JBoss Enterprise Application Platform JBoss Enterprise Application Platform 5 Hibernate Annotations

  • JBoss Enterprise Application Platform 5

    Hibernate Annotations Reference Guide

    for Use with JBoss Enterprise Application Platform 5 Edition 5.2.0

    Last Updated: 2017-10-13

  • JBoss Enterprise Application Platform 5 Hibernate Annotations Reference Guide

    for Use with JBoss Enterprise Application Platform 5 Edition 5.2.0

    Eva Kopalova

    Petr Penicka

    Russell Dickenson

    Scott Mumford

  • Legal Notice

    Copyright © 2012 Red Hat, Inc.

    This document is licensed by Red Hat under the Creative Commons Attribution-ShareAlike 3.0 Unported License. If you distribute this document, or a modified version of it, you must provide attribution to Red Hat, Inc. and provide a link to the original. If the document is modified, all Red Hat trademarks must be removed.

    Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.

    Red Hat, Red Hat Enterprise Linux, the Shadowman logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.

    Linux ® is the registered trademark of Linus Torvalds in the United States and other countries.

    Java ® is a registered trademark of Oracle and/or its affiliates.

    XFS ® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.

    MySQL ® is a registered trademark of MySQL AB in the United States, the European Union and other countries.

    Node.js ® is an official trademark of Joyent. Red Hat Software Collections is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.

    The OpenStack ® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.

    All other trademarks are the property of their respective owners.

    Abstract

    The Hibernate Annotations Reference Guide for JBoss Enterprise Application Platform 5 and its patch releases.

    http://creativecommons.org/licenses/by-sa/3.0/

  • . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    Table of Contents

    CHAPTER 1. SETTING UP AN ANNOTATIONS PROJECT 1.1. REQUIREMENTS 1.2. CONFIGURATION 1.3. PROPERTIES 1.4. LOGGING

    CHAPTER 2. ENTITY BEANS 2.1. INTRO 2.2. MAPPING WITH EJB3/JPA ANNOTATIONS

    2.2.1. Declaring an entity bean 2.2.1.1. Defining the table 2.2.1.2. Versioning for optimistic locking

    2.2.2. Mapping simple properties 2.2.2.1. Declaring basic property mappings 2.2.2.2. Declaring column attributes 2.2.2.3. Embedded objects (aka components) 2.2.2.4. Non-annotated property defaults

    2.2.3. Mapping identifier properties 2.2.4. Mapping inheritance

    2.2.4.1. Table per class 2.2.4.2. Single table per class hierarchy 2.2.4.3. Joined subclasses 2.2.4.4. Inherit properties from superclasses

    2.2.5. Mapping entity bean associations/relationships 2.2.5.1. One-to-one 2.2.5.2. Many-to-one 2.2.5.3. Collections

    2.2.5.3.1. Overview 2.2.5.3.2. One-to-many 2.2.5.3.3. Many-to-many

    2.2.5.4. Transitive persistence with cascading 2.2.5.5. Association fetching

    2.2.6. Mapping composite primary and foreign keys 2.2.7. Mapping secondary tables

    2.3. MAPPING QUERIES 2.3.Mapping JPAQL/HQL queries. Mapping JPAQL/HQL queries 2.3.2. Mapping native queries

    2.4. HIBERNATE ANNOTATION EXTENSIONS 2.4.1. Entity 2.4.Identifier. Identifier

    2.4.Identifier.1. Generators 2.4.Identifier.2. @NaturalId

    2.4.3. Property 2.4.3.1. Access type 2.4.3.2. Formula 2.4.3.3. Type 2.4.3.4. Index 2.4.3.5. @Parent 2.4.3.6. Generated properties 2.4.3.7. @Target 2.4.3.8. Optimistic lock

    4 4 4 5 6

    7 7 7 7 7 8 8 8

    10 11 13 13 16 17 17 18 18 20 20 21 22 23 25 27 29 29 30 31 32 32 33 37 37 39 39 40 40 40 42 42 43 43 44 44 44

    Table of Contents

    1

  • . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    2.4.4. Inheritance 2.4.5. Single Association related annotations

    2.4.5.1. Lazy options and fetching modes 2.4.5.2. @Any

    2.4.6. Collection related annotations 2.4.6.1. Enhance collection settings 2.4.6.2. Extra collection types

    2.4.6.2.1. List 2.4.6.2.2. Map 2.4.6.2.3. Bidirectional association with indexed collections 2.4.6.2.4. Bag with primary key 2.4.6.2.5. Collection of element or composite elements 2.4.6.2.6. @ManyToAny

    2.4.7. Cascade 2.4.8. Cache 2.4.9. Filters 2.4.10. Queries 2.4.11. Custom SQL for CRUD operations 2.4.12. Tuplizer

    CHAPTER 3. OVERRIDING METADATA THROUGH XML 3.1. PRINCIPLES

    3.1.1. Global level metadata 3.1.2. Entity level metadata 3.1.3. Property level metadata 3.1.4. Association level metadata

    CHAPTER 4. ADDITIONAL MODULES 4.1. HIBERNATE VALIDATOR

    4.1.1. Description 4.1.2. Integration with Hibernate Annotations

    4.2. HIBERNATE SEARCH 4.2.1. Description 4.2.2. Integration with Hibernate Annotations

    APPENDIX A. REVISION HISTORY

    45 45 46 47 48 48 49 49 49 49 50 51 53 54 54 55 56 56 58

    59 59 59 59 63 64

    65 65 65 65 66 66 66

    67

    Hibernate Annotations Reference Guide

    2

  • Table of Contents

    3

  • CHAPTER 1. SETTING UP AN ANNOTATIONS PROJECT

    1.1. REQUIREMENTS

    This release requires Hibernate Core 3.3 and above.

    This release is known to work on Hibernate Core 3.3.2.GA.

    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, download it from the Hibernate website and add hibernate- validator.jar in your classpath.

    If you wish to use Hibernate Search, 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 start Hibernate in a static initializer block, known as HibernateUtil. 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:

View more >