刚接触Hibernate,搭配环境:eclipse + Hibernate + MSSQL2000
错误一:java.lang.AbstractMethodError: com.microsoft.jdbc.base.BaseDatabaseMetaData.supportsGetGeneratedKeys()Z
查找到的解决方案:http://www.cnblogs.com/Snowfun/archive/2010/12/17/1909197.html,描述如下
将MSSQL的jdbc驱动改为jtds驱动,使用jtds(下载地址:http://sourceforge.net/projects/jtds/files/)下载jtds,解压得到jtds-版本号.jar,引用到eclipse中项目目录下(去除原jdbc驱动引用),更改hibernate配置文件:
原驱动连接配置:
<property name="hibernate.connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Hibernate</property>
更改jtds连接:
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=Hibernate</property>
错误二:在http://sourceforge.net/projects/jtds/files/下载jtds,下载了jtds-1.3.0后出现如下错误信息:
Exception in thread "main" java.lang.UnsupportedClassVersionError: net/sourceforge/jtds/jdbc/Driver : Unsupported major.minor version 51.0
初步判断是版本号的问题,是用前一个版本(ver 1.2.5)的该错误信息消失
问题得到解决。ps1:java代码调原驱动连接数据库是没有问题的,搭配hibernate会有此问题。
ps2:hibernate 4 buildSessionFactory()Deprecated
文档中解释:
SessionFactory buildSessionFactory()Deprecated. Use buildSessionFactory(ServiceRegistry) instead
要使用
SessionFactory buildSessionFactory(ServiceRegistry serviceRegistry)
Create a SessionFactory using the properties and mappings in this configuration.
eg:
Configuration conf = new Configuration()
//下面方法默认加载hibernate.cfg.xml文件
.configure();
//以Configuration创建SessionFactory
//SessionFactory sf = conf.buildSessionFactory();//该方法过期
ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
SessionFactory sf = conf.buildSessionFactory(sr);
//创建Session
Session sess = sf.openSession();
//开始事务
Transaction tx = sess.beginTransaction();
//创建员工实例
Personal p = new Personal();
//设置员工属性
p.setName("LocoRocooo");
p.setFcode("gz2015");
//保存消息
sess.save(p);
//提交事务
tx.commit();
//关闭Session
sess.close();
sf.close();