3

Is there way to set the dynamic-insert attribute globally with Hibernate (so it would be the default for all entities) ?

Alex
  • 1,041
  • 3
  • 14
  • 32

3 Answers3

3

in NHibernate it is

foreach (var clazz in config.ClassMappings)
{
    clazz.DynamicInsert = true;
}

i don't know the exact syntax in hibernate.

for (PersistentClass clazz : configuration.ClassMappings)
{
    clazz.setDynamicInsert(true);
}
Firo
  • 30,626
  • 4
  • 55
  • 94
  • Hmm didn't see anything that reminds this method in `ClassMetaData`, but there is a `PersistentClass.setDynamicInsert(boolean)` method. Will have to see if it helps me somehow – Alex Feb 08 '12 at 17:02
1

For Java Hibernate, the correct answer is:

Iterator mappingClasses = config.getClassMappings();
while(mappingClasses.hasNext()) {
   PersistentClass clazz = (PersistentClass) mappingClasses.next();
   clazz.setDynamicInsert(true);
   clazz.setDynamicUpdate(true);
}

Keep in mind that you have to build the mappings before you attempt to iterate over them. Otherwise config.getClassMappings() will return empty iterator.

config.buildMappings();
Henryk Konsek
  • 9,016
  • 5
  • 32
  • 41
0

You can write a simple integrator that reads the persistence.xml and modifies hibernates boot metadata:

import org.hibernate.boot.Metadata;
import org.hibernate.engine.config.spi.ConfigurationService;
import org.hibernate.engine.config.spi.StandardConverters;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.integrator.spi.Integrator;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.service.spi.SessionFactoryServiceRegistry;
import org.immutables.metainf.Metainf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Metainf.Service(Integrator.class)
public class DynamicUpdateIntegrator implements Integrator {
  private static final String SETTING = "hibernate.dynamic_update";
  private final Logger logger = LoggerFactory.getLogger(getClass());

  @Override
  public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory,
      SessionFactoryServiceRegistry serviceRegistry) {
    ConfigurationService cfgService = serviceRegistry.getService(ConfigurationService.class);
    Boolean shouldUseDynamicUpdate = cfgService.getSetting(SETTING, StandardConverters.BOOLEAN, null);
    if (shouldUseDynamicUpdate == null) {
      return;
    }

    logger.info("Setting dynamic update to {} for all entities.", shouldUseDynamicUpdate);
    for (PersistentClass persistentClass : metadata.getEntityBindings()) {
      persistentClass.setDynamicUpdate(shouldUseDynamicUpdate);
    }
  }

  @Override
  public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {}
}

Then add this line to your persistence.xml:

<property name="hibernate.dynamic_update" value="true" />

Note that this code makes use of the annotation processor org.immutables:metainf:2.9.0 to add the full qualified classname of DynamicUpdateIntegrator to the file META-INF/services/org.hibernate.integrator.spi.Integrator. You can instead use https://github.com/google/auto/tree/master/service to achieve the same or manually create and update that file.

Adrodoc
  • 673
  • 10
  • 19