Skip to content

Commit

Permalink
New major version to work with hibernate 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas de Vos committed Oct 5, 2015
1 parent 19c31d1 commit 451b7db
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 148 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,10 @@ Components
* populator-excel (excel driven database populator)
* utils (common utility library, used by other components)
* sample (demonstrates all above described functionality in a simple project)

Release notes
-------------
* Version 3.0.0
- Java version below 1.8 not supported anymore
- Hibernate version below 5.0 not supported anymore

2 changes: 1 addition & 1 deletion jarb-constraints/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.jarbframework</groupId>
<artifactId>jarb-parent</artifactId>
<version>2.4.3-SNAPSHOT</version>
<version>3.0.0</version>
</parent>

<artifactId>jarb-constraints</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.jarbframework.init.migrate.MigratingDataSource;
import org.jarbframework.init.migrate.liquibase.LiquibaseMigrator;
import org.jarbframework.utils.orm.hibernate.ConventionNamingStrategy;
import org.jarbframework.utils.orm.hibernate.ConventionImplicitNamingStrategy;
import org.jarbframework.utils.orm.hibernate.ConventionPhysicalNamingStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
Expand Down Expand Up @@ -47,7 +48,8 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
entityManagerFactoryBean.setPackagesToScan("org.jarbframework.constraint");

Map<String, Object> jpaProperties = new HashMap<String, Object>();
jpaProperties.put("hibernate.ejb.naming_strategy", ConventionNamingStrategy.class.getName());
jpaProperties.put("hibernate.implicit_naming_strategy", ConventionImplicitNamingStrategy.class.getName());
jpaProperties.put("hibernate.physical_naming_strategy", ConventionPhysicalNamingStrategy.class.getName());
jpaProperties.put("hibernate.dialect", hibernateDialect);
jpaProperties.put("javax.persistence.validation.factory", validator());

Expand Down
5 changes: 3 additions & 2 deletions jarb-constraints/src/test/resources/application-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
<property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider" />
<property name="packagesToScan" value="org.jarbframework.constraint.domain" />
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.dialect" value-ref="hibernateDialect"/>
<entry key="hibernate.ejb.naming_strategy" value="org.jarbframework.utils.orm.hibernate.ConventionNamingStrategy"/>
<entry key="hibernate.implicit_naming_strategy" value="org.jarbframework.utils.orm.hibernate.ConventionImplicitNamingStrategy"/>
<entry key="hibernate.physical_naming_strategy" value="org.jarbframework.utils.orm.hibernate.ConventionPhysicalNamingStrategy"/>
<entry key="javax.persistence.validation.factory" value-ref="validator"/>
</map>
</property>
Expand Down
2 changes: 1 addition & 1 deletion jarb-init/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.jarbframework</groupId>
<artifactId>jarb-parent</artifactId>
<version>2.4.3-SNAPSHOT</version>
<version>3.0.0</version>
</parent>

<artifactId>jarb-init</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion jarb-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.jarbframework</groupId>
<artifactId>jarb-parent</artifactId>
<version>2.4.3-SNAPSHOT</version>
<version>3.0.0</version>
</parent>

<artifactId>jarb-sample</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import org.jarbframework.init.LocalHsqlDatabaseConfig;
import org.jarbframework.init.populate.PopulatingApplicationListenerBuilder.DatabasePopulateAppender;
import org.jarbframework.sample.populator.PostPopulator;
import org.jarbframework.utils.orm.hibernate.ConventionNamingStrategy;
import org.jarbframework.utils.orm.hibernate.ConventionImplicitNamingStrategy;
import org.jarbframework.utils.orm.hibernate.ConventionPhysicalNamingStrategy;
import org.jarbframework.utils.orm.hibernate.dialect.ImprovedHsqlDialect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
Expand Down Expand Up @@ -55,7 +56,8 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
entityManagerFactoryBean.setPackagesToScan("org.jarbframework.sample");

Map<String, Object> jpaProperties = new HashMap<String, Object>();
jpaProperties.put("hibernate.ejb.naming_strategy", ConventionNamingStrategy.class.getName());
jpaProperties.put("hibernate.implicit_naming_strategy", ConventionImplicitNamingStrategy.class.getName());
jpaProperties.put("hibernate.physical_naming_strategy", ConventionPhysicalNamingStrategy.class.getName());
jpaProperties.put("hibernate.dialect", ImprovedHsqlDialect.class.getName());
jpaProperties.put("hibernate.hbm2ddl.auto", "validate");
jpaProperties.put("hibernate.jdbc.use_get_generated_keys", true);
Expand Down
2 changes: 1 addition & 1 deletion jarb-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.jarbframework</groupId>
<artifactId>jarb-parent</artifactId>
<version>2.4.3-SNAPSHOT</version>
<version>3.0.0</version>
</parent>

<artifactId>jarb-utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jarbframework.utils.orm.hibernate;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.ImplicitForeignKeyNameSource;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl;

/**
* Naming strategy that describes our mapping between classes and tables.
*
* @author Jeroen van Schagen
*/
public class ConventionImplicitNamingStrategy extends ImplicitNamingStrategyJpaCompliantImpl {

private static final String FOREIGN_KEY_SUFFIX = "_id";

/**
* {@inheritDoc}
*/
@Override
public Identifier determineForeignKeyName(ImplicitForeignKeyNameSource source) {
Identifier identifier = super.determineForeignKeyName(source);
return Identifier.toIdentifier(identifier.getText() + FOREIGN_KEY_SUFFIX, identifier.isQuoted());
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jarbframework.utils.orm.hibernate;

import static org.hibernate.boot.model.naming.Identifier.toIdentifier;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

/**
* Physical naming strategy for generic usage.
*
* @author Bas de Vos
*/
public class ConventionPhysicalNamingStrategy extends PhysicalNamingStrategyStandardImpl {

/**
* {@inheritDoc}
*/
@Override
public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
return toIdentifier(addUnderscores(name.getText()), name.isQuoted());
}

/**
* {@inheritDoc}
*/
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
return toIdentifier(addUnderscores(name.getText()), name.isQuoted());
}

protected static String addUnderscores(String name) {
StringBuilder buffer = new StringBuilder(name.replace('.', '_'));
for (int index = 1; index < buffer.length() - 1; index++) {
if (isSeparator(buffer, index)) {
buffer.insert(index++, '_');
}
}
return buffer.toString().toLowerCase();
}

private static boolean isSeparator(StringBuilder buffer, int index) {
char previous = buffer.charAt(index - 1);
char current = buffer.charAt(index);
return Character.isLowerCase(previous) && Character.isUpperCase(current);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public IdentifierGeneratingInsert prepareIdentifierGeneratingInsert() {

@Override
protected PreparedStatement prepare(String insertSQL, SessionImplementor session) throws SQLException {
return session.getTransactionCoordinator().getJdbcCoordinator().getStatementPreparer().prepareStatement(insertSQL, new String[] { keyColumnName });
return session.getJdbcCoordinator().getStatementPreparer().prepareStatement(insertSQL, new String[] { keyColumnName });
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.spi.SessionFactoryImplementor;

public class HibernateUtils {

Expand All @@ -13,11 +14,10 @@ public class HibernateUtils {
* @param entityManagerFactory the entity manager factory
* @return the data source
*/
@SuppressWarnings("deprecation")
public static DataSource getDataSource(EntityManagerFactory entityManagerFactory) {
try {
SessionFactory sessionFactory = ((org.hibernate.jpa.HibernateEntityManagerFactory) entityManagerFactory).getSessionFactory();
return ((org.hibernate.engine.spi.SessionFactoryImplementor) sessionFactory).getConnectionProvider().unwrap(DataSource.class);
SessionFactoryImplementor sessionFactory = ((org.hibernate.jpa.HibernateEntityManagerFactory) entityManagerFactory).getSessionFactory();
return sessionFactory.getServiceRegistry().getService(ConnectionProvider.class).unwrap(DataSource.class);
} catch (RuntimeException rte) {
throw new IllegalStateException("Could not extract data source from entity manager factory.", rte);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.hibernate.SessionFactory;
import org.hibernate.StatelessSession;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.engine.transaction.spi.TransactionContext;
import org.hibernate.internal.StatelessSessionImpl;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.jarbframework.utils.Asserts;
import org.springframework.aop.framework.ProxyFactory;
Expand Down Expand Up @@ -107,7 +107,11 @@ private boolean isFlushCalled(MethodInvocation invocation) {
}

private static void flush(StatelessSession session) {
((TransactionContext) session).managedFlush();
if (session instanceof FlushableStatelessSession) {
((FlushableStatelessSession) session).flush();
} else if (session instanceof StatelessSessionImpl) {
((StatelessSessionImpl) session).flush();
}
}

private StatelessSession getCurrentSession() {
Expand All @@ -130,7 +134,7 @@ private StatelessSession openNewStatelessSession() {
private Connection getPhysicalConnection() {
EntityManager entityManager = EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
SessionImplementor sessionImplementor = (SessionImplementor) entityManager.getDelegate();
return sessionImplementor.getTransactionCoordinator().getJdbcCoordinator().getLogicalConnection().getConnection();
return sessionImplementor.getJdbcCoordinator().getLogicalConnection().getPhysicalConnection();
}

private void bindWithTransaction(StatelessSession statelessSession) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import javax.sql.DataSource;

import org.hibernate.jpa.HibernatePersistenceProvider;
import org.jarbframework.utils.orm.hibernate.ConventionNamingStrategy;
import org.jarbframework.utils.orm.hibernate.ConventionImplicitNamingStrategy;
import org.jarbframework.utils.orm.hibernate.ConventionPhysicalNamingStrategy;
import org.jarbframework.utils.orm.hibernate.dialect.ImprovedHsqlDialect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -38,7 +39,8 @@ public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
entityManagerFactoryBean.setPackagesToScan("org.jarbframework.utils.domain");

Map<String, Object> jpaProperties = new HashMap<String, Object>();
jpaProperties.put("hibernate.ejb.naming_strategy", ConventionNamingStrategy.class.getName());
jpaProperties.put("hibernate.implicit_naming_strategy", ConventionImplicitNamingStrategy.class.getName());
jpaProperties.put("hibernate.physical_naming_strategy", ConventionPhysicalNamingStrategy.class.getName());
jpaProperties.put("hibernate.dialect", ImprovedHsqlDialect.class.getName());
jpaProperties.put("hibernate.hbm2ddl.auto", "validate");

Expand Down

This file was deleted.

Loading

0 comments on commit 451b7db

Please sign in to comment.