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

📄 printer.java

📁 人力资源管理系统主要包括:人员管理、招聘管理、培训管理、奖惩管理和薪金管理五大管理模块。
💻 JAVA
字号:
//$Id: Printer.java,v 1.1.2.2 2003/11/27 16:09:59 oneovthafew Exp $
package net.sf.hibernate.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.engine.SessionFactoryImplementor;
import net.sf.hibernate.engine.TypedValue;
import net.sf.hibernate.metadata.ClassMetadata;
import net.sf.hibernate.type.Type;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Renders entities to a nicely readable string.
 * @author Gavin King
 */
public final class Printer {
	
	private SessionFactoryImplementor factory;
	private static final Log log = LogFactory.getLog(Printer.class);
	
	/**
	 * @param entity an actual entity object, not a proxy!
	 */
	public String toString(Object entity) throws HibernateException {
		
		ClassMetadata cm = factory.getClassMetadata( entity.getClass() );
		if ( cm==null ) return entity.getClass().getName();
		
		Map result = new HashMap();
		
		if ( cm.hasIdentifierProperty() ) {
			result.put( 
				cm.getIdentifierPropertyName(), 
				cm.getIdentifierType().toString( cm.getIdentifier(entity), factory ) 
			);
		}
		
		Type[] types = cm.getPropertyTypes();
		String[] names = cm.getPropertyNames();
		Object[] values = cm.getPropertyValues(entity);
		for ( int i=0; i<types.length; i++ ) {
			result.put( names[i], types[i].toString( values[i], factory ) );
		}
		return cm.getMappedClass().getName() + result.toString();
	}
	
	public String toString(Type[] types, Object[] values) throws HibernateException {
		List list = new ArrayList( types.length * 5 );
		for ( int i=0; i<types.length; i++ ) {
			if ( types[i]!=null )list.add( types[i].toString( values[i], factory ) );
		}
		return list.toString();
	}
	
	public String toString(Map namedTypedValues) throws HibernateException {
		Map result = new HashMap();
		Iterator iter = namedTypedValues.entrySet().iterator();
		while ( iter.hasNext() ) {
			Map.Entry me = (Map.Entry) iter.next();
			TypedValue tv = (TypedValue) me.getValue();
			result.put( me.getKey(), tv.getType().toString( tv.getValue(), factory ) );
		}
		return result.toString();
	}
	
	public void toString(Iterator iter) throws HibernateException {
		if ( !log.isDebugEnabled() || !iter.hasNext() ) return;
		log.debug("listing entities:");
		int i=0;
		while ( iter.hasNext() ) {
			if (i++>20) {
				log.debug("more......");
				break;
			}
			log.debug( toString( iter.next() ) );
		}
	}

	public Printer(SessionFactoryImplementor factory) {
		this.factory = factory;
	}

}

⌨️ 快捷键说明

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