为什么创建不了SessionFactory?

tanzhijun1988 2012-05-15 10:55:19
Student.java

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Student {
private int id;
private String name;
private int age;

@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}
hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=db_hibernate
</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<!-- JDBC connection pool (use the built-in) 连接池-->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect 方言 -->
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">create</property> -->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="myeclipse.connection.profile">
sqlserver2000
</property>
<mapping class="com.newer.model.Student" />
</session-factory>

</hibernate-configuration>


StudentTest.java

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

public class StudentTest {

public static void main(String[] args) {
Student s = new Student();
s.setName("s1");
s.setAge(1);
Configuration con = new AnnotationConfiguration();

System.out.println(con);

con.configure();
SessionFactory sf = con.buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();

}

}
运行上面的代码报错如下, 请大侠们帮忙解决下

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.

org.hibernate.cfg.AnnotationConfiguration@121f1d

Exception in thread "main" java.lang.AbstractMethodError: com.microsoft.jdbc.base.BaseDatabaseMetaData.supportsGetGeneratedKeys()Z
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:123)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
at com.newer.model.StudentTest.main(StudentTest.java:18)
...全文
136 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cseu 2012-05-16
  • 打赏
  • 举报
回复
将驱动改为jtds试试
tanzhijun1988 2012-05-16
  • 打赏
  • 举报
回复
我试了可以了 !感谢啊

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧