Red Hat Jboss Enterprise Application Platform 7.3 Developing Hibernate Applications
Total Page:16
File Type:pdf, Size:1020Kb
Red Hat JBoss Enterprise Application Platform 7.3 Developing Hibernate Applications Instructions and information for developers and administrators who want to develop and deploy Jakarta Persistence API (JPA) or Hibernate applications for Red Hat JBoss Enterprise Application Platform. Last Updated: 2021-09-10 Red Hat JBoss Enterprise Application Platform 7.3 Developing Hibernate Applications Instructions and information for developers and administrators who want to develop and deploy Jakarta Persistence API (JPA) or Hibernate applications for Red Hat JBoss Enterprise Application Platform. Legal Notice Copyright © 2021 Red Hat, Inc. The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/ . In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version. 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, the Red Hat 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 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 This document provides information for developers and administrators who want to develop and deploy Jakarta Persistence or Hibernate applications with Red Hat JBoss Enterprise Application Platform. Table of Contents Table of Contents .C . H. .A . P. .T .E . R. 1.. .I .N . T. .R .O . .D . U. .C . T. .I O. N. 6. 1.1. ABOUT HIBERNATE CORE 6 1.2. HIBERNATE ENTITYMANAGER 6 .C . H. .A . P. .T .E . R. 2. H. .I .B . E. R. .N . .A .T . E. C. .O . N. F. I. G. .U . R. .A . T. .I O. .N . 8. 2.1. HIBERNATE CONFIGURATION 8 2.2. SECOND-LEVEL CACHES 8 2.2.1. About Second-level Caches 8 2.2.2. Configure a Second-level Cache for Hibernate 9 Configuring a Second-level Cache for Hibernate Using Hibernate Native Applications 9 .C . H. .A . P. .T .E . R. 3. H. I.B . E. .R .N . .A . T. E. A. .N . N. .O . .T . A. .T .I .O . N. .S . 1.0 . 3.1. HIBERNATE ANNOTATIONS 10 .C . H. .A . P. .T .E . R. 4. .H . I.B . E. .R . N. .A . T. .E . Q. U. .E . R. Y. L. .A . N. .G . .U . A. .G . E. 1. 7. 4.1. ABOUT HIBERNATE QUERY LANGUAGE 17 Introduction to Java Persistence query language 17 Introduction to HQL 17 4.2. ABOUT HQL STATEMENTS 17 About the UPDATE and DELETE Statements 19 About the INSERT Statement 20 4.3. ABOUT HQL ORDERING 21 4.4. ABOUT COLLECTION MEMBER REFERENCES 22 4.5. ABOUT QUALIFIED PATH EXPRESSIONS 22 4.6. ABOUT HQL FUNCTIONS 24 4.6.1. About HQL Standardized Functions 24 4.6.2. About HQL Non-Standardized Functions 24 4.6.3. About the Concatenation Operation 25 4.7. ABOUT DYNAMIC INSTANTIATION 25 4.8. ABOUT HQL PREDICATES 26 HQL Predicates 26 4.9. ABOUT RELATIONAL COMPARISONS 28 4.10. BYTECODE ENHANCEMENT 30 4.10.1. Lazy Attribute Loading 30 .C . H. .A . P. .T .E . R. 5. H. I.B . E. R. .N . .A . T. E. S. .E .R . V. .I C. .E . S. .3 . 2. 5.1. ABOUT HIBERNATE SERVICES 32 5.2. ABOUT SERVICE CONTRACTS 32 5.3. TYPES OF SERVICE DEPENDENCIES 32 5.3.1. The Service Registry 32 5.3.1.1. About the ServiceRegistry 32 5.3.2. Custom Services 33 5.3.2.1. About Custom Services 33 5.3.3. The Boot-Strap Registry 34 5.3.3.1. About the Boot-strap Registry 34 Using BootstrapServiceRegistryBuilder 34 5.3.3.2. BootstrapRegistry Services 34 5.3.4. SessionFactory Registry 35 5.3.4.1. SessionFactory Services 35 5.3.5. Integrators 36 5.3.5.1. Integrator Use Cases 36 1 Red Hat JBoss Enterprise Application Platform 7.3 Developing Hibernate Applications .C . H. .A . P. .T .E . R. 6. .H . I.B . E. .R .N . .A . T. E. E. .N . V. .E . R. .S . .3 . 8. 6.1. ABOUT HIBERNATE ENVERS 38 6.2. ABOUT AUDITING PERSISTENT CLASSES 38 6.3. AUDITING STRATEGIES 38 6.3.1. About Auditing Strategies 38 6.3.2. Set the Auditing Strategy 39 Define an Auditing Strategy 39 6.3.3. Adding Auditing Support to a Jakarta Persistence Entity 39 6.4. CONFIGURATION 40 6.4.1. Configure Envers Parameters 41 6.4.2. Enable or Disable Auditing at Runtime 41 6.4.3. Configure Conditional Auditing 42 6.4.4. Envers Configuration Properties 42 6.5. QUERYING AUDIT INFORMATION 44 6.5.1. Retrieve Auditing Information Through Queries 44 6.5.2. Traversing Entity Associations Using Properties of Referenced Entities 47 6.6. PERFORMANCE TUNING 48 6.6.1. Alternative Batch Loading Algorithms 48 6.6.2. Second Level Caching of Object References for Non-mutable Data 50 .C . H. .A . P. .T .E . R. 7. H. I.B . E. R. .N . .A . T. E. S. .E .A . R. .C . H. 5. .1 . 7.1. GETTING STARTED WITH HIBERNATE SEARCH 51 7.1.1. About Hibernate Search 51 7.1.2. Overview of Hibernate Search 51 7.1.3. About the Directory Provider 51 7.1.4. About the Worker 52 7.1.5. Back End Setup and Operations 52 7.1.5.1. Back End 52 7.1.5.2. Lucene 52 7.1.5.3. Jakarta Messaging 53 7.1.6. Reader Strategies 54 7.1.6.1. The Shared Strategy 54 7.1.6.2. The Not-shared Strategy 55 7.1.6.3. Custom Reader Strategies 55 7.2. CONFIGURATION 55 7.2.1. Minimum Configuration 55 7.2.2. Configuring the IndexManager 55 7.2.2.1. Directory-based 55 7.2.2.2. Near Real Time 56 7.2.2.3. Custom 56 7.2.3. DirectoryProvider Configuration 56 Directory Providers and Their Properties 57 7.2.4. Worker Configuration 59 7.2.4.1. Jakarta Messaging Master/Slave Back End 62 7.2.4.2. Slave Nodes 63 7.2.4.3. Master Node 63 7.2.5. Tuning Lucene Indexing 64 7.2.5.1. Tuning Lucene Indexing Performance 64 7.2.5.2. The Lucene IndexWriter 68 7.2.5.3. Performance Option Configuration 68 7.2.5.4. Tuning the Indexing Speed 72 7.2.5.5. Control Segment Size 72 7.2.6. LockFactory Configuration 73 2 Table of Contents 7.2.7. Index Format Compatibility 74 7.3. HIBERNATE SEARCH FOR YOUR APPLICATION 74 7.3.1. First Steps with Hibernate Search 75 7.3.2. Enable Hibernate Search Using Maven 75 7.3.3. Add Annotations 75 7.3.4. Indexing 78 7.3.5. Searching 78 7.3.6. Analyzer 79 7.4. MAPPING ENTITIES TO THE INDEX STRUCTURE 80 7.4.1. Mapping an Entity 80 7.4.1.1. Basic Mapping 81 7.4.1.2. @Indexed 81 7.4.1.3. @Field 81 7.4.1.4. @NumericField 83 7.4.1.5. @Id 84 7.4.1.6. Mapping Properties Multiple Times 85 7.4.1.7. Embedded and Associated Objects 85 7.4.1.8. Limiting Object Embedding to Specific Paths 88 7.4.2. Boosting 91 7.4.2.1. Static Index Time Boosting 91 7.4.2.2. Dynamic Index Time Boosting 91 7.4.3. Analysis 92 7.4.3.1. Default Analyzer and Analyzer by Class 92 7.4.3.2. Named Analyzers 93 7.4.3.3. Available Analyzers 95 7.4.3.4. Dynamic Analyzer Selection 97 7.4.3.5. Retrieving an Analyzer 98 7.4.4. Bridges 99 7.4.4.1. Built-in Bridges 99 7.4.4.2. Custom Bridges 100 7.4.4.2.1. StringBridge 100 7.4.4.2.2. Parameterized Bridge 101 7.4.4.2.3. Type Aware Bridge 102 7.4.4.2.4. Two-Way Bridge 102 7.4.4.2.5. FieldBridge 103 7.4.4.2.6. ClassBridge 104 7.5. USING HIBERNATE SEARCH TO EXECUTE LUCENE QUERIES 105 7.5.1. Building Queries 107 7.5.1.1. Building a Lucene Query Using the Lucene API 107 7.5.1.2. Building a Lucene Query 107 7.5.1.3. Keyword Queries 108 7.5.1.4. Fuzzy Queries 110 7.5.1.5. Wildcard Queries 110 7.5.1.6.