双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

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

fangshusen fangshusen 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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();
}

}



运行测试时的一场信息
...全文
评论 2
收藏 已收藏
分享
返回顶部
fangshusen
Java EE 67,535 成员 225,843 内容