Skip to content

Instantly share code, notes, and snippets.

@elaatifi
Last active December 10, 2015 19:58
Show Gist options
  • Save elaatifi/4484905 to your computer and use it in GitHub Desktop.
Save elaatifi/4484905 to your computer and use it in GitHub Desktop.
Hibernate (If initialized) Mapping Specification for Orika Mapper
package ma.glasnost.orika.impl.generator.specification;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.generator.SourceCodeContext;
import ma.glasnost.orika.impl.generator.Specification;
import ma.glasnost.orika.impl.generator.VariableRef;
import ma.glasnost.orika.metadata.FieldMap;
public class IfInitializedHibernateSpecification extends AbstractSpecification {
private Specification delegate;
public IfInitializedHibernateSpecification(Specification delegate) {
this.delegate = delegate;
}
@Override
public boolean appliesTo(FieldMap fieldMap) {
return delegate.appliesTo(fieldMap);
}
@Override
public String generateMappingCode(FieldMap fieldMap, VariableRef source, VariableRef destination, SourceCodeContext code) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("if(org.hibernate.Hibernate.isPropertyInitialized(%s, \"%s\")) {", source.owner(), source.property().getName()));
sb.append(delegate.generateMappingCode(fieldMap, source, destination, code));
sb.append("}\n");
return sb.toString();
}
@Override
public void setMapperFactory(MapperFactory mapperFactory) {
super.setMapperFactory(mapperFactory);
delegate.setMapperFactory(mapperFactory);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment