Package org.hibernate.cfg
This package defines APIs for configuring Hibernate.
AvailableSettings
enumerates all the configuration properties recognized by Hibernate.Configuration
provides a simplified API for bootstrapping Hibernate, as an alternative to the more extensive facilities defined underorg.hibernate.boot
.
Note that all the real meat behind these APIs is defined in the package
org.hibernate.boot
.
Configuration properties may be specified:
- in Java code that configures Hibernate,
- in a JPA configuration file named
persistence.xml
, - in a native configuration file named
hibernate.cfg.xml
, - in a file named
hibernate.properties
, or - using some container-specific configuration facility, for example, Quarkus configuration properties.
We now present a couple of example configuration files.
Example JPA configuration file
The following JPA configuration may be specified in a file namedpersistence.xml
:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="postgresql-example" transaction-type="RESOURCE_LOCAL">
<class>org.hibernate.orm.example.Author</class>
<class>org.hibernate.orm.example.Book</class>
<properties>
<!-- PostgreSQL -->
<property name="javax.persistence.jdbc.url"
value="jdbc:postgresql://localhost/library"/>
<!-- Credentials -->
<property name="javax.persistence.jdbc.user"
value="hibernate"/>
<property name="javax.persistence.jdbc.password"
value="hibernate"/>
<!-- Agroal connection pool config -->
<property name="hibernate.agroal.maxSize"
value="10"/>
<property name="hibernate.agroal.acquisitionTimeout"
value="PT1s"/>
<property name="hibernate.agroal.reapTimeout"
value="PT10s"/>
<!-- Automatic schema export -->
<property name="javax.persistence.schema-generation.database.action"
value="drop-and-create"/>
<!-- SQL statement logging -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.highlight_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
The JPA configuration file is necessary when bootstrapping Hibernate via
Persistence.createEntityManagerFactory(java.lang.String)
.
Example native configuration file
The following configuration may be specified in a file namedhibernate.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>
<!-- PostgreSQL -->
<property name="javax.persistence.jdbc.url">jdbc:postgresql://localhost/library</property>
<!-- Credentials -->
<property name="hibernate.connection.username">hibernate</property>
<property name="hibernate.connection.password">hibernate</property>
<!-- Agroal connection pool config -->
<property name="hibernate.agroal.maxSize">10</property>
<property name="hibernate.agroal.acquisitionTimeout">PT1s</property>
<property name="hibernate.agroal.reapTimeout">PT10s</property>
<!-- Automatic schema export -->
<property name="hibernate.hbm2ddl.auto">create</property>
<!-- SQL statement logging -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.highlight_sql">true</property>
<!-- Maximum JDBC batch size -->
<property name="hibernate.jdbc.batch_size">10</property>
<!-- Entity classes -->
<mapping class="org.hibernate.orm.example.Author"/>
<mapping class="org.hibernate.orm.example.Book"/>
</session-factory>
</hibernate-configuration>
The native configuration file is used when configuring Hibernate via
Configuration.configure()
or
StandardServiceRegistryBuilder.configure()
.-
Interface Summary Interface Description AvailableSettings Enumerates the configuration properties supported by Hibernate, including properties defined by the JPA specification.BatchSettings BytecodeSettings Settings which control theBytecodeProvider
used for bytecode enhancement and reflection optimization.C3p0Settings CacheSettings Settings for Hibernate's second-level cachingEnvironmentSettings FetchSettings JdbcSettings Settings related to JDBC, Connections, pools, Dialects, etcJpaComplianceSettings ManagedBeanSettings MappingSettings MultiTenancySettings NamingStrategy Deprecated, for removal: This API element is subject to removal in a future version. ImplicitNamingStrategy
andPhysicalNamingStrategy
should be used instead.PersistenceSettings Settings related to persistence-unitsProxoolSettings QuerySettings SchemaToolingSettings SecondPass Deprecated, for removal: This API element is subject to removal in a future version. UseSecondPass
insteadSessionEventSettings StatisticsSettings TransactionSettings ValidationSettings -
Class Summary Class Description Configuration A convenience API making it easier to bootstrap an instance of Hibernate.DefaultComponentSafeNamingStrategy Deprecated. NamingStrategy
itself has been deprecatedDefaultNamingStrategy Deprecated. NamingStrategy
itself has been deprecatedEnvironment Provides access to configuration properties passed inProperties
objects.ImprovedNamingStrategy Deprecated. NamingStrategy
itself has been deprecatedPersistenceStandardNamingStrategy Deprecated. NamingStrategy
itself has been deprecated -
Enum Summary Enum Description MetadataSourceType Deprecated, for removal: This API element is subject to removal in a future version. hbm.xml
mappings are no longer supported, making this attribute irrelevant -
Exception Summary Exception Description RecoverableException Deprecated. Was only ever referenced in a single place, in an extremely dubious way.