hibernate5.0.6报错Unknown entity

网上这个错误的解决方法很多,我都试过了但是没有一个行的,确认我的配置文件没写错,请问是什么问题?
报错
Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.hibernate.Student
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:781)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
at com.hibernateservice.StudentTest.main(StudentTest.java:33)
hibernate.cfg.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="SessionFac">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">1234</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <!-- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="com/hibernate/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Student.hbm.xml

 <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-12-22 14:24:24 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping >
    <class name="com.hibernate.Student" table="t_student">
        <id name="id" column="stuId" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <properties name="name" column="stuName" type="java.lang.String"></properties>
    </class>
</hibernate-mapping>

Student.java:

 package com.hibernate;

public class Student {

    int id;
    String name;

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    public Student() {
        // TODO Auto-generated constructor stub

    }

}

StudentTest.java:

 package com.hibernateservice;
import com.hibernate.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class StudentTest {

    public StudentTest() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {

        Configuration config = new Configuration().configure();

        org.hibernate.service.ServiceRegistry registry = 
                new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();

        SessionFactory sessionFactory = config.buildSessionFactory(registry);
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        Student s = new Student();
        s.setName("张三");
        s.setId(0);
        session.save(s);

        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }

}

目录:
图片说明

2个回答


你是使用配置文件配置entity的,把class那一串去了就行了

gromling
Hasting_Y 回复xhjwyy: 谢啦~我把版本换成Hibernate4.3就解决了,源码应该没有错,不清楚这是什么原因
4 年多之前 回复
u010515202
xhjwyy 回复gromling: 你把源码发我qq邮箱里854836846 ,下班了给你看看
4 年多之前 回复
gromling
Hasting_Y 我删掉了,依然是同样的错误。。。麻烦你帮我看看好吗
4 年多之前 回复

mapping resource="com/hibernate/Student.hbm.xml" class="com.hibernate.Student"/>你这样写肯定不行的,,如果你用的配置文件的话,,就这样
mapping resource="com/hibernate/Student.hbm.xml"/>,,如果你用的注解的话,,就这样 mapping class="com.hibernate.Student"/>

gromling
Hasting_Y 我删掉了,可还是报错。。。。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐