毕竟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