Hibernate orm 5.3.7.final user guide pdf download






















Session instances. A SessionFactory is very expensive to create, so, for any given database, the application should have only one associated SessionFactory.

The SessionFactory maintains services that Hibernate uses across all Session s such as second level caches, connection pools, transaction system integrations, etc. Connection and acts as a factory for org. Transaction instances. It maintains a generally "repeatable read" persistence context first level cache of the application domain model. A single-threaded, short-lived object used by the application to demarcate individual physical transaction boundaries.

The term domain model comes from the realm of data modeling. It is the model that ultimately describes the problem domain you are working in. Sometimes you will also hear the term persistent classes. Ultimately the application domain model is the central character in an ORM. They make up the classes you wish to map. However, none of these rules are hard requirements. Indeed, Hibernate assumes very little about the nature of your persistent objects. You can express a domain model in other ways using trees of java.

Map instances, for example. Historically applications using Hibernate would have used its proprietary XML mapping file format for this purpose. This chapter will focus on JPA mapping where possible.

A type, in this usage, is an implementation of the org. Type interface. This Hibernate type also describes various behavioral aspects of the Java type such as how to check for equality, how to clone values, etc. It provides information about mapping a Java type to an SQL type as well as how to persist and fetch a given Java type to and from a relational database. When you encounter the term type in discussions of Hibernate, it may refer to the Java type, the JDBC type, or the Hibernate type, depending on the context.

A value type is a piece of data that does not define its own lifecycle. It is, in effect, owned by an entity, which defines its lifecycle. Looked at another way, all the state of an entity is made up entirely of value types. These state fields or JavaBean properties are termed persistent attributes.

The persistent attributes of the Contact class are value types. Basic types are discussed in detail in Basic types. Collection types are further discussed in Collections.

Entities, by nature of their unique identifier, exist independently of other objects whereas values do not. Entities are domain model classes which correlate to rows in a database table, using a unique identifier. Because of the requirement for a unique identifier, entities exist independently and define their own lifecycle.

The Contact class itself would be an example of an entity. Part of the mapping of an object model to the relational database is mapping names from the object model to the corresponding database names.

Hibernate looks at this as 2-stage process:. The first stage is determining a proper logical name from the domain model mapping. A logical name can be either explicitly specified by the user e. Second is the resolving of this logical name to a physical name which is defined by the PhysicalNamingStrategy contract. Historically Hibernate defined just a single org. That singular NamingStrategy contract actually combined the separate concerns that are now modeled individually as ImplicitNamingStrategy and PhysicalNamingStrategy.

Also, the NamingStrategy contract was often not flexible enough to properly apply a given naming "rule", either because the API lacked the information to decide or because the API was honestly not well defined as it grew. Due to these limitation, org. At the core, the idea behind each naming strategy is to minimize the amount of repetitive information a developer must provide for mapping a domain model. JPA defines inherent rules about implicit logical name determination. Also, JPA defines no separation between logical and physical name.

Following the JPA specification, the logical name is the physical name. When an entity does not explicitly name the database table that it maps to, we need to implicitly determine that table name. Or when a particular attribute does not explicitly name the database column that it maps to, we need to implicitly determine that column name. There are examples of the role of the org. ImplicitNamingStrategy contract to determine a logical name when the mapping did not provide an explicit name.

Hibernate defines multiple ImplicitNamingStrategy implementations out-of-the-box. Applications are also free to plug in custom implementations. There are multiple ways to specify the ImplicitNamingStrategy to use. First, applications can specify the implementation using the hibernate. ImplicitNamingStrategy contract.

FQN of a class that implements the org. Secondly, applications and integrations can leverage org. See Bootstrap for additional details on bootstrapping. Many organizations define rules around the naming of database objects tables, columns, foreign keys, etc. The idea of a PhysicalNamingStrategy is to help implement such naming rules without having to hard-code them into the mapping via explicit names.

But the point here is the separation of concerns. The PhysicalNamingStrategy will be applied regardless of whether the attribute explicitly specified the column name or whether we determined that implicitly.

The ImplicitNamingStrategy would only be applied if an explicit name was not given. So, it all depends on needs and intent. The default implementation is to simply use the logical name as the physical name. However applications and integrations can define custom implementations of this PhysicalNamingStrategy contract.

Here is an example PhysicalNamingStrategy for a fictitious company named Acme Corp whose naming standards are to:. There are multiple ways to specify the PhysicalNamingStrategy to use. PhysicalNamingStrategy contract.

MetadataBuilder applyPhysicalNamingStrategy. Basic value types usually map a single database column, to a single, non-aggregated Java type. Hibernate provides a number of built-in basic types, which follow the natural mappings recommended by the JDBC specifications. Internally Hibernate uses a registry of basic types when it needs to resolve a specific org. The uppercase value is written to the database. Unlike the other value types, multiple instances of this type are registered.

It is registered once under java. Serializable, and registered under the specific java. Serializable implementation class names. To use the Hibernate Spatial types, you must add the hibernate-spatial dependency to your classpath and use an org. SpatialDialect implementation. These mappings are managed by a service inside Hibernate called the org.

BasicTypeRegistry , which essentially maintains a map of org. BasicType a org. Type specialization instances keyed by a name. Latest release announcement : 5. A detailed list of new features, improvements and fixes in this series can be found on our issue tracker. How to get it. Maven artifacts Download Resolved issues Release announcement.

Tools About. Hibernate ORM 5. Improved bootstrapping, hibernate-java8, hibernate-spatial, Karaf support. About Releases Overview 6. Documentation 6. Engage with the community using mailing lists, forums, IRC, or other ways listed in the Community section. Help improve or translate this documentation. Contact us on the developer mailing list if you have interest. When building Hibernate 5. New users may want to first look through the Hibernate Getting Started Guide for basic information as well as tutorials.

There is also a series of topical guides providing deep dives into various topics. While having a strong background in SQL is not required to use Hibernate, it certainly helps a lot because it all boils down to SQL statements.

Probably even more important is an understanding of data modeling principles. You might want to consider these resources as a good starting point:. These topics will be discussed in the documentation, but a prior understanding will certainly help. Hibernate, as an ORM solution, effectively "sits between" the Java application data access layer and the Relational Database, as can be seen in the diagram above.

Here we will introduce the essential Hibernate APIs. This will be a brief introduction; we will discuss these contracts in detail later. A thread-safe and immutable representation of the mapping of the application domain model to a database. Acts as a factory for org. Session instances. A SessionFactory is very expensive to create, so, for any given database, the application should have only one associated SessionFactory. The SessionFactory maintains services that Hibernate uses across all Session s such as second level caches, connection pools, transaction system integrations, etc.

Connection and acts as a factory for org. Transaction instances. It maintains a generally "repeatable read" persistence context first level cache of the application domain model. A single-threaded, short-lived object used by the application to demarcate individual physical transaction boundaries. The term domain model comes from the realm of data modeling. It is the model that ultimately describes the problem domain you are working in.

Sometimes you will also hear the term persistent classes. Ultimately the application domain model is the central character in an ORM. They make up the classes you wish to map. However, none of these rules are hard requirements. Indeed, Hibernate assumes very little about the nature of your persistent objects. You can express a domain model in other ways using trees of java.

Map instances, for example. Historically applications using Hibernate would have used its proprietary XML mapping file format for this purpose. This chapter will focus on JPA mapping where possible.

A type, in this usage, is an implementation of the org. Type interface. This Hibernate type also describes various behavioral aspects of the Java type such as how to check for equality, how to clone values, etc.

It provides information about mapping a Java type to an SQL type as well as how to persist and fetch a given Java type to and from a relational database.

When you encounter the term type in discussions of Hibernate, it may refer to the Java type, the JDBC type, or the Hibernate type, depending on the context.

A value type is a piece of data that does not define its own lifecycle. It is, in effect, owned by an entity, which defines its lifecycle. Looked at another way, all the state of an entity is made up entirely of value types. These state fields or JavaBean properties are termed persistent attributes. The persistent attributes of the Contact class are value types.

Basic types are discussed in detail in Basic types. Collection types are further discussed in Collections.

Entities, by nature of their unique identifier, exist independently of other objects whereas values do not. Entities are domain model classes which correlate to rows in a database table, using a unique identifier.

Because of the requirement for a unique identifier, entities exist independently and define their own lifecycle. The Contact class itself would be an example of an entity. Part of the mapping of an object model to the relational database is mapping names from the object model to the corresponding database names.

Hibernate looks at this as 2-stage process:. The first stage is determining a proper logical name from the domain model mapping.

A logical name can be either explicitly specified by the user e. Second is the resolving of this logical name to a physical name which is defined by the PhysicalNamingStrategy contract. Historically Hibernate defined just a single org. That singular NamingStrategy contract actually combined the separate concerns that are now modeled individually as ImplicitNamingStrategy and PhysicalNamingStrategy. Also, the NamingStrategy contract was often not flexible enough to properly apply a given naming "rule", either because the API lacked the information to decide or because the API was honestly not well defined as it grew.

Due to these limitation, org. At the core, the idea behind each naming strategy is to minimize the amount of repetitive information a developer must provide for mapping a domain model.

JPA defines inherent rules about implicit logical name determination. Also, JPA defines no separation between logical and physical name. Following the JPA specification, the logical name is the physical name. When an entity does not explicitly name the database table that it maps to, we need to implicitly determine that table name. Or when a particular attribute does not explicitly name the database column that it maps to, we need to implicitly determine that column name.

There are examples of the role of the org. ImplicitNamingStrategy contract to determine a logical name when the mapping did not provide an explicit name.

Hibernate defines multiple ImplicitNamingStrategy implementations out-of-the-box. Applications are also free to plug in custom implementations. There are multiple ways to specify the ImplicitNamingStrategy to use. First, applications can specify the implementation using the hibernate.

ImplicitNamingStrategy contract. FQN of a class that implements the org. Secondly, applications and integrations can leverage org.

See Bootstrap for additional details on bootstrapping. Many organizations define rules around the naming of database objects tables, columns, foreign keys, etc.

The idea of a PhysicalNamingStrategy is to help implement such naming rules without having to hard-code them into the mapping via explicit names. But the point here is the separation of concerns. The PhysicalNamingStrategy will be applied regardless of whether the attribute explicitly specified the column name or whether we determined that implicitly.

The ImplicitNamingStrategy would only be applied if an explicit name was not given. So, it all depends on needs and intent. The default implementation is to simply use the logical name as the physical name. However applications and integrations can define custom implementations of this PhysicalNamingStrategy contract.

Here is an example PhysicalNamingStrategy for a fictitious company named Acme Corp whose naming standards are to:. There are multiple ways to specify the PhysicalNamingStrategy to use. PhysicalNamingStrategy contract. MetadataBuilder applyPhysicalNamingStrategy. Basic value types usually map a single database column, to a single, non-aggregated Java type. Hibernate provides a number of built-in basic types, which follow the natural mappings recommended by the JDBC specifications.

Internally Hibernate uses a registry of basic types when it needs to resolve a specific org. The uppercase value is written to the database. Unlike the other value types, multiple instances of this type are registered. It is registered once under java. Serializable, and registered under the specific java. Serializable implementation class names. To use the Hibernate Spatial types, you must add the hibernate-spatial dependency to your classpath and use an org. SpatialDialect implementation. These mappings are managed by a service inside Hibernate called the org.

BasicTypeRegistry , which essentially maintains a map of org. BasicType a org. Type specialization instances keyed by a name. That is the purpose of the "BasicTypeRegistry key s " column in the previous tables. Strictly speaking, a basic type is denoted by the javax. Basic annotation. Generally speaking, the Basic annotation can be ignored, as it is assumed by default.

Both of the following examples are ultimately the same. The JPA specification strictly limits the Java types that can be marked as basic to the following listing:. Boolean , java. Integer , etc. Note that JPA 2. AttributeConverter contract to help alleviate some of these concerns. See JPA 2. Defines whether this attribute allows nulls. JPA defines this as "a hint", which essentially means that its effect is specifically required.

Defines whether this attribute should be fetched eagerly or lazily. Hibernate ignores this setting for basic types unless you are using bytecode enhancement. See the Bytecode Enhancement for additional information on fetching and on bytecode enhancement. JPA defines rules for implicitly determining the name of tables and columns. For a detailed discussion of implicit naming see Naming strategies. For basic type attributes, the implicit naming rule is that the column name is the same as the attribute name.

If that implicit naming rule does not meet your requirements, you can explicitly tell Hibernate and other providers the column name to use. Here we use Column to explicitly map the description attribute to the NOTES column, as opposed to the implicit column name description. The Column annotation defines other mapping information as well.

See its Javadocs for details. We said before that a Hibernate type is not a Java type, nor an SQL type, but that it understands both and performs the marshalling between them. But looking at the basic type mappings from the previous examples, how did Hibernate know to use its org. StringType for mapping for java. String attributes, or its org. IntegerType for mapping java. Integer attributes? The answer lies in a service inside Hibernate called the org. BasicType an org.

We will see later, in the Explicit BasicTypes section, that we can explicitly tell Hibernate which BasicType to use for a particular attribute. A thorough discussion of BasicTypeRegistry and all the different ways to contribute types is beyond the scope of this documentation. Please see the Integration Guide for complete details. As an example, take a String attribute such as we saw before with Product sku.

Since there was no explicit type mapping, Hibernate looks to the BasicTypeRegistry to find the registered mapping for java. This goes back to the "BasicTypeRegistry key s " column we saw in the tables at the start of this chapter.

So that is the baseline mapping within BasicTypeRegistry for Strings. Applications can also extend add new BasicType registrations or override replace an existing BasicType registration using one of the MetadataBuilder applyBasicType methods or the MetadataBuilder applyTypes method during bootstrap.

For more details, see Custom BasicTypes section. Sometimes you want a particular attribute to be handled differently. Occasionally Hibernate will implicitly pick a BasicType that you do not want and for some reason you do not want to adjust the BasicTypeRegistry. In these cases, you must explicitly tell Hibernate the BasicType to use, via the org.

Type annotation. This tells Hibernate to store the Strings as nationalized data. This is just for illustration purposes; for better ways to indicate nationalized character data see Mapping Nationalized Character Data section. Additionally, the description is to be handled as a LOB. Hibernate makes it relatively easy for developers to create their own basic type mappings type. For example, you might want to persist properties of type java. On the Java side, we need to use a BitSetTypeDescriptor instance which can be implemented like this:.

The unwrap method is used when passing a BitSet as a PreparedStatement bind parameter, while the wrap method is used to transform the JDBC column value object e.

String in our case to the actual mapping object type e. BitSet in this example. With the new BitSetType being registered as bitset , the entity mapping looks like this:. In this example, the UserType is registered under the bitset name, and this is done like this:. Like BasicType , you can also register the UserType using a simple name. Without registering a name, the UserType mapping requires the fully qualified class name:. The original JPA-compliant way to map enums was via the Enumerated or MapKeyEnumerated for map keys annotations, working on the principle that the enum values are stored according to one of 2 strategies indicated by javax.

EnumType :. Enum ordinal. Enum name. So, when using the AttributeConverter approach, be sure not to mark the attribute as Enumerated. Traditionally, you could only use the DB data Caption representation, which in our case is a String , when referencing the caption entity property.

In order to use the Java object Caption representation, you have to get the associated Hibernate Type. By passing the associated Hibernate Type , you can use the Caption object when binding the query parameter value. When using HBM mappings, you can still make use of the JPA AttributeConverter because Hibernate supports such mapping via the type attribute as demonstrated by the following example.

For this purpose, we are going to use the following MoneyConverter utility:. To map the MoneyConverter using HBM configuration files you need to use the converted:: prefix in the type attribute of the property element.

You can also map enums using a Hibernate custom type mapping. For additional details on using custom types, see Custom BasicTypes section. However, they can be unnatural to deal with and have certain limitations. For example, a LOB locator is only portably valid during the duration of the transaction in which it was obtained. The idea of materialized LOBs is to trade-off the potential efficiency not all drivers handle LOB data efficiently for a more natural programming paradigm using familiar Java types such as String or byte[] , etc for these LOBs.

Mapping materialized forms of these LOB values would use more familiar Java types such as String , char[] , byte[] , etc. The trade-off for more familiar is usually performance. Clob type:. To persist such an entity, you have to create a Clob using the ClobProxy Hibernate utility:. To retrieve the Clob content, you need to transform the underlying java.

Reader :. We could also map the CLOB in a materialized form. This way, we can either use a String or a char[]. However, some drivers are trickier e. Such discussions are beyond the scope of this guide.

We might even want the materialized data as a char array although this might not be a very good idea. To persist such an entity, you have to create a Blob using the BlobProxy Hibernate utility:. To retrieve the Blob content, you need to transform the underlying java.

InputStream :. JDBC 4 added the ability to explicitly handle nationalized character data. To this end, it added specific nationalized character data types:. To map a specific attribute to a nationalized variant data type, Hibernate defines the Nationalized annotation. To retrieve the NClob content, you need to transform the underlying java. If your application and database use nationalization, you may instead want to enable nationalized character data as the default. You can do this via the hibernate.

However, many applications prefer the readability of the character-based column storage. To switch the default mapping, simply call MetadataBuilder. As mentioned, the default mapping for UUID attributes. Maps the UUID to a byte[] using java. UUID toString and java. For details, see the discussion of generators in Identifiers. Represents a calendar date by storing years, months and days.

The JDBC equivalent is java. Represents the time of a day and it stores hours, minutes and seconds. To avoid dependencies on the java.

Timestamp and java. Time ones. While the java. This way, a java. Date or a java. Just like the java. Date , the java. If the java. Date marks a point in time, the java. Calendar takes into consideration the default Time Zone. Instant , java. LocalDateTime , java. OffsetDateTime and java. By default, Hibernate is going to use the PreparedStatement. Timestamp or PreparedStatement. Time x when saving a java.

Timestamp or a java. Time property. When the time zone is not specified, the JDBC driver is going to use the underlying JVM default time zone, which might not be suitable if the application is used from all across the globe.

For this reason, it is very common to use a single reference time zone e. However, as explained in this article , this is not always practical, especially for front-end nodes. For this reason, Hibernate offers the hibernate. With this configuration property in place, Hibernate is going to call the PreparedStatement.

Timestamp, Calendar cal or PreparedStatement. Time x, Calendar cal , where the java. Calendar references the time zone provided via the hibernate. Although Hibernate has long been offering custom types , as a JPA 2. In the following example, the java. To make use of this custom converter, the Convert annotation must decorate the entity attribute.

When persisting such entity, Hibernate will do the type conversion based on the AttributeConverter logic:. In cases when the Java type specified for the "database side" of the conversion the second AttributeConverter bind parameter is not known, Hibernate will fallback to a java. Serializable type. Therefore, mutability is given by the JavaTypeDescriptor getMutabilityPlan of the associated entity attribute type. If the entity attribute is a String , a primitive wrapper e.

Integer , Long an Enum type, or any other immutable Object type, then you can only change the entity attribute value by reassigning it to a new value. Considering we have the same Period entity attribute as illustrated in the JPA 2.

On the other hand, consider the following example where the Money type is a mutable. A mutable Object allows you to modify its internal structure, and Hibernate dirty checking mechanism is going to propagate the change to the database:. Although the AttributeConverter types can be mutable so that dirty checking, deep copying, and second-level caching work properly, treating these as immutable when they really are is more efficient.

You can force Hibernate to quote an identifier in the generated SQL by enclosing the table or column name in backticks in the mapping document. Once the reserved keywords are escaped, Hibernate will use the correct quotation style for the SQL Dialect. Because name and number are reserved words, the Product entity mapping uses backticks to quote these column names. Hibernate can also quote all identifiers e. When persisting a Product entity, Hibernate is going to quote all identifiers as in the following example:.

For more about quoting-related configuration properties, check out the Mapping configurations section as well. Generated properties are properties that have their values generated by the database. Typically, Hibernate applications needed to refresh objects that contain any properties for which the database was generating values. Inside the release archive is a documentation directory that contains all various documentation guides for that version's release.

Unfortunately, the official Hibernate's documenation sources do not provide us with pdf versions. The following solution suits for Linux , but you can download according command-line tools for Windows as well:.

Let's create some local folder, go to there and then download the single-html version of the doc with embedded images:. Now, we have to convert the downloaded html page to pdf.

We'll use wkhtmltopdf , which you can download here :. The approach, described above, could be used not only for Hibernate, Spring etc. It is the universal solution for converting almost any single-html document pages to PDF. I think it is not possible to download a pdf version of the user guide of Hibernate. Worst case you will need a plugin. That's what I did and the resulting pdf is pretty decent.



0コメント

  • 1000 / 1000