天天看點

hibernate5快速入門

好久沒有關注hibernate了,最早使用過3.2版本,後來就沒有再用過了。無意中看到hibernate版本都到5了。近日閑來無事,再溫習下。

再次聲明,本篇文章,隻是簡單的入門,來源于官方文檔。希望能夠到幫助别人。我用的版本是5.0.12。順便說下為什麼選擇這個版本,我看官方上有5.0.12這個版本。最新的是5.2.10。兩者都是穩定版本。再看下時間,5.0.12是在2017-01-19釋出的。到現在也有幾個月的時候了。決定還是用它吧。最新的不代表是最好的,另外,太新的話,估計資料也不一定全。最終選擇了5.0.12。

官方下載下傳位址

http://hibernate.org/orm/downloads/

下載下傳完以後,我把它解到了D:\document\hibernate-release-5.0.12.Final目錄下。本文章來源于

file:///D:/document/hibernate-release-5.0.12.Final/documentation/orm/5.0/quickstart/html/index.html的說明,檔案來源于

D:\document\hibernate-release-5.0.12.Final\project\documentation\src\main\asciidoc\quickstart\tutorials\basic\src\test

以下是我的代碼。建了個項目,maven方式,POM裡添加

<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>5.0.12.Final</version>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>	           

hibernate.cfg.xml,用的dtd,還是3.0版本的。另外,建立sessionFactory的代碼變了

// A SessionFactory is set up once for an application!
		final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
				.configure() // configures settings from hibernate.cfg.xml
				.build();
		try {
			sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
		}
		catch (Exception e) {
			// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
			// so destroy it manually.
			StandardServiceRegistryBuilder.destroy( registry );
		}           

就寫這麼多吧,剩下的自己看代碼去吧。我送出到了git上,位址是:https://git.oschina.net/hnzmdpan/hibernate.git

hibernate新特性

參考文章:http://blog.csdn.net/qwe6112071/article/details/51010636 

http://blog.csdn.net/xiangzhihong8/article/details/54171302

最後,我建了個 QQ群622539266,java知識交流,期待你的加入。

繼續閱讀