All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.cfg.ClassPropertyHolder Maven / Gradle / Ivy

There is a newer version: 3.5.6-Final
Show newest version
//$Id: ClassPropertyHolder.java 10321 2006-08-23 22:00:58Z epbernard $
package org.hibernate.cfg;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.JoinTable;

import org.hibernate.cfg.annotations.EntityBinder;
import org.hibernate.mapping.Component;
import org.hibernate.mapping.Join;
import org.hibernate.mapping.KeyValue;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Table;
import org.hibernate.reflection.XClass;

/**
 * @author Emmanuel Bernard
 */
public class ClassPropertyHolder extends AbstractPropertyHolder {
	private PersistentClass persistentClass;
	private Map joins;
	private transient Map joinsPerRealTableName;
	private EntityBinder entityBinder;

	public ClassPropertyHolder(
			PersistentClass persistentClass, XClass clazzToProcess, Map joins, ExtendedMappings mappings
	) {
		super( persistentClass.getEntityName(), null, clazzToProcess, mappings );
		this.persistentClass = persistentClass;
		this.joins = joins;
	}

	public ClassPropertyHolder(
			PersistentClass persistentClass, XClass clazzToProcess, EntityBinder entityBinder,
			ExtendedMappings mappings
	) {
		this( persistentClass, clazzToProcess, entityBinder.getSecondaryTables(), mappings );
		this.entityBinder = entityBinder;
	}

	public String getEntityName() {
		return persistentClass.getEntityName();
	}

	public void addProperty(Property prop, Ejb3Column[] columns) {
		//Ejb3Column.checkPropertyConsistency( ); //already called earlier
		if ( columns[0].isSecondary() ) {
			//TODO move the getJoin() code here?
			columns[0].getJoin().addProperty( prop );
		}
		else {
			addProperty( prop );
		}
	}

	public Join addJoin(JoinTable joinTableAnn, boolean noDelayInPkColumnCreation) {
		Join join = entityBinder.addJoin( joinTableAnn, this, noDelayInPkColumnCreation );
		this.joins = entityBinder.getSecondaryTables();
		return join;
	}

	public void addProperty(Property prop) {
		if ( prop.getValue() instanceof Component ) {
			//TODO handle quote and non quote table comparison
			String tableName = prop.getValue().getTable().getName();
			if ( getJoinsPerRealTableName().containsKey( tableName ) ) {
				getJoinsPerRealTableName().get( tableName ).addProperty( prop );
			}
			else {
				persistentClass.addProperty( prop );
			}
		}
		else {
			persistentClass.addProperty( prop );
		}
	}

	/**
	 * Needed for proper compliance with naming strategy, the property table
	 * can be overriden if the properties are part of secondary tables
	 */
	private Map getJoinsPerRealTableName() {
		if ( joinsPerRealTableName == null ) {
			joinsPerRealTableName = new HashMap( joins.size() );
			for ( Join join : joins.values() ) {
				joinsPerRealTableName.put( join.getTable().getName(), join );
			}
		}
		return joinsPerRealTableName;
	}

	public String getClassName() {
		return persistentClass.getClassName();
	}

	public String getEntityOwnerClassName() {
		return getClassName();
	}

	public Table getTable() {
		return persistentClass.getTable();
	}

	public boolean isComponent() {
		return false;
	}

	public PersistentClass getPersistentClass() {
		return persistentClass;
	}

	public KeyValue getIdentifier() {
		return persistentClass.getIdentifier();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy