The Hibernate Types project gives you extra types and general-purpose utilities that are not supported by the Hibernate ORM core.
The main advantage of this project is that it supports a broad range of Hibernate versions, spanning from Hibernate ORM 6 to 5.6, 5.5, 5.4, 5.3, 5.2, 5.1, 5, 4.3, 4.2, and Hibernate 4.1.
Depending on the Hibernate version you are using, you need to add the following dependency:
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-60</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-55</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-5</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-43</artifactId>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-4</artifactId>
<version>2.18.0</version>
</dependency>
The Hibernate Types project defines a list of optional dependencies that you will have to declare explicitly in your project in order to use them.
The reason why all these dependencies are optional, like Guava, Jackson, or PostgreSQL JDBC Driver, is because not all projects may need them.
More, the dependency version is extremely important because, from time to time, security issues may be discovered that get fixed in newer versions.
So, relying on this library to supply you with the dependency versions is a very dangerous thing to do.
For instance, there have been 65 security issues discovered in the Jackson Data Bind library this project is heavily relying on.
To avoid risking security issues, you need to take the responsibility of constantly upgrading all the dependencies that you are using along with the Hibernate Types library.
If you are using JSON Types, then you might be interested in setting the following dependencies based on your Hibernate version:
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jakarta-xmlbind-annotations</artifactId>
<version>${jackson-module-jakarta-xmlbind-annotation}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>${jackson-module-jaxb-annotation}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
If you are mapping a Range
using Guava, then you have to provide the Guava dependency explicitly:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
If you are mapping a MonetaryAmount
, then you have to provide the Moneta dependency explicitly:
<dependency>
<groupId>org.javamoney</groupId>
<artifactId>moneta</artifactId>
<version>${moneta.version}</version>
<type>pom</type>
</dependency>
If you are mapping a PostgreSQL-specific column type (e.g., inet
, hstore
, array
, interval
), then you have to provide the PostgreSQL dependency explicitly:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
The JsonType
allows you to map JSON column types, no matter if you're using Oracle,
SQL Server, PostgreSQL or MySQL.
If you're using Hibernate 6, you can map any JSON column to Map
, List
, POJO, String
, or JsonNode
entity property:
@Type(JsonType.class)
private Map<String, String> properties = new HashMap<>();
If you're using Hibernate 5 or 4, you can either provide the fully-qualified name of the Hibernate Type:
@Type(type = "com.vladmihalcea.hibernate.type.json.JsonType")
Or, you can add the following mapping to your package-info.java
class in the same package where your JPA entities are located:
@TypeDef(
name = "json", typeClass = JsonType.class
)
package io.hypersistence.optimizer;
import com.vladmihalcea.hibernate.type.json.JsonType;
import org.hibernate.annotations.TypeDef;
And later, you can map the Map
, List
, POJO, String
, or JsonNode
entity properties to JSON columns like this:
@Type(type = "json")
private Map<String, String> properties = new HashMap<>();
For more details, check out this article.
When mapping a JSON column type to a
POJO
,List<POJO>
orMap<String, POJO>
, you need to make sure that thePOJO
type overrides the defaultequals
andhashCode
methods and implements them according to the JSON object content.Otherwise, the Hibernate dirty checking mechanism may trigger unexpected UPDATE statements. Check out the #134 issue for more details.
When using Oracle, you have several options:
- you can use the generic
JsonType
that can work with theJSON
,VARCHAR
, orBLOB
column types, as long as you hint the column type using thecolumnDefinition
attribute of the JPA@Column
annotation. - you can use the
JsonStringType
to map aVARCHAR2
column type storing JSON. - you can use the
JsonBlobType
to map aBLOB
column type storing JSON.
For more details, check out this article.
When using SQL Server, you can use the generic JsonType
or the JsonStringType
to map an NVARCHAR
column type storing JSON.
For more details, check out this article.
When using PostgreSQL, you can use the generic JsonType
or the JsonBinaryType
to map both jsonb
and json
column types.
For more details, check out this article.
When using MySQL, you can use the generic JsonType
or the JsonStringType
to map the json
column type.
For more details, check out this article.
- How to map a POJO to a JSON column using JPA and Hibernate
- How to map a String JPA property to a JSON column using Hibernate
- How to map a Jackson
JsonNode
to a JSON column - How to map JSON collections using JPA and Hibernate
- How to map a Java Map entity property as a JSON column with JPA and Hibernate
- How to map Java Records to JSON columns using Hibernate
- How to map polymorphic JSON objects with JPA and Hibernate
- How to encrypt and decrypt JSON properties with JPA and Hibernate
- Why you should use Hibernate Dynamic Update for JSON properties
- How to map Oracle JSON columns using JPA and Hibernate
- How to map SQL Server JSON columns using JPA and Hibernate
- How to customize the Jackson ObjectMapper used by Hibernate-Types
- How to customize the JSON Serializer used by Hibernate-Types
- How to fix the Hibernate
No Dialect mapping for JDBC type: 1111
issue when mixing JSON types with native SQL queries - How to fix the
column is of type jsonb but expression is of type record
orbytea
issue
- How to map a PostgreSQL ARRAY to a Java
List
with JPA and Hibernate - How to map the PostgreSQL ARRAY to Java Arrays with JPA and Hibernate
- Multidimensional array mapping with JPA and Hibernate
- How to map the HSQLDB ARRAY type with JPA and Hibernate
- Java Enum to PostgreSQL Enum Type
- How to map the PostgreSQL Inet type with JPA and Hibernate
- How to map a PostgreSQL HStore entity property with JPA and Hibernate
- How to map a PostgreSQL Enum ARRAY to a JPA entity property using Hibernate
- How to map a PostgreSQL Range column type with JPA and Hibernate
- How to map a PostgreSQL Interval to a Java
Duration
with Hibernate
- The best way to map
MonetaryAmount
with JPA and Hibernate - How to map
java.time.YearMonth
to DATE or INTEGER column - How to map
java.time.Year
andjava.time.Month
with JPA and Hibernate Character
to nullable CHAR columnImmutableType
utility to simplifyUserType
implementations
When using the HibernateRepository
, make sure that you include the com.vladmihalcea.spring.repository
package
in your @EnableJpaRepositories
configuration:
@Configuration
@EnableJpaRepositories(
basePackages = {
"com.vladmihalcea.spring.repository",
...
}
)
public class JpaConfiguration {
...
}
ClassImportIntegrator
- How to write a compact DTO projection query with JPAListResultTransformer
- The best way to use a Hibernate ResultTransformerMapResultTransformer
- How to return a Map result from a JPA or Hibernate query
- Java version supported by the Hibernate ORM version you are using.
- SLF4J
- Jackson Databind
If you have an issue, then there are two ways to address it.
Since this project is open-source, you have the ability to fix any issue you bump into. Therefore, when dealing with a problem, this is what you need to do:
- Provide a replicating test case using the existing test cases as a template
- Provide a fix proposal
- Send a Pull Request with the fix proposal and the test case
If you don't have the time to provide a fix, then I can fix your issue via consulting. If you're in a hurry, this is going to be your best option.
Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. No more performance issues, no more having to spend countless hours trying to figure out why your application is barely crawling.
Imagine discovering early during the development cycle that you are using suboptimal mappings and entity relationships or that you are missing performance-related settings.
More, with Hypersistence Optimizer, you can detect all such issues during testing and make sure you don't deploy to production a change that will affect data access layer performance.
Hypersistence Optimizer is the tool you've been long waiting for!
If you are interested in on-site training, I can offer you my High-Performance Java Persistence training, which can be adapted to one, two or three days of sessions. For more details, check out my website.
If you want me to review your application and provide insight into how you can optimize it to run faster, then check out my consulting page.
If you want the fastest way to learn how to speed up a Java database application, then you should definitely enroll in my High-Performance Java Persistence video courses.
Or, if you prefer reading books, you are going to love my High-Performance Java Persistence book as well.
The project uses Maven Toolchains as different modules are compiled and tested using different Java versions. Hibernate Types 6 requires Java 17 while the other modules are compiled with either Java 1.8 or 1.6.
To see how to configure Maven Toolchains, check out this article.
The project uses various database systems for integration testing, and you can configure the JDBC connection settings using the
DatasourceProvider
instances (e.g., PostgreSQLDataSourceProvider
), and the project uses Testcontainers to bootstrap a Docker container
with the required Oracle, SQL Server, PostgreSQL, or MySQL instance on demand.
If you are a regular contributor, it's advisable to set up the required database locally or use the Docker Compose configuration provided in the
docker
folder, as bootstrapping the containers on demand is slower, and your tests are going to take longer to run.
If you want to fix an issue or add support for a new feature, please provide the associated integration test case that proves the improvement is working as expected.