畢竟hibernate是有名的ORM框架,因此仍是學着用一用,結果仍是出了很多問題,文檔啊文檔,你就不能及時更新麼。java
首先在建立一個全局的SessionFactory中,文檔中的示例代碼是這樣的,但這個是不能成功運行的,須要在try塊中稍微改一下mysql
public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml new Configuration().configure().buildSessionFactory( new StandardServiceRegistryBuilder().build() ); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
Configuration config = new Configuration(); config.configure("hibernate.cfg.xml"); // 這裏仍是指定一下要否則會先去找hibernate.properties return config.buildSessionFactory(new StandardServiceRegistryBuilder() .applySettings(config.getProperties()).build()); // 主要多加了一個applySettings
而後在啓動的時候還會拋一個錯sql
九月 17, 2014 9:29:52 下午 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
session
在下面這個網站能夠看這部分的源碼
http://grepcode.com/file/repo1.maven.org/maven2/org.hibernate/hibernate-core/4.3.6.Final/org/hibernate/engine/jdbc/internal/LobCreatorBuilder.java#LobCreatorBuilder.useContextualLobCreation%28java.util.Map%2Cjava.sql.Connection%29
其中的註釋是這樣說的app
Basically here we are simply checking whether we can call the java.sql.Connection methods for LOB creation added in JDBC 4. We not only check whether the java.sql.Connection declares these methods, but also whether the actual java.sql.Connection instance implements them (i.e. can be called without simply throwing an exception).框架
也就是說,這個錯誤應該是hibernate檢測到mysql-connector-java(5.1.31)沒有實現這個接口。maven
也看到有人說http://my.oschina.net/huangchp/blog/205461網站
使用mysql-connector-java 5.1.13及如下版本能夠解決,但具體緣由還不知道ui
感受應該是這個緣由了,不過暫時不影響使用就先無論了.net