JAVA PERSISTENCE API (JPA)

简介: 13.2.1. About JPA The Java Persistence API (JPA) is the standard for using persistence in Java projects.

13.2.1. About JPA

The Java Persistence API (JPA) is the standard for using persistence in Java projects. Java EE 6 applications use the Java Persistence 2.0 specification, documented here: http://www.jcp.org/en/jsr/detail?id=317.
Hibernate EntityManager implements the programming interfaces and life-cycle rules defined by the specification. It provides JBoss EAP 6 with a complete Java Persistence solution.
JBoss EAP 6 is 100% compliant with the Java Persistence 2.0 specification. Hibernate also provides additional features to the specification.
To get started with JPA and JBoss EAP 6, refer to the bean-validation, greeter, and kitchensink quickstarts: Section 1.4.1.1, “Access the Quickstarts”.

 

 

13.2.4. Configuration

13.2.4.1. Hibernate Configuration Properties

Table 13.1. Hibernate Java Properties

Property Name Description
hibernate.dialect
The classname of a Hibernate  org.hibernate.dialect.Dialect. Allows Hibernate to generate SQL optimized for a particular relational database.
In most cases Hibernate will be able to choose the correct  org.hibernate.dialect.Dialect implementation, based on the  JDBC metadata returned by the JDBC driver.
hibernate.show_sql
Boolean. Writes all SQL statements to console. This is an alternative to setting the log category  org.hibernate.SQL to  debug.
hibernate.format_sql
Boolean. Pretty print the SQL in the log and console.
hibernate.default_schema
Qualify unqualified table names with the given schema/tablespace in generated SQL.
hibernate.default_catalog
Qualifies unqualified table names with the given catalog in generated SQL.
hibernate.session_factory_name
The  org.hibernate.SessionFactory will be automatically bound to this name in JNDI after it has been created. For example,  jndi/composite/name.
hibernate.max_fetch_depth
Sets a maximum "depth" for the outer join fetch tree for single-ended associations (one-to-one, many-to-one). A  0 disables default outer join fetching. The recommended value is between  0 and  3.
hibernate.default_batch_fetch_size
Sets a default size for Hibernate batch fetching of associations. The recommended values are  48, and  16.
hibernate.default_entity_mode
Sets a default mode for entity representation for all sessions opened from this  SessionFactory. Values include:  dynamic-mapdom4jpojo.
hibernate.order_updates
Boolean. Forces Hibernate to order SQL updates by the primary key value of the items being updated. This will result in fewer transaction deadlocks in highly concurrent systems.
hibernate.generate_statistics
Boolean. If enabled, Hibernate will collect statistics useful for performance tuning.
hibernate.use_identifier_rollback
Boolean. If enabled, generated identifier properties will be reset to default values when objects are deleted.
hibernate.use_sql_comments
Boolean. If turned on, Hibernate will generate comments inside the SQL, for easier debugging. Default value is  false.
hibernate.id.new_generator_mappings
Boolean. This property is relevant when using  @GeneratedValue. It indicates whether or not the new  IdentifierGenerator implementations are used for  javax.persistence.GenerationType.AUTOjavax.persistence.GenerationType.TABLE and  javax.persistence.GenerationType.SEQUENCE. Default value is  true.
hibernate.ejb.naming_strategy
Chooses the  org.hibernate.cfg.NamingStrategy implementation when using Hibernate EntityManager. This class is deprecated and this property is only provided for backward compatibility. This property must not be used with  hibernate.ejb.naming_strategy_delegator.
If the application does not use EntityManager, follow the instructions here to configure the NamingStrategy:  Hibernate Reference Documentation - Implementing a Naming Strategy.
hibernate.ejb.naming_strategy_delegator
Specifies an  org.hibernate.cfg.naming.NamingStrategyDelegator implementation for database objects and schema elements when using Hibernate EntityManager. This property has the following possible values.
  • org.hibernate.cfg.naming.LegacyNamingStrategyDelegator: This is the default value. This class is deprecated and is only provided for backward compatibility.
  • org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator: This is the preferred value. It generates default table and column names that comply with the JPA specification. It allows for specification of both the entity and foreign key class names. This class only affects entities that are mapped using Java annotations or JPA XML descriptors. Entities mapped using  hbm.xml are not affected,
  • If you prefer, you can configure a custom class that implements  org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator

NOTE

This property must not be used with  hibernate.ejb.naming_strategy. It is a temporary replacement for  org.hibernate.cfg.NamingStrategy to address its limitations. A more comprehensive solution is planned for Hibernate 5.0 that replaces both  org.hibernate.cfg.NamingStrategy and  org.hibernate.cfg.naming.NamingStrategyDelegator.
If the application does not use EntityManager, follow the instructions here to configure the NamingStrategy:  Hibernate Reference Documentation - Implementing a Naming Strategy.

 

IMPORTANT

For  hibernate.id.new_generator_mappings, new applications should keep the default value of  true. Existing applications that used Hibernate 3.3.x may need to change it to  false to continue using a sequence object or table based generator, and maintain backward compatibility.

13.2.4.2. Hibernate JDBC and Connection Properties

Table 13.2. Properties

Property Name Description
hibernate.jdbc.fetch_size
A non-zero value that determines the JDBC fetch size (calls  Statement.setFetchSize()).
hibernate.jdbc.batch_size
A non-zero value enables use of JDBC2 batch updates by Hibernate. The recommended values are between  5 and  30.
hibernate.jdbc.batch_versioned_data
Boolean. Set this property to  true if the JDBC driver returns correct row counts from  executeBatch(). Hibernate will then use batched DML for automatically versioned data. Default value is to  false.
hibernate.jdbc.factory_class
Select a custom  org.hibernate.jdbc.Batcher. Most applications will not need this configuration property.
hibernate.jdbc.use_scrollable_resultset
Boolean. Enables use of JDBC2 scrollable resultsets by Hibernate. This property is only necessary when using user-supplied JDBC connections. Hibernate uses connection metadata otherwise.
hibernate.jdbc.use_streams_for_binary
Boolean. This is a system-level property. Use streams when writing/reading  binary or  serializable types to/from JDBC.
hibernate.jdbc.use_get_generated_keys
Boolean. Enables use of JDBC3  PreparedStatement.getGeneratedKeys() to retrieve natively generated keys after insert. Requires JDBC3+ driver and JRE1.4+. Set to false if JDBC driver has problems with the Hibernate identifier generators. By default, it tries to determine the driver capabilities using connection metadata.
hibernate.connection.provider_class
The classname of a custom  org.hibernate.connection.ConnectionProviderwhich provides JDBC connections to Hibernate.
hibernate.connection.isolation
Sets the JDBC transaction isolation level. Check  java.sql.Connection for meaningful values, but note that most databases do not support all isolation levels and some define additional, non-standard isolations. Standard values are  1, 2, 4, 8.
hibernate.connection.autocommit
Boolean. This property is not recommended for use. Enables autocommit for JDBC pooled connections.
hibernate.connection.release_mode
Specifies when Hibernate should release JDBC connections. By default, a JDBC connection is held until the session is explicitly closed or disconnected. The default value  auto will choose  after_statement for the JTA and CMT transaction strategies, and  after_transaction for the JDBC transaction strategy.
Available values are  auto (default),  on_closeafter_transactionafter_statement.
This setting only affects  Session returned from  SessionFactory.openSession. For  Session obtained through  SessionFactory.getCurrentSession, the  CurrentSessionContext implementation configured for use controls the connection release mode for that  Session.
hibernate.connection.<propertyName>
Pass the JDBC property  <propertyName> to DriverManager.getConnection().
hibernate.jndi.<propertyName>
Pass the property  <propertyName> to the JNDI InitialContextFactory.

13.2.4.3. Hibernate Cache Properties

Table 13.3. Properties

Property Name Description
hibernate.cache.region.factory_class
The classname of a custom  CacheProvider.
hibernate.cache.use_minimal_puts
Boolean. Optimizes second-level cache operation to minimize writes, at the cost of more frequent reads. This setting is most useful for clustered caches and, in Hibernate3, is enabled by default for clustered cache implementations.
hibernate.cache.use_query_cache
Boolean. Enables the query cache. Individual queries still have to be set cacheable.
hibernate.cache.use_second_level_cache
Boolean. Used to completely disable the second level cache, which is enabled by default for classes that specify a  <cache> mapping.
hibernate.cache.query_cache_factory
The classname of a custom  QueryCache interface. The default value is the built-in  StandardQueryCache.
hibernate.cache.region_prefix
A prefix to use for second-level cache region names.
hibernate.cache.use_structured_entries
Boolean. Forces Hibernate to store data in the second-level cache in a more human-friendly format.
hibernate.cache.default_cache_concurrency_strategy
Setting used to give the name of the default  org.hibernate.annotations.CacheConcurrencyStrategy to use when either  @Cacheable or  @Cache is used.  @Cache(strategy="..") is used to override this default.

 

13.2.4.4. Hibernate Transaction Properties

Table 13.4. Properties

Property Name Description
hibernate.transaction.factory_class
The classname of a  TransactionFactory to use with Hibernate  Transaction API. Defaults to  JDBCTransactionFactory).
jta.UserTransaction
A JNDI name used by  JTATransactionFactory to obtain the JTA  UserTransaction from the application server.
hibernate.transaction.manager_lookup_class
The classname of a  TransactionManagerLookup. It is required when JVM-level caching is enabled or when using hilo generator in a JTA environment.
hibernate.transaction.flush_before_completion
Boolean. If enabled, the session will be automatically flushed during the before completion phase of the transaction. Built-in and automatic session context management is preferred.
hibernate.transaction.auto_close_session
Boolean. If enabled, the session will be automatically closed during the after completion phase of the transaction. Built-in and automatic session context management is preferred.

 

 

https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Application_Platform/6.4/html/Development_Guide/sect-Java_Persistence_API_JPA.html

 

相关文章
|
4天前
|
安全 Java API
RESTful API设计与实现:Java后台开发指南
【4月更文挑战第15天】本文介绍了如何使用Java开发RESTful API,重点是Spring Boot框架和Spring MVC。遵循无状态、统一接口、资源标识和JSON数据格式的设计原则,通过创建控制器处理HTTP请求,如示例中的用户管理操作。此外,文章还提及数据绑定、验证、异常处理和跨域支持。最后,提出了版本控制、安全性、文档测试以及限流和缓存的最佳实践,以确保API的稳定、安全和高效。
|
6天前
|
存储 Java 关系型数据库
掌握Java 8 Stream API的艺术:详解流式编程(一)
掌握Java 8 Stream API的艺术:详解流式编程
34 1
|
16天前
|
前端开发 Java API
构建RESTful API:Java中的RESTful服务开发
【4月更文挑战第3天】本文介绍了在Java环境中构建RESTful API的重要性及方法。遵循REST原则,利用HTTP方法处理资源,实现CRUD操作。在Java中,常用框架如Spring MVC简化了RESTful服务开发,包括定义资源、设计表示层、实现CRUD、考虑安全性、文档和测试。通过Spring MVC示例展示了创建RESTful服务的步骤,强调了其在现代Web服务开发中的关键角色,有助于提升互操作性和用户体验。
构建RESTful API:Java中的RESTful服务开发
|
25天前
|
Java 数据库连接 API
Java 学习路线:基础知识、数据类型、条件语句、函数、循环、异常处理、数据结构、面向对象编程、包、文件和 API
Java 是一种广泛使用的、面向对象的编程语言,始于1995年,以其跨平台性、安全性和可靠性著称,应用于从移动设备到数据中心的各种场景。基础概念包括变量(如局部、实例和静态变量)、数据类型(原始和非原始)、条件语句(if、else、switch等)、函数、循环、异常处理、数据结构(如数组、链表)和面向对象编程(类、接口、继承等)。深入学习还包括包、内存管理、集合框架、序列化、网络套接字、泛型、流、JVM、垃圾回收和线程。构建工具如Gradle、Maven和Ant简化了开发流程,Web框架如Spring和Spring Boot支持Web应用开发。ORM工具如JPA、Hibernate处理对象与数
90 3
|
26天前
|
分布式计算 Java 程序员
Java 8新特性之Lambda表达式与Stream API
本文将详细介绍Java 8中的两个重要新特性:Lambda表达式和Stream API。Lambda表达式是Java 8中引入的一种简洁、匿名的函数表示方法,它允许我们将函数作为参数传递给其他方法。而Stream API则是一种新的数据处理方式,它允许我们以声明式的方式处理数据,从而提高代码的可读性和可维护性。通过本文的学习,你将能够掌握Lambda表达式和Stream API的基本用法,以及如何在项目中应用这两个新特性。
30 10
|
27天前
|
Java API 数据处理
Java 8新特性之Lambda表达式与Stream API
本文将介绍Java 8中的两个重要特性:Lambda表达式和Stream API。Lambda表达式是一种新的语法结构,允许我们将函数作为参数传递给方法。而Stream API则是一种处理数据的新方式,它允许我们对数据进行更简洁、更高效的操作。通过学习这两个特性,我们可以编写出更简洁、更易读的Java代码。
|
27天前
|
Java API Maven
email api java编辑方法?一文教你学会配置步骤
在Java开发中,Email API是简化邮件功能的关键工具。本文指导如何配置和使用Email API Java:首先,在项目中添加javax.mail-api和javax.mail依赖;接着,配置SMTP服务器和端口;然后,创建邮件,设定收件人、发件人、主题和正文;最后,使用Transport.send()发送邮件。借助Email API Java,可为应用添加高效邮件功能。
|
Java API
Java 8 Stream API详解
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/47038607 Java ...
980 0
|
9天前
|
缓存 前端开发 API
API接口封装系列
API(Application Programming Interface)接口封装是将系统内部的功能封装成可复用的程序接口并向外部提供,以便其他系统调用和使用这些功能,通过这种方式实现系统之间的通信和协作。下面将介绍API接口封装的一些关键步骤和注意事项。
|
16天前
|
监控 前端开发 JavaScript
实战篇:商品API接口在跨平台销售中的有效运用与案例解析
随着电子商务的蓬勃发展,企业为了扩大市场覆盖面,经常需要在多个在线平台上展示和销售产品。然而,手工管理多个平台的库存、价格、商品描述等信息既耗时又容易出错。商品API接口在这一背景下显得尤为重要,它能够帮助企业在不同的销售平台之间实现商品信息的高效同步和管理。本文将通过具体的淘宝API接口使用案例,展示如何在跨平台销售中有效利用商品API接口,以及如何通过代码实现数据的统一管理。