Last active
December 10, 2015 19:58
-
-
Save elaatifi/4484905 to your computer and use it in GitHub Desktop.
Hibernate (If initialized) Mapping Specification for Orika Mapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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