`

Hibernate源码分析

 
阅读更多

记录下学习Hibernate的过程。先写下软件的版本

	<properties>
		<org.slf4j.version>1.7.12</org.slf4j.version>
	</properties>
  
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
	<dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-core</artifactId>
		<version>3.3.2.GA</version>
	</dependency>
	
	<dependency>
		<groupId>javassist</groupId>
		<artifactId>javassist</artifactId>
		<version>3.9.0.GA</version>
	</dependency>
	
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${org.slf4j.version}</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${org.slf4j.version}</version>
		</dependency>
		
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2</version>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>		
  </dependencies>

 

 

然后给出log4j的配置(比较简单)

log4j.rootLogger=info, stdout

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n

 

 

 

public class Main {
	private static Configuration configuration = new Configuration();
//	private static org.hibernate.SessionFactory sessionFactory;

	public static void main(String[] args) {}
}

 

 好了,程序运行以后,会在控制台输出以下内容:

2016-04-14 21:41:47,471 [org.hibernate.cfg.Environment]-[INFO] Hibernate 3.3.2.GA
2016-04-14 21:41:47,475 [org.hibernate.cfg.Environment]-[INFO] loaded properties from resource hibernate.properties: {hibernate.show_sql=false, hibernate.bytecode.use_reflection_optimizer=false}
2016-04-14 21:41:47,479 [org.hibernate.cfg.Environment]-[INFO] Bytecode provider name : javassist
2016-04-14 21:41:47,483 [org.hibernate.cfg.Environment]-[INFO] using JDK 1.4 java.sql.Timestamp handling

 

好了,别看这么几行代码,我比较奇怪的是,程序是怎么执行的,为什么会打印那么一段代码。经过查找,发现了以下代码:

在new Configuration();时,代码执行到了org.hibernate.cfg.Configuration类的构造函数。

 

	protected Configuration(SettingsFactory settingsFactory) {
		this.settingsFactory = settingsFactory;
		reset();
	}

	public Configuration() {
		this( new SettingsFactory() );
	}
 于是又调用了org.hibernate.cfg.SettingsFactory

 

 

	protected SettingsFactory() {
	}
 这是一个空的构造函数。于是回到了Configuration(SettingsFactory settingsFactory)里,调用了一个reset方法

 

protected void reset() {
		classes = new HashMap();
		imports = new HashMap();
		collections = new HashMap();
		tables = new TreeMap();
		namedQueries = new HashMap();
		namedSqlQueries = new HashMap();
		sqlResultSetMappings = new HashMap();
		xmlHelper = new XMLHelper();
		typeDefs = new HashMap();
		propertyReferences = new ArrayList();
		secondPasses = new ArrayList();
		interceptor = EmptyInterceptor.INSTANCE;
		properties = Environment.getProperties();   // 呵呵,就是这一句很重要。
		entityResolver = XMLHelper.DEFAULT_DTD_RESOLVER;
		eventListeners = new EventListeners();
		filterDefinitions = new HashMap();
//		extendsQueue = new ArrayList();
		extendsQueue = new HashMap();
		auxiliaryDatabaseObjects = new ArrayList();
		tableNameBinding = new HashMap();
		columnNameBindingPerTable = new HashMap();
		namingStrategy = DefaultNamingStrategy.INSTANCE;
		sqlFunctions = new HashMap();

		entityTuplizerFactory = new EntityTuplizerFactory();
//		componentTuplizerFactory = new ComponentTuplizerFactory();
	}
 于是一个类org.hibernate.cfg.Environment上场了。里面有一段代码段,是这么写的
static {

		log.info("Hibernate " + VERSION);

		RENAMED_PROPERTIES.put( "hibernate.cglib.use_reflection_optimizer", USE_REFLECTION_OPTIMIZER );

		ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_NONE), "NONE" );
		ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_READ_UNCOMMITTED), "READ_UNCOMMITTED" );
		ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_READ_COMMITTED), "READ_COMMITTED" );
		ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_REPEATABLE_READ), "REPEATABLE_READ" );
		ISOLATION_LEVELS.put( new Integer(Connection.TRANSACTION_SERIALIZABLE), "SERIALIZABLE" );

		GLOBAL_PROPERTIES = new Properties();
		//Set USE_REFLECTION_OPTIMIZER to false to fix HHH-227
		GLOBAL_PROPERTIES.setProperty( USE_REFLECTION_OPTIMIZER, Boolean.FALSE.toString() );

		try {
			InputStream stream = ConfigHelper.getResourceAsStream("/hibernate.properties");
			try {
				GLOBAL_PROPERTIES.load(stream);
				log.info( "loaded properties from resource hibernate.properties: " + PropertiesHelper.maskOut(GLOBAL_PROPERTIES, PASS) );
			}
			catch (Exception e) {
				log.error("problem loading properties from hibernate.properties");
			}
			finally {
				try{
					stream.close();
				}
				catch (IOException ioe){
					log.error("could not close stream on hibernate.properties", ioe);
				}
			}
		}
		catch (HibernateException he) {
			log.info("hibernate.properties not found");
		}

		try {
			GLOBAL_PROPERTIES.putAll( System.getProperties() );
		}
		catch (SecurityException se) {
			log.warn("could not copy system properties, system properties will be ignored");
		}

		verifyProperties(GLOBAL_PROPERTIES);

		ENABLE_BINARY_STREAMS = PropertiesHelper.getBoolean(USE_STREAMS_FOR_BINARY, GLOBAL_PROPERTIES);
		ENABLE_REFLECTION_OPTIMIZER = PropertiesHelper.getBoolean(USE_REFLECTION_OPTIMIZER, GLOBAL_PROPERTIES);

		if (ENABLE_BINARY_STREAMS) {
			log.info("using java.io streams to persist binary types");
		}
		if (ENABLE_REFLECTION_OPTIMIZER) {
			log.info("using bytecode reflection optimizer");
		}
		BYTECODE_PROVIDER_INSTANCE = buildBytecodeProvider( GLOBAL_PROPERTIES );

		boolean getGeneratedKeysSupport;
		try {
			Statement.class.getMethod("getGeneratedKeys", null);
			getGeneratedKeysSupport = true;
		}
		catch (NoSuchMethodException nsme) {
			getGeneratedKeysSupport = false;
		}
		JVM_SUPPORTS_GET_GENERATED_KEYS = getGeneratedKeysSupport;
		if (!JVM_SUPPORTS_GET_GENERATED_KEYS) {
			log.info("JVM does not support Statement.getGeneratedKeys()");
		}

		boolean linkedHashSupport;
		try {
			Class.forName("java.util.LinkedHashSet");
			linkedHashSupport = true;
		}
		catch (ClassNotFoundException cnfe) {
			linkedHashSupport = false;
		}
		JVM_SUPPORTS_LINKED_HASH_COLLECTIONS = linkedHashSupport;
		if (!JVM_SUPPORTS_LINKED_HASH_COLLECTIONS) {
			log.info("JVM does not support LinkedHasMap, LinkedHashSet - ordered maps and sets disabled");
		}

		long x = 123456789;
		JVM_HAS_TIMESTAMP_BUG = new Timestamp(x).getTime() != x;
		if (JVM_HAS_TIMESTAMP_BUG) {
			log.info("using workaround for JVM bug in java.sql.Timestamp");
		}

		Timestamp t = new Timestamp(0);
		t.setNanos(5 * 1000000);
		JVM_HAS_JDK14_TIMESTAMP = t.getTime() == 5;
		if (JVM_HAS_JDK14_TIMESTAMP) {
			log.info("using JDK 1.4 java.sql.Timestamp handling");
		}
		else {
			log.info("using pre JDK 1.4 java.sql.Timestamp handling");
		}
	}
  
看看,控制台里的信息打印出来了吧。
 
得了,我的睡觉了,今天先到这里吧,要睡觉了。
0
5
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics