Skip to content

Commit

Permalink
Spring hibernate mongodb (#432)
Browse files Browse the repository at this point in the history
* hibernate 5, spring 5, mongo driver 4

* migration to new mongodb java api to support all mongodb versions
  • Loading branch information
ymarcon authored Aug 25, 2022
1 parent 62fb797 commit 44763b4
Show file tree
Hide file tree
Showing 49 changed files with 534 additions and 533 deletions.
2 changes: 1 addition & 1 deletion magma-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>org.obiba.magma</groupId>
<artifactId>magma</artifactId>
<version>3.1-SNAPSHOT</version>
<version>4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion magma-beans/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>org.obiba.magma</groupId>
<artifactId>magma</artifactId>
<version>3.1-SNAPSHOT</version>
<version>4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion magma-crypt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>org.obiba.magma</groupId>
<artifactId>magma</artifactId>
<version>3.1-SNAPSHOT</version>
<version>4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion magma-data-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>org.obiba.magma</groupId>
<artifactId>magma</artifactId>
<version>3.1-SNAPSHOT</version>
<version>4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion magma-datasource-csv/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>org.obiba.magma</groupId>
<artifactId>magma</artifactId>
<version>3.1-SNAPSHOT</version>
<version>4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion magma-datasource-excel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>org.obiba.magma</groupId>
<artifactId>magma</artifactId>
<version>3.1-SNAPSHOT</version>
<version>4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion magma-datasource-fs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>org.obiba.magma</groupId>
<artifactId>magma</artifactId>
<version>3.1-SNAPSHOT</version>
<version>4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion magma-datasource-hibernate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<groupId>org.obiba.magma</groupId>
<artifactId>magma</artifactId>
<version>3.1-SNAPSHOT</version>
<version>4.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ public ValueTableWriter createWriter(@NotNull String tableName, @NotNull String
} else {
// Create table transaction.
ValueTableState valueTableState = new ValueTableState(tableName, entityType, getDatasourceState());
getSessionFactory().getCurrentSession().save(valueTableState);
getSessionFactory().getCurrentSession().refresh(valueTableState); //OPAL-2635
Session session = getSessionFactory().getCurrentSession();
session.save(valueTableState);
session.flush();
session.refresh(valueTableState); //OPAL-2635
// Create a transaction for a new table
valueTableTransaction = newTableTransaction(new HibernateValueTable(this, valueTableState), true);
}
Expand Down Expand Up @@ -227,8 +229,10 @@ protected void onInitialise() {
// If datasource not persisted, create the persisted DatasourceState.
if(datasourceState == null) {
datasourceState = new DatasourceState(getName());
sessionFactory.getCurrentSession().save(datasourceState);
sessionFactory.getCurrentSession().refresh(datasourceState); //OPAL-2635
Session session = sessionFactory.getCurrentSession();
session.save(datasourceState);
session.flush();
session.refresh(datasourceState); //OPAL-2635
} else {
// If already persisted, load the persisted attributes for that datasource.
for(AttributeState attribute : datasourceState.getAttributes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class HibernateValueTableWriter implements ValueTableWriter {

session = valueTable.getDatasource().getSessionFactory().getCurrentSession();
valueSourceFactory = new HibernateVariableValueSourceFactory(valueTable);
if(session.getFlushMode() != FlushMode.MANUAL) {
session.setFlushMode(FlushMode.MANUAL);
if(session.getHibernateFlushMode() != FlushMode.MANUAL) {
session.setHibernateFlushMode(FlushMode.MANUAL);
}

context = valueTable.createContext();
Expand Down Expand Up @@ -261,6 +261,7 @@ private HibernateValueSetWriter(@NotNull VariableEntity entity) {
state = new ValueSetState(valueTable.getValueTableState(), variableEntityState);
// Persists the ValueSet
session.save(state);
session.flush();
session.refresh(state); //OPAL-2635
values = Maps.newHashMap();
isNewValueSet = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,41 @@
*/
package org.obiba.magma.datasource.hibernate.cfg;

import org.hibernate.dialect.Database;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.MySQL5Dialect;
import org.hibernate.engine.jdbc.dialect.internal.StandardDialectResolver;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;

/**
* Ensures usage of InnoDB for MySQL databases and uses custom dialect for HSQLDB, otherwise, fallback to default
* behavior.
* <p/>
* This class is instantiated by Hibernate itself through the {@code hibernate.properties} file.
*/
public class MagmaDialectResolver extends StandardDialectResolver {
public class MagmaDialectResolver implements DialectResolver {

private static final long serialVersionUID = 6167226108895659666L;

@Override
@SuppressWarnings("ChainOfInstanceofChecks")
public Dialect resolveDialect(DialectResolutionInfo info) {
Dialect dialect = super.resolveDialect(info);
Dialect dialect = resolveStandardDialect(info);
if(dialect instanceof MySQL5Dialect) return new MySQL5InnoDbUtf8Dialect();
if(dialect instanceof HSQLDialect) return new MagmaHSQLDialect();
return dialect;
}

private Dialect resolveStandardDialect(DialectResolutionInfo info) {
for ( Database database : Database.values() ) {
Dialect dialect = database.resolveDialect( info );
if ( dialect != null ) {
return dialect;
}
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public VariableEntityState marshal(VariableEntity variableEntity, HibernateMarsh
if(variableEntityState == null) {
variableEntityState = new VariableEntityState(variableEntity.getIdentifier(), variableEntity.getType());
context.getSessionFactory().getCurrentSession().save(variableEntityState);
context.getSessionFactory().getCurrentSession().refresh(variableEntityState); //OPAL-2635
//context.getSessionFactory().getCurrentSession().refresh(variableEntityState); //OPAL-2635
}

return variableEntityState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@

package org.obiba.magma.datasource.hibernate.support;

import java.util.Properties;

import javax.annotation.Nullable;
import javax.sql.DataSource;
import javax.validation.constraints.NotNull;

import org.hibernate.SessionFactory;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.ImprovedNamingStrategy;
import org.obiba.core.service.impl.hibernate.PhysicalNamingStrategyImpl;
import org.obiba.magma.Initialisable;
import org.obiba.magma.MagmaRuntimeException;
import org.obiba.magma.datasource.hibernate.SessionFactoryProvider;
import org.obiba.magma.datasource.hibernate.cfg.HibernateConfigurationHelper;
import org.obiba.magma.datasource.hibernate.cfg.MagmaDialectResolver;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;

import javax.annotation.Nullable;
import javax.sql.DataSource;
import javax.validation.constraints.NotNull;
import java.util.Properties;

public class LocalSessionFactoryProvider implements SessionFactoryProvider, Initialisable {

Expand Down Expand Up @@ -53,7 +53,8 @@ public void initialise() {
// Set some reasonable defaults
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);
builder.addAnnotatedClasses(HibernateConfigurationHelper.getAnnotatedTypesAsArray());
builder.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
builder.setImplicitNamingStrategy(ImplicitNamingStrategyLegacyHbmImpl.INSTANCE);
builder.setPhysicalNamingStrategy(PhysicalNamingStrategyImpl.INSTANCE);
builder.setProperty(Environment.DIALECT, dialect);
builder.setProperty(Environment.HBM2DDL_AUTO, "update");
builder.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,24 @@

package org.obiba.magma.datasource.hibernate.type;

import java.io.StringReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;

import javax.annotation.Nullable;

import org.dom4j.Node;
import com.google.common.base.Strings;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.metamodel.relational.Size;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.AbstractType;
import org.obiba.magma.Value;
import org.obiba.magma.ValueType;

import com.google.common.base.Strings;
import javax.annotation.Nullable;
import java.io.StringReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;

/**
* A Hibernate Type for persisting {@code Value} instances. The strategy uses 3 columns:
Expand All @@ -54,28 +51,30 @@ public String getName() {
return "Value";
}

@Override
public boolean isDirty(Object old, Object current, boolean[] checkable, SessionImplementor session)
throws HibernateException {
return !old.equals(current);
}

@Override
public boolean isMutable() {
// Value instances are immutable
return false;
}

@Override
public Object nullSafeGet(ResultSet rs, String name, SessionImplementor session, Object owner)
throws HibernateException, SQLException {
throw new UnsupportedOperationException();
public Object replace(Object original, Object target, SharedSessionContractImplementor session, Object owner, Map copyCache) throws HibernateException {
return null;
}

@Override
public Class<?> getReturnedClass() {
return Value.class;
}

@Override
public boolean isDirty(Object oldState, Object currentState, boolean[] checkable, SharedSessionContractImplementor session) throws HibernateException {
return !oldState.equals(currentState);
}

@Nullable
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException {
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
String valueTypeName = rs.getString(names[0]);
// Even when the column is NOT NULL, a SELECT statement can return NULL (using a left join for example).
// When this column is null, we cannot construct a valid {@code Value} instance, so this method returns null
Expand All @@ -89,8 +88,12 @@ public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor sessi
}

@Override
public void nullSafeSet(PreparedStatement st, Object obj, int index, boolean[] settable, SessionImplementor session)
throws HibernateException, SQLException {
public Object nullSafeGet(ResultSet rs, String name, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException {
throw new UnsupportedOperationException();
}

@Override
public void nullSafeSet(PreparedStatement st, Object obj, int index, boolean[] settable, SharedSessionContractImplementor session) throws HibernateException, SQLException {
Value value = (Value) obj;

int offset = 0;
Expand All @@ -106,38 +109,14 @@ public void nullSafeSet(PreparedStatement st, Object obj, int index, boolean[] s
}

@Override
public void nullSafeSet(PreparedStatement st, Object obj, int index, SessionImplementor session)
throws HibernateException, SQLException {
public void nullSafeSet(PreparedStatement st, Object obj, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
Value value = (Value) obj;
st.setString(index, value.getValueType().getName());
st.setBoolean(index + 1, value.isSequence());
String stringValue = Strings.nullToEmpty(value.isNull() ? null : value.toString());
st.setClob(index + 2, new StringReader(stringValue), stringValue.length());
}

@Override
public Class<?> getReturnedClass() {
return Value.class;
}

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Object replace(Object original, Object target, SessionImplementor session, Object owner, Map copyCache)
throws HibernateException {
// It is safe to return the original parameter since Value instances are immutable
return original;
}

@Override
public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException {
throw new UnsupportedOperationException();
}

@Override
public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) throws HibernateException {
throw new UnsupportedOperationException();
}

@Override
public int[] sqlTypes(Mapping mapping) throws MappingException {
return new int[] { Types.VARCHAR, Types.BIT, Types.CLOB };
Expand Down
Loading

0 comments on commit 44763b4

Please sign in to comment.