hibernate5.0.5获取sessionFactory总报错,不知道问题出在哪?求大神啊

fangshusen 2016-01-01 05:33:40
1.hibernate.cfg.xml如下

<?xml version='1.0' encoding='utf-8'?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/j2ee</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>

<mapping resource="cn/hibernate5/basic/Student.hbm.xml"/>

</session-factory>

</hibernate-configuration>

2.Student.hbm.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.hibernate5.basic">

<class name="Student" table="student">
<id name="id" column="studentId">
</id>
<property name="name"></property>
<property name="sex"></property>
</class>

</hibernate-mapping>

3.Student.java

package cn.hibernate5.basic;

public class Student {
private int id;
private String name;
private String sex;

public Student(){

}

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 String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

}


4.Hibernate.java (此类用于获取SessionFactory)

package Test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import cn.hibernate5.basic.Student;

public class Hibernate {

protected static SessionFactory getSessionFactory() throws Exception {
SessionFactory sessionFactory = null;
// 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);
}

return sessionFactory;
}
}



5.StudentTest.java(测试类)

package Test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import cn.hibernate5.basic.Student;

public class StudentTest {
public static void main(String args[]) {
SessionFactory sf = null;
try {
sf = Hibernate.getSessionFactory();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Student s = new Student();
s.setId(1);
s.setName("fangshusen");
s.setSex("man");

Session session = sf.getCurrentSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
}

}



运行测试时的一场信息
...全文
197 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanghef 2016-01-01
  • 打赏
  • 举报
回复
Project -> Source -> Clean-up清一下项目试试,也可能是缺少jar包commons-lang-1.0-b1.jar
qq_21527087 2016-01-01
  • 打赏
  • 举报
回复
hbm.xml里面的id 没有生成策略吗? 你发一下控制台的信息看一下

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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