org.hibernate.envers.internal.entities.mapper.id.SingleIdMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-envers Show documentation
Show all versions of hibernate-envers Show documentation
Hibernate's entity version (audit/history) support
/*
* 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.id;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.internal.entities.PropertyData;
import org.hibernate.envers.internal.tools.ReflectionTools;
import org.hibernate.property.access.spi.Getter;
import org.hibernate.property.access.spi.Setter;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.service.ServiceRegistry;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class SingleIdMapper extends AbstractIdMapper implements SimpleIdMapperBuilder {
private PropertyData propertyData;
public SingleIdMapper(ServiceRegistry serviceRegistry) {
super( serviceRegistry );
}
public SingleIdMapper(ServiceRegistry serviceRegistry, PropertyData propertyData) {
this( serviceRegistry );
this.propertyData = propertyData;
}
@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 mapToEntityFromMap(final Object obj, Map data) {
if ( data == null || obj == null ) {
return false;
}
final Object value = data.get( propertyData.getName() );
if ( value == null ) {
return false;
}
return AccessController.doPrivileged(
new PrivilegedAction() {
@Override
public Boolean run() {
final Setter setter = ReflectionTools.getSetter(
obj.getClass(),
propertyData,
getServiceRegistry()
);
setter.set( obj, value, null );
return true;
}
}
);
}
@Override
public Object mapToIdFromMap(Map data) {
if ( data == null ) {
return null;
}
return data.get( propertyData.getName() );
}
@Override
public Object mapToIdFromEntity(final Object data) {
if ( data == null ) {
return null;
}
if ( data instanceof HibernateProxy ) {
final HibernateProxy hibernateProxy = (HibernateProxy) data;
return hibernateProxy.getHibernateLazyInitializer().getInternalIdentifier();
}
else {
return AccessController.doPrivileged(
new PrivilegedAction