Skip to content

Commit 2611619

Browse files
timis1nlor6
authored
JAVA-20163 Upgrade hibernate specific modules to JDK 11 (eugenp#13817)
* JAVA-20163 Migration hibernate-annotations * JAVA-20163 Migrate hibernate-queries * JAVA-20163 Migrating hibernate-mapping * JAVA-20163 rename reserved keywords, update inheritance example * JAVA-20163 Migrate hibernate-ogm module to the jdk 8 because hibernate-ogm doesn't support jakarta API * JAVA-20163 Migrate hibernate-enterprise module * JAVA-20163 Add update to HibernateExceptionUnitTest#whenQueryExecutedWithUnmappedEntity_thenMappingException * JAVA-20163 Set explicit version for hibernate 6.1.7.Final in the hibernate-queries module * JAVA-20163 Fix failed test with port that already exists (giving another port 8088) * JAVA-20163 Fix other location after changing the port * JAVA-20163 Remove duplicate Unit Test --------- Co-authored-by: n <[email protected]> Co-authored-by: Loredana Crusoveanu <[email protected]>
1 parent 0ec6aa5 commit 2611619

File tree

117 files changed

+810
-781
lines changed

Some content is hidden

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

117 files changed

+810
-781
lines changed

persistence-modules/hibernate-annotations/pom.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,18 @@
7676
<version>${org.springframework.version}</version>
7777
<scope>test</scope>
7878
</dependency>
79+
<dependency>
80+
<groupId>io.hypersistence</groupId>
81+
<artifactId>hypersistence-utils-hibernate-60</artifactId>
82+
<version>3.3.1</version>
83+
</dependency>
7984
</dependencies>
8085

8186
<properties>
8287
<!-- Spring -->
83-
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
84-
<org.springframework.data.version>1.10.6.RELEASE</org.springframework.data.version>
85-
<hibernate-core.version>5.6.7.Final</hibernate-core.version>
88+
<org.springframework.version>6.0.6</org.springframework.version>
89+
<org.springframework.data.version>3.0.3</org.springframework.data.version>
90+
<hibernate-core.version>6.1.7.Final</hibernate-core.version>
8691
<maven.deploy.skip>true</maven.deploy.skip>
8792
<tomcat-dbcp.version>9.0.0.M26</tomcat-dbcp.version>
8893
</properties>

persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/creationupdatetimestamp/model/Book.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import java.time.Instant;
44

5-
import javax.persistence.Entity;
6-
import javax.persistence.GeneratedValue;
7-
import javax.persistence.Id;
5+
import jakarta.persistence.Entity;
6+
import jakarta.persistence.GeneratedValue;
7+
import jakarta.persistence.Id;
88

99
import org.hibernate.annotations.CreationTimestamp;
1010
import org.hibernate.annotations.UpdateTimestamp;
Lines changed: 60 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.baeldung.hibernate.customtypes;
22

33
import org.hibernate.HibernateException;
4+
import org.hibernate.engine.spi.SessionFactoryImplementor;
45
import org.hibernate.engine.spi.SharedSessionContractImplementor;
5-
import org.hibernate.type.IntegerType;
6-
import org.hibernate.type.StringType;
7-
import org.hibernate.type.Type;
6+
import org.hibernate.metamodel.spi.ValueAccess;
87
import org.hibernate.usertype.CompositeUserType;
8+
import org.hibernate.usertype.UserType;
99

1010
import java.io.Serializable;
1111
import java.sql.PreparedStatement;
@@ -14,74 +14,51 @@
1414
import java.sql.Types;
1515
import java.util.Objects;
1616

17-
public class AddressType implements CompositeUserType {
17+
public class AddressType implements CompositeUserType<Address>, UserType<Address> {
1818

1919
@Override
20-
public String[] getPropertyNames() {
21-
return new String[]{"addressLine1", "addressLine2",
22-
"city", "country", "zipcode"};
23-
}
24-
25-
@Override
26-
public Type[] getPropertyTypes() {
27-
return new Type[]{StringType.INSTANCE, StringType.INSTANCE,
28-
StringType.INSTANCE, StringType.INSTANCE, IntegerType.INSTANCE};
29-
}
30-
31-
@Override
32-
public Object getPropertyValue(Object component, int property) throws HibernateException {
33-
34-
Address empAdd = (Address) component;
20+
public Object getPropertyValue(Address component, int property) throws HibernateException {
3521

3622
switch (property) {
3723
case 0:
38-
return empAdd.getAddressLine1();
24+
return component.getAddressLine1();
3925
case 1:
40-
return empAdd.getAddressLine2();
26+
return component.getAddressLine2();
4127
case 2:
42-
return empAdd.getCity();
28+
return component.getCity();
4329
case 3:
44-
return empAdd.getCountry();
30+
return component.getCountry();
4531
case 4:
46-
return Integer.valueOf(empAdd.getZipCode());
32+
return component.getZipCode();
33+
default:
34+
throw new IllegalArgumentException(property +
35+
" is an invalid property index for class type " +
36+
component.getClass().getName());
4737
}
48-
49-
throw new IllegalArgumentException(property +
50-
" is an invalid property index for class type " +
51-
component.getClass().getName());
5238
}
5339

5440
@Override
55-
public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
56-
57-
Address empAdd = (Address) component;
58-
59-
switch (property) {
60-
case 0:
61-
empAdd.setAddressLine1((String) value);
62-
case 1:
63-
empAdd.setAddressLine2((String) value);
64-
case 2:
65-
empAdd.setCity((String) value);
66-
case 3:
67-
empAdd.setCountry((String) value);
68-
case 4:
69-
empAdd.setZipCode((Integer) value);
70-
}
41+
public Address instantiate(ValueAccess values, SessionFactoryImplementor sessionFactory) {
42+
return null;
43+
}
7144

72-
throw new IllegalArgumentException(property +
73-
" is an invalid property index for class type " +
74-
component.getClass().getName());
45+
@Override
46+
public Class<?> embeddable() {
47+
return Address.class;
48+
}
7549

50+
@Override
51+
public int getSqlType() {
52+
return Types.VARCHAR;
7653
}
7754

7855
@Override
79-
public Class returnedClass() {
56+
public Class<Address> returnedClass() {
8057
return Address.class;
8158
}
8259

8360
@Override
84-
public boolean equals(Object x, Object y) throws HibernateException {
61+
public boolean equals(Address x, Address y) {
8562
if (x == y)
8663
return true;
8764

@@ -92,57 +69,52 @@ public boolean equals(Object x, Object y) throws HibernateException {
9269
}
9370

9471
@Override
95-
public int hashCode(Object x) throws HibernateException {
72+
public int hashCode(Address x) {
9673
return x.hashCode();
9774
}
9875

9976
@Override
100-
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
101-
77+
public Address nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
10278
Address empAdd = new Address();
103-
empAdd.setAddressLine1(rs.getString(names[0]));
79+
empAdd.setAddressLine1(rs.getString(position));
10480

10581
if (rs.wasNull())
10682
return null;
10783

108-
empAdd.setAddressLine2(rs.getString(names[1]));
109-
empAdd.setCity(rs.getString(names[2]));
110-
empAdd.setCountry(rs.getString(names[3]));
111-
empAdd.setZipCode(rs.getInt(names[4]));
84+
empAdd.setAddressLine2(rs.getString(position));
85+
empAdd.setCity(rs.getString(position));
86+
empAdd.setCountry(rs.getString(position));
87+
empAdd.setZipCode(rs.getInt(position));
11288

11389
return empAdd;
11490
}
11591

11692
@Override
117-
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
118-
93+
public void nullSafeSet(PreparedStatement st, Address value, int index, SharedSessionContractImplementor session) throws SQLException {
11994
if (Objects.isNull(value))
12095
st.setNull(index, Types.VARCHAR);
12196
else {
12297

123-
Address empAdd = (Address) value;
124-
st.setString(index, empAdd.getAddressLine1());
125-
st.setString(index + 1, empAdd.getAddressLine2());
126-
st.setString(index + 2, empAdd.getCity());
127-
st.setString(index + 3, empAdd.getCountry());
128-
st.setInt(index + 4, empAdd.getZipCode());
98+
st.setString(index, value.getAddressLine1());
99+
st.setString(index + 1, value.getAddressLine2());
100+
st.setString(index + 2, value.getCity());
101+
st.setString(index + 3, value.getCountry());
102+
st.setInt(index + 4, value.getZipCode());
129103
}
130104
}
131105

132106
@Override
133-
public Object deepCopy(Object value) throws HibernateException {
134-
107+
public Address deepCopy(Address value) {
135108
if (Objects.isNull(value))
136109
return null;
137110

138-
Address oldEmpAdd = (Address) value;
139111
Address newEmpAdd = new Address();
140112

141-
newEmpAdd.setAddressLine1(oldEmpAdd.getAddressLine1());
142-
newEmpAdd.setAddressLine2(oldEmpAdd.getAddressLine2());
143-
newEmpAdd.setCity(oldEmpAdd.getCity());
144-
newEmpAdd.setCountry(oldEmpAdd.getCountry());
145-
newEmpAdd.setZipCode(oldEmpAdd.getZipCode());
113+
newEmpAdd.setAddressLine1(value.getAddressLine1());
114+
newEmpAdd.setAddressLine2(value.getAddressLine2());
115+
newEmpAdd.setCity(value.getCity());
116+
newEmpAdd.setCountry(value.getCountry());
117+
newEmpAdd.setZipCode(value.getZipCode());
146118

147119
return newEmpAdd;
148120
}
@@ -153,17 +125,27 @@ public boolean isMutable() {
153125
}
154126

155127
@Override
156-
public Serializable disassemble(Object value, SharedSessionContractImplementor session) throws HibernateException {
128+
public Serializable disassemble(Address value) {
157129
return (Serializable) deepCopy(value);
158130
}
159131

160132
@Override
161-
public Object assemble(Serializable cached, SharedSessionContractImplementor session, Object owner) throws HibernateException {
162-
return deepCopy(cached);
133+
public Address assemble(Serializable cached, Object owner) {
134+
return deepCopy((Address) cached);
135+
}
136+
137+
@Override
138+
public Address replace(Address detached, Address managed, Object owner) {
139+
return detached;
140+
}
141+
142+
@Override
143+
public boolean isInstance(Object object, SessionFactoryImplementor sessionFactory) {
144+
return CompositeUserType.super.isInstance(object, sessionFactory);
163145
}
164146

165147
@Override
166-
public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner) throws HibernateException {
167-
return original;
148+
public boolean isSameClass(Object object, SessionFactoryImplementor sessionFactory) {
149+
return CompositeUserType.super.isSameClass(object, sessionFactory);
168150
}
169151
}

persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/LocalDateStringJavaDescriptor.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.baeldung.hibernate.customtypes;
22

3-
import org.hibernate.type.LocalDateType;
43
import org.hibernate.type.descriptor.WrapperOptions;
5-
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
64
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
7-
import org.hibernate.type.descriptor.java.MutabilityPlan;
85

96
import java.time.LocalDate;
7+
import java.time.format.DateTimeFormatter;
108

11-
public class LocalDateStringJavaDescriptor extends AbstractTypeDescriptor<LocalDate> {
9+
import io.hypersistence.utils.hibernate.type.array.internal.AbstractArrayTypeDescriptor;
10+
11+
public class LocalDateStringJavaDescriptor extends AbstractArrayTypeDescriptor<LocalDate> {
1212

1313
public static final LocalDateStringJavaDescriptor INSTANCE = new LocalDateStringJavaDescriptor();
1414

@@ -18,12 +18,12 @@ public LocalDateStringJavaDescriptor() {
1818

1919
@Override
2020
public String toString(LocalDate value) {
21-
return LocalDateType.FORMATTER.format(value);
21+
return DateTimeFormatter.ISO_LOCAL_DATE.format(value);
2222
}
2323

2424
@Override
25-
public LocalDate fromString(String string) {
26-
return LocalDate.from(LocalDateType.FORMATTER.parse(string));
25+
public LocalDate fromString(CharSequence string) {
26+
return LocalDate.from( DateTimeFormatter.ISO_LOCAL_DATE.parse(string));
2727
}
2828

2929
@Override
@@ -33,7 +33,7 @@ public <X> X unwrap(LocalDate value, Class<X> type, WrapperOptions options) {
3333
return null;
3434

3535
if (String.class.isAssignableFrom(type))
36-
return (X) LocalDateType.FORMATTER.format(value);
36+
return (X) DateTimeFormatter.ISO_LOCAL_DATE.format(value);
3737

3838
throw unknownUnwrap(type);
3939
}
@@ -44,7 +44,7 @@ public <X> LocalDate wrap(X value, WrapperOptions options) {
4444
return null;
4545

4646
if(String.class.isInstance(value))
47-
return LocalDate.from(LocalDateType.FORMATTER.parse((CharSequence) value));
47+
return LocalDate.from( DateTimeFormatter.ISO_LOCAL_DATE.parse((CharSequence) value));
4848

4949
throw unknownWrap(value.getClass());
5050
}

persistence-modules/hibernate-annotations/src/main/java/com/baeldung/hibernate/customtypes/LocalDateStringType.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,29 @@
22

33
import org.hibernate.dialect.Dialect;
44
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
5-
import org.hibernate.type.DiscriminatorType;
6-
import org.hibernate.type.descriptor.java.LocalDateJavaDescriptor;
7-
import org.hibernate.type.descriptor.sql.VarcharTypeDescriptor;
5+
import org.hibernate.type.descriptor.jdbc.VarcharJdbcType;
86

97
import java.time.LocalDate;
108

11-
public class LocalDateStringType extends AbstractSingleColumnStandardBasicType<LocalDate> implements DiscriminatorType<LocalDate> {
9+
public class LocalDateStringType extends AbstractSingleColumnStandardBasicType<LocalDate> {
1210

1311
public static final LocalDateStringType INSTANCE = new LocalDateStringType();
1412

1513
public LocalDateStringType() {
16-
super(VarcharTypeDescriptor.INSTANCE, LocalDateStringJavaDescriptor.INSTANCE);
14+
super(VarcharJdbcType.INSTANCE, LocalDateStringJavaDescriptor.INSTANCE);
1715
}
1816

1917
@Override
2018
public String getName() {
2119
return "LocalDateString";
2220
}
2321

24-
@Override
25-
public LocalDate stringToObject(String xml) throws Exception {
22+
public LocalDate stringToObject(String xml) {
2623
return fromString(xml);
2724
}
2825

29-
@Override
30-
public String objectToSQLString(LocalDate value, Dialect dialect) throws Exception {
31-
return '\'' + toString(value) + '\'';
26+
public String objectToSQLString(LocalDate value, Dialect dialect) {
27+
return '\'' + LocalDateStringJavaDescriptor.INSTANCE.toString(value) + '\'';
3228
}
3329

3430
}

0 commit comments

Comments
 (0)