org.hibernate.envers.internal.entities.mapper.SinglePropertyMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-envers-jakarta Show documentation
Show all versions of hibernate-envers-jakarta Show documentation
Hibernate's entity version (audit/history) support Jakarta edition
The newest version!
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.envers.internal.entities.mapper;
import java.io.Serializable;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.hibernate.HibernateException;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.dialect.Oracle8iDialect;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.internal.entities.PropertyData;
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
import org.hibernate.envers.internal.tools.ReflectionTools;
import org.hibernate.envers.internal.tools.StringTools;
import org.hibernate.property.access.spi.Setter;
import org.hibernate.property.access.spi.SetterFieldImpl;
/**
* TODO: diff
*
* @author Adam Warski (adam at warski dot org)
* @author Michal Skowronek (mskowr at o2 dot pl)
* @author Chris Cranford
*/
public class SinglePropertyMapper extends AbstractPropertyMapper implements SimpleMapperBuilder {
private PropertyData propertyData;
public SinglePropertyMapper(PropertyData propertyData) {
this.propertyData = propertyData;
}
public SinglePropertyMapper() {
}
@Override
public void add(PropertyData propertyData) {
if ( this.propertyData != null ) {
throw new AuditException( "Only one property can be added!" );
}
this.propertyData = propertyData;
}
@Override
public boolean mapToMapFromEntity(
SessionImplementor session,
Map data,
Object newObj,
Object oldObj) {
data.put( propertyData.getName(), newObj );
boolean dbLogicallyDifferent = true;
if ( (session.getFactory().getJdbcServices()
.getDialect() instanceof Oracle8iDialect) && (newObj instanceof String || oldObj instanceof String) ) {
// Don't generate new revision when database replaces empty string with NULL during INSERT or UPDATE statements.
dbLogicallyDifferent = !(StringTools.isEmpty( newObj ) && StringTools.isEmpty( oldObj ));
}
return dbLogicallyDifferent && !areEqual( newObj, oldObj );
}
@Override
public void mapModifiedFlagsToMapFromEntity(
SessionImplementor session,
Map data,
Object newObj,
Object oldObj) {
// Synthetic properties are not subject to withModifiedFlag analysis
if ( propertyData.isUsingModifiedFlag() && !propertyData.isSynthetic() ) {
data.put( propertyData.getModifiedFlagPropertyName(), !areEqual( newObj, oldObj ) );
}
}
@Override
public void mapModifiedFlagsToMapForCollectionChange(String collectionPropertyName, Map data) {
}
@Override
public void mapToEntityFromMap(
final EnversService enversService,
final Object obj,
final Map data,
Object primaryKey,
AuditReaderImplementor versionsReader,
Number revision) {
// synthetic properties are not part of the entity model; therefore they should be ignored.
if ( data == null || obj == null || propertyData.isSynthetic() ) {
return;
}
final Object value = data.get( propertyData.getName() );
if ( isDynamicComponentMap() ) {
@SuppressWarnings("unchecked")
final Map map = (Map) obj;
map.put( propertyData.getBeanName(), value );
}
else {
AccessController.doPrivileged(
new PrivilegedAction
© 2015 - 2025 Weber Informatics LLC | Privacy Policy