Skip to content

Commit cb6c7db

Browse files
authored
Add Hibernate 7 module (#185)
1 parent c4a680b commit cb6c7db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+7457
-17
lines changed

.github/dependabot.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
interval: "monthly"
7+
groups:
8+
github-actions:
9+
patterns:
10+
- "*"

.github/workflows/dep_build_v2.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
java_version: ['11', '17']
16+
java_version: ['17', '21']
1717
env:
1818
JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
1919
steps:

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
# 20-Dec-2022, tatu: Build now requires JDK 11; won't yet work with JDK 17
22-
java_version: [ '11']
21+
# Hibernate 7 requires Java 17+
22+
java_version: [ '17', '21' ]
2323
env:
2424
JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
2525
steps:

README.md

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ and properties; especially lazy-loading aspects.
99

1010
As of version 2.0 module is usable and used by non-trivial number of developers and projects.
1111

12-
Note: Hibernate 4.x and 5.x are supported (5.x starting with Jackson 2.6),
13-
but they require different jar, and Maven artifact names (and jar names differ).
14-
This document refers to "Hibernate 5" version, but changes with 4.x should require
15-
little more than replacing "5" in names with "4".
12+
Note: Hibernate 4.x, 5.x, 6.x and 7.x are supported (5.x starting with Jackson 2.6; 6.x with Jackson 2.15 and 7.x with Jackson 2.20) but they require different jars, and Maven artifact names (and jar names differ).
13+
14+
This document refers to "Hibernate 5" version, but changes with 4.x/6.x/7.x should require
15+
little more than replacing "5" in names with "4", "6" or "7".
1616

1717
Hibernate 3.x was supported up to Jackson 2.12 but is no longer supported at and after 2.13
1818

1919
Jackson 2.13 adds Support for "Hibernate 5 Jakarta" variant (for Hibernate 5.5 and beyond);
2020
see below for more information.
2121

22-
Jackson 2.15 adds Support for Hibernate 6.x;
23-
see below for more information.
22+
Jackson 2.15 adds Support for Hibernate 6.x; see below for more information.
23+
24+
Jackson 2.20 adds Support for Hibernate 7.x; see below for more information.
2425

2526
### JDK requirements
2627

@@ -31,6 +32,10 @@ With Jackson 2.15, JDK 11 will be required to build: all modules run on
3132
Java 8 except for Hibernate 6.x module which requires Java 11 like
3233
Hibernate 6.x itself.
3334

35+
With Jackson 2.20, JDK 17 will be required to build: 4.x and 5.x modules run on
36+
Java 8, 6.x on 11 and Hibernate 7.x module requires Java 17 like
37+
Hibernate 7.x itself.
38+
3439
### Javax vs Jakarta
3540

3641
Due to changes related to
@@ -54,7 +59,7 @@ To use module on Maven-based projects, use following dependency
5459
<dependency>
5560
<groupId>com.fasterxml.jackson.datatype</groupId>
5661
<artifactId>jackson-datatype-hibernate5</artifactId>
57-
<version>2.14.2</version>
62+
<version>2.19.1</version>
5863
</dependency>
5964
```
6065

@@ -66,7 +71,7 @@ Note that you need to use "jackson-datatype-hibernate4" for Hibernate 4.x.
6671
<dependency>
6772
<groupId>com.fasterxml.jackson.datatype</groupId>
6873
<artifactId>jackson-datatype-hibernate4</artifactId>
69-
<version>2.14.2</version>
74+
<version>2.19.1</version>
7075
</dependency>
7176
```
7277

@@ -77,17 +82,28 @@ you will need the jakarta suffixed dependency for Hibernate 5.5:
7782
<dependency>
7883
<groupId>com.fasterxml.jackson.datatype</groupId>
7984
<artifactId>jackson-datatype-hibernate5-jakarta</artifactId>
80-
<version>2.14.2</version>
85+
<version>2.19.1</version>
8186
</dependency>
8287
```
8388

84-
you will need to use "jackson-datatype-hibernate6" for Hibernate 6.x (when v2.15.0 is released):
89+
but you will need to use "jackson-datatype-hibernate6" for Hibernate 6.x:
90+
(for which only "jakarta" version exists).
8591

8692
```xml
8793
<dependency>
8894
<groupId>com.fasterxml.jackson.datatype</groupId>
8995
<artifactId>jackson-datatype-hibernate6</artifactId>
90-
<version>2.15.0</version>
96+
<version>2.19.1</version>
97+
</dependency>
98+
```
99+
100+
and finally, for Hibernate 7.x
101+
102+
```xml
103+
<dependency>
104+
<groupId>com.fasterxml.jackson.datatype</groupId>
105+
<artifactId>jackson-datatype-hibernate7</artifactId>
106+
<version>2.19.1</version>
91107
</dependency>
92108
```
93109

@@ -99,12 +115,30 @@ Like all standard Jackson modules (libraries that implement Module interface), r
99115
ObjectMapper mapper = new ObjectMapper();
100116
// for Hibernate 4.x:
101117
mapper.registerModule(new Hibernate4Module());
118+
// OR newer style
119+
ObjectMapper mapper = JsonMapper.builder()
120+
.addModule(new Hibernate4Module()));
121+
.build();
122+
102123
// or, for Hibernate 5.x
103-
mapper.registerModule(new Hibernate5Module());
124+
ObjectMapper mapper = JsonMapper.builder()
125+
.addModule(new Hibernate5Module()));
126+
.build();
127+
104128
// or, for Hibernate 5.5+ with Jakarta
105-
mapper.registerModule(new Hibernate5JakartaModule());
129+
ObjectMapper mapper = JsonMapper.builder()
130+
.addModule(new Hibernate5JakartaModule()));
131+
.build();
132+
106133
// or, for Hibernate 6.x
107-
mapper.registerModule(new Hibernate6Module());
134+
ObjectMapper mapper = JsonMapper.builder()
135+
.addModule(new Hibernate6Module()));
136+
.build();
137+
138+
// or, for Hibernate 7.x
139+
ObjectMapper mapper = JsonMapper.builder()
140+
.addModule(new Hibernate7Module()));
141+
.build();
108142
```
109143

110144
after which functionality is available for all normal Jackson operations.

hibernate7/pom.xml

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<!-- This module was also published with a richer model, Gradle metadata, -->
3+
<!-- which should be used instead. Do not delete the following line which -->
4+
<!-- is to indicate to Gradle or any Gradle module metadata file consumer -->
5+
<!-- that they should prefer consuming it instead. -->
6+
<!-- do_not_remove: published-with-gradle-metadata -->
7+
<modelVersion>4.0.0</modelVersion>
8+
<parent>
9+
<groupId>com.fasterxml.jackson.datatype</groupId>
10+
<artifactId>jackson-datatype-hibernate-parent</artifactId>
11+
<version>2.20.0-SNAPSHOT</version>
12+
</parent>
13+
<artifactId>jackson-datatype-hibernate7</artifactId>
14+
<name>Jackson-datatype-hibernate7</name>
15+
<packaging>bundle</packaging>
16+
<description>Add-on module for Jackson (https://github.com/FasterXML/jackson) to support
17+
Hibernate (https://hibernate.org/) version 7.x with Jakarta data types.
18+
</description>
19+
<url>https://github.com/FasterXML/jackson-datatype-hibernate</url>
20+
<properties>
21+
<!-- Generate PackageVersion.java into this directory. -->
22+
<packageVersion.dir>com/fasterxml/jackson/datatype/hibernate7</packageVersion.dir>
23+
<packageVersion.package>${project.groupId}.hibernate7</packageVersion.package>
24+
<!-- Hibernate with Jakarta Persistence 3.0 -->
25+
<hibernate.version>7.0.3.Final</hibernate.version>
26+
<osgi.export>${project.groupId}.hibernate7</osgi.export>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>jakarta.transaction</groupId>
32+
<artifactId>jakarta.transaction-api</artifactId>
33+
<version>2.0.1</version>
34+
</dependency>
35+
36+
<!-- what would be the best scope to use here? -->
37+
<dependency>
38+
<groupId>org.hibernate.orm</groupId>
39+
<artifactId>hibernate-core</artifactId>
40+
<version>${hibernate.version}</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
44+
<!-- and for some contributed tests Mockito -->
45+
<dependency>
46+
<groupId>org.mockito</groupId>
47+
<artifactId>mockito-core</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.slf4j</groupId>
52+
<artifactId>slf4j-log4j12</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>log4j</groupId>
57+
<artifactId>log4j</artifactId>
58+
<scope>test</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.h2database</groupId>
62+
<artifactId>h2</artifactId>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>jakarta.xml.bind</groupId>
67+
<artifactId>jakarta.xml.bind-api</artifactId>
68+
<version>3.0.1</version>
69+
<scope>test</scope>
70+
</dependency>
71+
</dependencies>
72+
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-compiler-plugin</artifactId>
78+
<configuration>
79+
<source>17</source>
80+
<target>17</target>
81+
</configuration>
82+
</plugin>
83+
<plugin>
84+
<!-- Inherited from oss-base. Generate PackageVersion.java.-->
85+
<groupId>com.google.code.maven-replacer-plugin</groupId>
86+
<artifactId>replacer</artifactId>
87+
</plugin>
88+
<!-- 29-Apr-2025, tatu: SBOM generation [JSTEP-14] -->
89+
<plugin>
90+
<groupId>org.cyclonedx</groupId>
91+
<artifactId>cyclonedx-maven-plugin</artifactId>
92+
</plugin>
93+
<plugin>
94+
<groupId>org.moditect</groupId>
95+
<artifactId>moditect-maven-plugin</artifactId>
96+
<inherited>true</inherited>
97+
<configuration>
98+
<jvmVersion>11</jvmVersion>
99+
</configuration>
100+
</plugin>
101+
<!-- 05-Jul-2020, tatu: Add generation of Gradle Module Metadata -->
102+
<!-- 28-Feb-2025, jjohannes: Apply plugin last as it has to be the last of all 'package phase' plugins -->
103+
<plugin>
104+
<groupId>org.gradlex</groupId>
105+
<artifactId>gradle-module-metadata-maven-plugin</artifactId>
106+
</plugin>
107+
</plugins>
108+
</build>
109+
</project>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.fasterxml.jackson.datatype.hibernate7;
2+
3+
import com.fasterxml.jackson.core.Version;
4+
import com.fasterxml.jackson.databind.AnnotationIntrospector;
5+
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
6+
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
7+
import jakarta.persistence.Transient;
8+
9+
/**
10+
* Simple {@link AnnotationIntrospector} that adds support for using
11+
* {@link Transient} to denote ignorable fields (alongside with Jackson
12+
* and/or JAXB annotations).
13+
*/
14+
public class Hibernate7AnnotationIntrospector extends AnnotationIntrospector
15+
{
16+
private static final long serialVersionUID = 1L;
17+
18+
/**
19+
* Whether we should check for existence of @Transient or not.
20+
* Default value is 'true'.
21+
*/
22+
protected boolean _cfgCheckTransient = true;
23+
24+
/*
25+
/**********************************************************************
26+
/* Construction, configuration
27+
/**********************************************************************
28+
*/
29+
30+
public Hibernate7AnnotationIntrospector() { }
31+
32+
/**
33+
* Method to call to specify whether @Transient annotation is to be
34+
* supported; if false, will be ignored, if true, will be used to
35+
* detect "ignorable" properties.
36+
*/
37+
public Hibernate7AnnotationIntrospector setUseTransient(boolean state) {
38+
_cfgCheckTransient = state;
39+
return this;
40+
}
41+
42+
/**
43+
* @since 2.5
44+
*/
45+
public boolean doesUseTransient() {
46+
return _cfgCheckTransient;
47+
}
48+
49+
/*
50+
/**********************************************************************
51+
/* Standard method impl/overrides
52+
/**********************************************************************
53+
*/
54+
55+
@Override
56+
public Version version() {
57+
return PackageVersion.VERSION;
58+
}
59+
60+
/*
61+
/**********************************************************************
62+
/* Annotation introspection methods
63+
/**********************************************************************
64+
*/
65+
66+
@Override
67+
public boolean hasIgnoreMarker(AnnotatedMember m) {
68+
return _cfgCheckTransient && m.hasAnnotation(Transient.class);
69+
}
70+
71+
@Override
72+
public Boolean isIgnorableType(AnnotatedClass ac)
73+
{
74+
/* 26-Dec-2015, tatu: To fix [datatype-hibernate#72], need to suppress handling
75+
* of `FieldHandled`. Not sure if it works without test (alas, none provided),
76+
* but will try our best -- problem is, if it'
77+
*/
78+
// 11-Feb-2016, tatu: As per [datatype-hibernate#86] must use indirection. Sigh.
79+
Class<?> handlerClass = FieldHandlerChecker.instance.getHandlerClass();
80+
if (handlerClass != null) {
81+
if (handlerClass.isAssignableFrom(ac.getAnnotated())) {
82+
return Boolean.TRUE;
83+
}
84+
}
85+
return null;
86+
}
87+
88+
/**
89+
* Helper class used to encapsulate detection of <code>FieldHandler</code>; class
90+
* that was part of Hibernate from 4.0 until 5.0, but removed from 5.1.
91+
*/
92+
final static class FieldHandlerChecker {
93+
private final static String FIELD_HANDLER_INTERFACE = "org.hibernate.bytecode.internal.javassist.FieldHandler";
94+
95+
private final Class<?> _handlerClass;
96+
97+
public final static FieldHandlerChecker instance = new FieldHandlerChecker();
98+
99+
public FieldHandlerChecker() {
100+
Class<?> cls = null;
101+
try {
102+
cls = Class.forName(FIELD_HANDLER_INTERFACE);
103+
} catch (Throwable t) { }
104+
_handlerClass = cls;
105+
}
106+
107+
public Class<?> getHandlerClass() {
108+
return _handlerClass;
109+
}
110+
}
111+
}

0 commit comments

Comments
 (0)