gromling
Hasting_Y
采纳率60%
2015-12-22 16:13 阅读 2.5k
已采纳

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条回答 默认 最新

  • 已采纳
    u010515202 xhjwyy 2015-12-23 03:38


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

    点赞 1 评论 复制链接分享
  • Royal_lr Royal_lr 2015-12-23 01:14

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

    点赞 1 评论 复制链接分享

相关推荐