Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to hibernate-6.4.4 #359

Merged
merged 9 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ out
.gradle
build
tmp
*.dump.sql
*.dump.sql
.kotlin/
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ These were not used by RePlay's users (could be reintroduced if needed).

#### Requirements

JDK 17 (as we are stuck at Hibernate 5.6 which does not support JDK versions beyond 17).
JDK 17. But since we are no longer stuck at Hibernate 5.6 (which does not support JDK versions beyond 17) this may soon change.


## Getting started
Expand Down Expand Up @@ -373,7 +373,11 @@ needs an additional `return new Ok()` with RePlay.
* While porting the controllers you will find some changes to the views (templates) are required too:
* In some cases the full package path needs to be provided, e.g.: `Play.configuration.getProperty("key")` becomes `play.Play.configuration.getProperty("key")`.
* Due to changed encrypting/signing of `CookieSessionStore` all active sessions are logged out when migrating from Play1 to RePlay.
This means that running the Play1 version of the app side-by-side with the RePlay version is not possible (all users get logged out all the time).
This means that running the Play1 version of the app side-by-side with the RePlay version is not possible (all users get logged out all the time).
* As of September 2024, Play1 brings Hibernate 5.6 while RePlay upgraded to 6.4. This may result in some problems:
* Hibernate 6.4 is stricter when it comes to mapping columns to properties. This results in a `NonUniqueDiscoveredSqlAliasException`
thrown when a column name occurs twice (e.g. the `id` column) when mapping the result of a query with joins to an entity.
* Hibernate changed the "generation strategy" for MySQL, hence you may want to implement `jpa.Model` yourself which is fully supported in RePlay.


## Licence
Expand Down
8 changes: 4 additions & 4 deletions framework/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ dependencies {
implementation('ch.qos.reload4j:reload4j:1.2.25')
implementation('org.ehcache:ehcache:3.10.8')
api('net.sf.oval:oval:3.2.1')
api('org.hibernate:hibernate-core-jakarta:5.6.15.Final')
// Hibernate 5.6 series is only compatible with persistence-api:3.0.0
api('jakarta.persistence:jakarta.persistence-api:3.0.0')
api('org.hibernate:hibernate-core:6.4.4.Final')
// Hibernate 6.2-6.5 are only compatible with Jakarta Persistence 3.1
api('jakarta.persistence:jakarta.persistence-api:3.1.0')
api('org.hibernate.common:hibernate-commons-annotations:7.0.1.Final')
implementation('org.hibernate:hibernate-ehcache:5.6.15.Final') {transitive = false}
implementation('org.hibernate:hibernate-jcache:6.4.4.Final') {transitive = false}
api("org.slf4j:slf4j-api:$slf4jVersion")
api("org.slf4j:slf4j-reload4j:$slf4jVersion")
api("org.slf4j:jul-to-slf4j:$slf4jVersion")
Expand Down
58 changes: 12 additions & 46 deletions framework/src/play/db/DB.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package play.db;

import jakarta.persistence.EntityManager;
import org.hibernate.Session;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.SessionImpl;
import org.hibernate.jpa.HibernateEntityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.db.jpa.JPA;
Expand Down Expand Up @@ -165,55 +167,19 @@ public static void close(String name) {
}
}

/**
* Open a connection for the current thread.
*
* @param name
* Name of the DB
* @return A valid SQL connection
*/
@Nonnull
public static Connection getConnection(String name) {
try {
if (JPA.isEnabled()) {
return ((SessionImpl) ((HibernateEntityManager) JPA.em(name)).getSession()).connection();
}

Connection localConnection = getLocalConnection(name);
if (localConnection != null) {
return localConnection;
}

// We have no connection
Connection connection = getDataSource(name).getConnection();
registerLocalConnection(name, connection);
return connection;
} catch (NullPointerException e) {
if (getDataSource(name) == null) {
throw new DatabaseException("No database found: '" + name + "'", e);
}
throw e;
} catch (SQLException e) {
throw new DatabaseException(e);
}
}

@Nonnull
public static Connection getConnection() {
return getConnection(DEFAULT);
}

public static void execute(String SQL) {
execute(DEFAULT, SQL);
}

public static void execute(String name, String SQL) {
try (Statement statement = getConnection(name).createStatement()) {
statement.execute(SQL);
}
catch (SQLException ex) {
throw new DatabaseException(ex);
}
public static void execute(String name, String sql) {
JPA.em(name).unwrap(Session.class).doWork(con -> {
try (Statement statement = con.createStatement()) {
statement.execute(sql);
}
catch (SQLException ex) {
throw new DatabaseException(ex);
}
});
}

/**
Expand Down
9 changes: 7 additions & 2 deletions framework/src/play/db/jpa/JPA.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package play.db.jpa;

import org.hibernate.Session;
import org.hibernate.jdbc.Work;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.InvocationContext;
Expand All @@ -8,6 +10,9 @@
import play.libs.SupplierWithException;

import jakarta.persistence.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -311,7 +316,7 @@ public static void closeTx(String name) {
// Be sure to set the connection is non-autoCommit mode as some driver will complain about COMMIT
// statement
try {
DB.getConnection(name).setAutoCommit(false);
manager.unwrap(Session.class).doWork(con -> con.setAutoCommit(false));
} catch (Exception e) {
logger.error("Why the driver complains here?", e);
}
Expand Down Expand Up @@ -339,7 +344,7 @@ public static void rollbackTx(String name) {
// Be sure to set the connection is non-autoCommit mode as some driver will complain about COMMIT
// statement
try {
DB.getConnection(name).setAutoCommit(false);
manager.unwrap(Session.class).doWork(con -> con.setAutoCommit(false));
} catch (Exception e) {
logger.error("Why the driver complains here?", e);
}
Expand Down
3 changes: 1 addition & 2 deletions framework/src/play/db/jpa/JPABase.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package play.db.jpa;

import org.hibernate.collection.internal.PersistentMap;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.collection.spi.PersistentMap;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.PersistenceContext;
import org.hibernate.exception.GenericJDBCException;
import org.hibernate.internal.SessionImpl;
Expand Down
6 changes: 3 additions & 3 deletions framework/src/play/db/jpa/JPAPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ protected EntityManagerFactory newEntityManagerFactory(String dbName, Configurat
protected PersistenceUnitInfoImpl persistenceUnitInfo(String dbName, Configuration dbConfig) {
final List<Class> managedClasses = entityClasses(dbName);
final Properties properties = properties(dbName, dbConfig);
properties.put(org.hibernate.jpa.AvailableSettings.LOADED_CLASSES, managedClasses);
properties.put(org.hibernate.jpa.AvailableSettings.FLUSH_MODE, MANUAL);
properties.put(AvailableSettings.LOADED_CLASSES, managedClasses);
properties.put(AvailableSettings.FLUSH_MODE, MANUAL);
return new PersistenceUnitInfoImpl(dbName,
managedClasses, mappingFiles(dbConfig), properties);
}
Expand Down Expand Up @@ -220,7 +220,7 @@ public static String getDefaultDialect(Configuration dbConfig, String driver) {
} else if ("com.mysql.jdbc.Driver".equals(driver)) {
return "org.hibernate.dialect.MySQLDialect";
} else if ("com.mysql.cj.jdbc.Driver".equals(driver)) {
return "org.hibernate.dialect.MySQL8Dialect";
return "org.hibernate.dialect.MySQLDialect";
} else if ("org.postgresql.Driver".equals(driver)) {
return "org.hibernate.dialect.PostgreSQLDialect";
} else if ("com.ibm.db2.jdbc.app.DB2Driver".equals(driver)) {
Expand Down
16 changes: 8 additions & 8 deletions framework/test/play/db/ConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ public void getPropertiesForDefaultTest() {
Play.configuration.put("jpa.dialect", "org.hibernate.dialect.PostgreSQLDialect");
Play.configuration.put("jpa.debugSQL", "true");
//hibernate
Play.configuration.put("hibernate.ejb.event.post-insert", "postInsert");
Play.configuration.put("hibernate.ejb.event.post-update", "postUpdate");
Play.configuration.put("hibernate.event.post-insert", "postInsert");
Play.configuration.put("hibernate.event.post-update", "postUpdate");
//org.hibernate
Play.configuration.put("org.hibernate.flushMode", "AUTO");
Play.configuration.put("hibernate.default_batch_fetch_size", "66");
Expand All @@ -287,8 +287,8 @@ public void getPropertiesForDefaultTest() {
assertThat(properties.get("jpa.dialect")).isEqualTo("org.hibernate.dialect.PostgreSQLDialect");
assertThat(properties.get("jpa.debugSQL")).isEqualTo("true");
//hibernate
assertThat(properties.get("hibernate.ejb.event.post-insert")).isEqualTo("postInsert");
assertThat(properties.get("hibernate.ejb.event.post-update")).isEqualTo("postUpdate");
assertThat(properties.get("hibernate.event.post-insert")).isEqualTo("postInsert");
assertThat(properties.get("hibernate.event.post-update")).isEqualTo("postUpdate");
//org.hibernate
assertThat(properties.get("org.hibernate.flushMode")).isEqualTo("AUTO");
assertThat(properties.get("hibernate.default_batch_fetch_size")).isEqualTo("66");
Expand All @@ -315,8 +315,8 @@ public void getPropertiesForDBTest() {
Play.configuration.put("jpa.test.ddl", "update");
Play.configuration.put("jpa.test.debugSQL", "true");
//hibernate
Play.configuration.put("hibernate.test.ejb.event.post-insert", "postInsert");
Play.configuration.put("hibernate.test.ejb.event.post-update", "postUpdate");
Play.configuration.put("hibernate.test.event.post-insert", "postInsert");
Play.configuration.put("hibernate.test.event.post-update", "postUpdate");
//org.hibernate
Play.configuration.put("org.hibernate.test.flushMode", "AUTO");

Expand All @@ -339,8 +339,8 @@ public void getPropertiesForDBTest() {
assertThat(properties.get("jpa.dialect")).isEqualTo("org.hibernate.dialect.PostgreSQLDialect");
assertThat(properties.get("jpa.debugSQL")).isEqualTo("true");
//hibernate
assertThat(properties.get("hibernate.ejb.event.post-insert")).isEqualTo("postInsert");
assertThat(properties.get("hibernate.ejb.event.post-update")).isEqualTo("postUpdate");
assertThat(properties.get("hibernate.event.post-insert")).isEqualTo("postInsert");
assertThat(properties.get("hibernate.event.post-update")).isEqualTo("postUpdate");
//org.hibernate
assertThat(properties.get("org.hibernate.flushMode")).isEqualTo("AUTO");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">

<changeSet id="202304171942" author="Andrei">
<createSequence sequenceName="hibernate_sequence" startValue="1000"/>
<createSequence sequenceName="Pet_SEQ" startValue="1000" incrementBy="50"/>
</changeSet>

<changeSet id="202304171943" author="Andrei">
Expand Down
Loading