刚从官网下了hibernate4.3.4,结果照着官网doc中的示例class,xml写报了两个错误,实在是头疼

Funnee 2014-03-24 03:33:49
[yabao=][/yabao]

实体类:
package cn.edu.jmu.hibernate.model;

public class Student {
private int id;
private String name;
private int age;
public int getAge() {
return age;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
配置文件:hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration SYSTEM "http://hibernate.sourceforge.net/hibernate-configuration-3.0dtd" PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN">

<hibernate-configuration>


<session-factory>

<!-- Database connection settings -->


<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</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.MySQL5Dialect</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>

<!-- Echo all executed SQL to stdout -->


<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->


<!-- <property name="hbm2ddl.auto">update</property> 自动生成建表语句-->


<mapping resource="cn/edu/jmu/hibernate/model/Student.hbm.xml"/>

<mapping class="cn.edu.jmu.hibernate.model.Student"/>

</session-factory>

</hibernate-configuration>
配置文件2:Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0dtd">

<hibernate-mapping package="cn.edu.jmu.hibernate.model">
<class name="Student"><!-- 实体类名与表名一致可不写table属性 -->
<id name="id" /><!--主键 -->
<property name="name" column="name"/><!-- 类中的属性名,对应的数据库中的属性名 -->
<property name="age" /><!-- 不写默认两者一样 -->
</class>

</hibernate-mapping>
测试类:StudentTest
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.util.*;

import cn.edu.jmu.hibernate.model.*;

import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tutorial.util.HibernateUtil;

public class StudentTest {

public static void main(String[] args) {
Student st = new Student();
st.setId(6);
st.setName("哈哈");
st.setAge(20);

Session session = HibernateUtil.getSessionFactory().getCurrentSession();//报错之一:21行的。。。
session.beginTransaction();

session.save(st);

session.getTransaction().commit();
}
}
自定义类(测试类中要用到):HibernateUtil
package org.hibernate.tutorial.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory(
new StandardServiceRegistryBuilder().build() );
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}

}
...全文
166 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
就我上面那个连接就有一个例子,自己根本不去看。
Funnee 2014-03-25
  • 打赏
  • 举报
回复
找到问题所在了,因为少加了这句:configuration.configure("hibernate.cfg.xml");犯了个低级错误,谢谢大家了
Funnee 2014-03-24
  • 打赏
  • 举报
回复
版主有没有hibernate4.3.4的示例helloworld,我实在是找不出问题所在啊
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
官方hibernate.cfg.xml头文件

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


maping头文件

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
以上才是4.3.4官方文件,不知道你那是从哪里来的。
Funnee 2014-03-24
  • 打赏
  • 举报
回复
我加.了还是不行,是不是jar包没有引入对吗?我只引入了required文件夹下的jar包
  • 打赏
  • 举报
回复
http://hibernate.sourceforge.net/hibernate-mapping-3.0dtd"这个应该是http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd",0后面有一个点的。
Funnee 2014-03-24
  • 打赏
  • 举报
回复
我把mbpping class 去掉还是有报错。@_@
ay转身遇 2014-03-24
  • 打赏
  • 举报
回复
hibernate配置文件: <mapping resource="cn/edu/jmu/hibernate/model/Student.hbm.xml"/> <mapping class="cn.edu.jmu.hibernate.model.Student"/> 这两条随便去掉一条。

67,513

社区成员

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

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