org.hibernate.persister.entity.AbstractPropertyMapping Maven / Gradle / Ivy
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.persister.entity;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.MappingException;
import org.hibernate.QueryException;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.sql.Template;
import org.hibernate.type.AssociationType;
import org.hibernate.type.CompositeType;
import org.hibernate.type.EntityType;
import org.hibernate.type.Type;
import org.jboss.logging.Logger;
/**
* Basic implementation of the {@link PropertyMapping} contract.
*
* @author Gavin King
*/
public abstract class AbstractPropertyMapping implements PropertyMapping {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class,
AbstractPropertyMapping.class.getName());
private final Map typesByPropertyPath = new HashMap();
private final Map columnsByPropertyPath = new HashMap();
private final Map columnReadersByPropertyPath = new HashMap();
private final Map columnReaderTemplatesByPropertyPath = new HashMap();
private final Map formulaTemplatesByPropertyPath = new HashMap();
public String[] getIdentifierColumnNames() {
throw new UnsupportedOperationException("one-to-one is not supported here");
}
public String[] getIdentifierColumnReaderTemplates() {
throw new UnsupportedOperationException("one-to-one is not supported here");
}
public String[] getIdentifierColumnReaders() {
throw new UnsupportedOperationException("one-to-one is not supported here");
}
protected abstract String getEntityName();
public Type toType(String propertyName) throws QueryException {
Type type = (Type) typesByPropertyPath.get(propertyName);
if ( type == null ) {
throw propertyException( propertyName );
}
return type;
}
protected final QueryException propertyException(String propertyName) throws QueryException {
return new QueryException( "could not resolve property: " + propertyName + " of: " + getEntityName() );
}
public String[] getColumnNames(String propertyName) {
String[] cols = (String[]) columnsByPropertyPath.get(propertyName);
if (cols==null) {
throw new MappingException("unknown property: " + propertyName);
}
return cols;
}
public String[] toColumns(String alias, String propertyName) throws QueryException {
//TODO: *two* hashmap lookups here is one too many...
String[] columns = (String[]) columnsByPropertyPath.get(propertyName);
if ( columns == null ) {
throw propertyException( propertyName );
}
String[] formulaTemplates = (String[]) formulaTemplatesByPropertyPath.get(propertyName);
String[] columnReaderTemplates = (String[]) columnReaderTemplatesByPropertyPath.get(propertyName);
String[] result = new String[columns.length];
for ( int i=0; i