⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typefactory.java

📁 hibernate-3.1.3-all-src.zip 面向对象的访问数据库工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// $Id: TypeFactory.java 7830 2005-08-11 00:10:26Z oneovthafew $
package org.hibernate.type;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.classic.Lifecycle;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.intercept.LazyPropertyInitializer;
import org.hibernate.property.BackrefPropertyAccessor;
import org.hibernate.tuple.StandardProperty;
import org.hibernate.usertype.CompositeUserType;
import org.hibernate.usertype.UserType;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.util.ReflectHelper;

/**
 * Used internally to obtain instances of <tt>Type</tt>. Applications should use static methods
 * and constants on <tt>org.hibernate.Hibernate</tt>.
 * 
 * @see org.hibernate.Hibernate
 * @author Gavin King
 */
public final class TypeFactory {

	private static final Map BASIC_TYPES;

	static {
		HashMap basics = new HashMap();
		basics.put( boolean.class.getName(), Hibernate.BOOLEAN );
		basics.put( long.class.getName(), Hibernate.LONG );
		basics.put( short.class.getName(), Hibernate.SHORT );
		basics.put( int.class.getName(), Hibernate.INTEGER );
		basics.put( byte.class.getName(), Hibernate.BYTE );
		basics.put( float.class.getName(), Hibernate.FLOAT );
		basics.put( double.class.getName(), Hibernate.DOUBLE );
		basics.put( char.class.getName(), Hibernate.CHARACTER );
		basics.put( Hibernate.CHARACTER.getName(), Hibernate.CHARACTER );
		basics.put( Hibernate.INTEGER.getName(), Hibernate.INTEGER );
		basics.put( Hibernate.STRING.getName(), Hibernate.STRING );
		basics.put( Hibernate.DATE.getName(), Hibernate.DATE );
		basics.put( Hibernate.TIME.getName(), Hibernate.TIME );
		basics.put( Hibernate.TIMESTAMP.getName(), Hibernate.TIMESTAMP );
		basics.put( "dbtimestamp", new DbTimestampType() );
		basics.put( Hibernate.LOCALE.getName(), Hibernate.LOCALE );
		basics.put( Hibernate.CALENDAR.getName(), Hibernate.CALENDAR );
		basics.put( Hibernate.CALENDAR_DATE.getName(), Hibernate.CALENDAR_DATE );
		basics.put( Hibernate.CURRENCY.getName(), Hibernate.CURRENCY );
		basics.put( Hibernate.TIMEZONE.getName(), Hibernate.TIMEZONE );
		basics.put( Hibernate.CLASS.getName(), Hibernate.CLASS );
		basics.put( Hibernate.TRUE_FALSE.getName(), Hibernate.TRUE_FALSE );
		basics.put( Hibernate.YES_NO.getName(), Hibernate.YES_NO );
		basics.put( Hibernate.BINARY.getName(), Hibernate.BINARY );
		basics.put( Hibernate.TEXT.getName(), Hibernate.TEXT );
		basics.put( Hibernate.BLOB.getName(), Hibernate.BLOB );
		basics.put( Hibernate.CLOB.getName(), Hibernate.CLOB );
		basics.put( Hibernate.BIG_DECIMAL.getName(), Hibernate.BIG_DECIMAL );
		basics.put( Hibernate.BIG_INTEGER.getName(), Hibernate.BIG_INTEGER );
		basics.put( Hibernate.SERIALIZABLE.getName(), Hibernate.SERIALIZABLE );
		basics.put( Hibernate.OBJECT.getName(), Hibernate.OBJECT );
		basics.put( Boolean.class.getName(), Hibernate.BOOLEAN );
		basics.put( Long.class.getName(), Hibernate.LONG );
		basics.put( Short.class.getName(), Hibernate.SHORT );
		basics.put( Integer.class.getName(), Hibernate.INTEGER );
		basics.put( Byte.class.getName(), Hibernate.BYTE );
		basics.put( Float.class.getName(), Hibernate.FLOAT );
		basics.put( Double.class.getName(), Hibernate.DOUBLE );
		basics.put( Character.class.getName(), Hibernate.CHARACTER );
		basics.put( String.class.getName(), Hibernate.STRING );
		basics.put( java.util.Date.class.getName(), Hibernate.TIMESTAMP );
		basics.put( Time.class.getName(), Hibernate.TIME );
		basics.put( Timestamp.class.getName(), Hibernate.TIMESTAMP );
		basics.put( java.sql.Date.class.getName(), Hibernate.DATE );
		basics.put( BigDecimal.class.getName(), Hibernate.BIG_DECIMAL );
		basics.put( BigInteger.class.getName(), Hibernate.BIG_INTEGER );
		basics.put( Locale.class.getName(), Hibernate.LOCALE );
		basics.put( Calendar.class.getName(), Hibernate.CALENDAR );
		basics.put( GregorianCalendar.class.getName(), Hibernate.CALENDAR );
		if ( CurrencyType.CURRENCY_CLASS != null ) {
			basics.put( CurrencyType.CURRENCY_CLASS.getName(), Hibernate.CURRENCY );
		}
		basics.put( TimeZone.class.getName(), Hibernate.TIMEZONE );
		basics.put( Object.class.getName(), Hibernate.OBJECT );
		basics.put( Class.class.getName(), Hibernate.CLASS );
		basics.put( byte[].class.getName(), Hibernate.BINARY );
		basics.put( "byte[]", Hibernate.BINARY );
		basics.put( Blob.class.getName(), Hibernate.BLOB );
		basics.put( Clob.class.getName(), Hibernate.CLOB );
		basics.put( Serializable.class.getName(), Hibernate.SERIALIZABLE );

		Type type = new AdaptedImmutableType(Hibernate.DATE);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.TIME);
		basics.put( type.getName(), type );		
		type = new AdaptedImmutableType(Hibernate.TIMESTAMP);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType( new DbTimestampType() );
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.CALENDAR);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.CALENDAR_DATE);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.SERIALIZABLE);
		basics.put( type.getName(), type );
		type = new AdaptedImmutableType(Hibernate.BINARY);
		basics.put( type.getName(), type );
		
		BASIC_TYPES = Collections.unmodifiableMap( basics );
	}

	private TypeFactory() {
		throw new UnsupportedOperationException();
	}

	/**
	 * A one-to-one association type for the given class
	 */
	public static EntityType oneToOne(
			String persistentClass, 
			ForeignKeyDirection foreignKeyType,
			String uniqueKeyPropertyName, 
			boolean lazy, 
			boolean unwrapProxy,
			boolean isEmbeddedInXML, 
			String entityName, 
			String propertyName
	) {
		return new OneToOneType(
				persistentClass,
				foreignKeyType,
				uniqueKeyPropertyName,
				lazy,
				unwrapProxy,
				isEmbeddedInXML,
				entityName,
				propertyName 
			);
	}

	/**
	 * A many-to-one association type for the given class
	 */
	public static EntityType manyToOne(String persistentClass) {
		return new ManyToOneType( persistentClass );
	}

	/**
	 * A many-to-one association type for the given class
	 */
	public static EntityType manyToOne(String persistentClass, boolean lazy) {
		return new ManyToOneType( persistentClass, lazy );
	}

	/**
	 * A many-to-one association type for the given class
	 */
	public static EntityType manyToOne(
			String persistentClass, 
			String uniqueKeyPropertyName,
			boolean lazy, 
			boolean unwrapProxy, 
			boolean isEmbeddedInXML,
			boolean ignoreNotFound
	) {
		return new ManyToOneType( 
				persistentClass, 
				uniqueKeyPropertyName, 
				lazy,
				unwrapProxy, 
				isEmbeddedInXML,
				ignoreNotFound
			);
	}

	/**
	 * Given the name of a Hibernate basic type, return an instance of
	 * <tt>org.hibernate.type.Type</tt>.
	 */
	public static Type basic(String name) {
		return (Type) BASIC_TYPES.get( name );
	}

	/**
	 * Uses heuristics to deduce a Hibernate type given a string naming the type or Java class.
	 * Return an instance of <tt>org.hibernate.type.Type</tt>.
	 */
	public static Type heuristicType(String typeName) throws MappingException {
		return heuristicType( typeName, null );
	}

	/**
	 * Uses heuristics to deduce a Hibernate type given a string naming the type or Java class.
	 * Return an instance of <tt>org.hibernate.type.Type</tt>.
	 */
	public static Type heuristicType(String typeName, Properties parameters)
			throws MappingException {
		Type type = TypeFactory.basic( typeName );
		if ( type == null ) {
			Class typeClass;
			try {
				typeClass = ReflectHelper.classForName( typeName );
			}
			catch (ClassNotFoundException cnfe) {
				typeClass = null;
			}
			if ( typeClass != null ) {
				if ( Type.class.isAssignableFrom( typeClass ) ) {
					try {
						type = (Type) typeClass.newInstance();
					}
					catch (Exception e) {
						throw new MappingException( 
								"Could not instantiate Type: " + typeClass.getName(),
								e 
							);
					}
					injectParameters(type, parameters);
				}
				else if ( CompositeUserType.class.isAssignableFrom( typeClass ) ) {
					type = new CompositeCustomType( typeClass, parameters );
				}
				else if ( UserType.class.isAssignableFrom( typeClass ) ) {
					type = new CustomType( typeClass, parameters );
				}
				else if ( Lifecycle.class.isAssignableFrom( typeClass ) ) {
					type = Hibernate.entity( typeClass );
				}
				else if ( Serializable.class.isAssignableFrom( typeClass ) ) {
					type = Hibernate.serializable( typeClass );
				}
			}
		}
		return type;

	}

	public static CollectionType customCollection(String typeName, String role, String propertyRef,
			boolean embedded) {
		Class typeClass;
		try {
			typeClass = ReflectHelper.classForName( typeName );
		}
		catch (ClassNotFoundException cnfe) {
			throw new MappingException( "user colllection type class not found: " + typeName, cnfe );
		}
		return new CustomCollectionType( typeClass, role, propertyRef, embedded );
	}

	// Collection Types:

	public static CollectionType array(String role, String propertyRef, boolean embedded,
			Class elementClass) {
		return new ArrayType( role, propertyRef, elementClass, embedded );
	}

	public static CollectionType list(String role, String propertyRef, boolean embedded) {
		return new ListType( role, propertyRef, embedded );
	}

	public static CollectionType bag(String role, String propertyRef, boolean embedded) {
		return new BagType( role, propertyRef, embedded );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -