/* * 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 <http://www.gnu.org/licenses/lgpl-2.1.html>. */ package org.hibernate.property.access.internal; import java.io.Serializable; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.util.Map; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.property.access.spi.Getter; import org.hibernate.property.access.spi.PropertyAccess; import org.hibernate.property.access.spi.PropertyAccessStrategy; import org.hibernate.property.access.spi.Setter; /** * @author Gavin King * @author Steve Ebersole */ public class PropertyAccessStrategyBackRefImpl implements PropertyAccessStrategy { /** * A placeholder for a property value, indicating that * we don't know the value of the back reference */ public static final Serializable UNKNOWN = new Serializable() { @Override public String toString() { return "<unknown>"; } public Object readResolve() { return UNKNOWN; } }; private final String entityName; private final String propertyName; public PropertyAccessStrategyBackRefImpl(String collectionRole, String entityName) { this.entityName = entityName; this.propertyName = collectionRole.substring( entityName.length() + 1 ); } @Override public PropertyAccess buildPropertyAccess(Class containerJavaType, String propertyName) { return new PropertyAccessBackRefImpl( this ); } private static class PropertyAccessBackRefImpl implements PropertyAccess { private PropertyAccessStrategyBackRefImpl strategy; private final GetterImpl getter; public PropertyAccessBackRefImpl(PropertyAccessStrategyBackRefImpl strategy) { this.strategy = strategy; this.getter = new GetterImpl( strategy.entityName, strategy.propertyName ); } @Override public PropertyAccessStrategy getPropertyAccessStrategy() { return strategy; } @Override public Getter getGetter() { return getter; } @Override public Setter getSetter() { return SetterImpl.INSTANCE; } } private static class GetterImpl implements Getter { private final String entityName; private final String propertyName; public GetterImpl(String entityName, String propertyName) { this.entityName = entityName; this.propertyName = propertyName; } @Override public Object get(Object owner) { return UNKNOWN; } @Override public Object getForInsert(Object owner, Map mergeMap, SharedSessionContractImplementor session) { if ( session == null ) { return UNKNOWN; } else { return session.getPersistenceContext().getOwnerId( entityName, propertyName, owner, mergeMap ); } } @Override public Class getReturnType() { return Object.class; } @Override public Member getMember() { return null; } @Override public String getMethodName() { return null; } @Override public Method getMethod() { return null; } } private static class SetterImpl implements Setter { /** * Singleton access */ public static final SetterImpl INSTANCE = new SetterImpl(); @Override public void set(Object target, Object value, SessionFactoryImplementor factory) { // this page intentionally left blank :) } @Override public String getMethodName() { return null; } @Override public Method getMethod() { return null; } } }