`unknown entity`在hibernate中的异常

时间:2015-10-29 01:41:35

标签: java hibernate orm hibernate-mapping

我正在学习休眠,但我无法弄清楚为什么会出现这个错误。我试过搜索,但我找不到帮助我的解决方案。我想知道为什么会收到此错误。

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.simpleprogrammer.User
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1451)
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:678)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665)
at Program.main(Program.java:23)

请帮助我已经浪费了这么多时间来调试它。 这是我的简单程序,

Program.java

import org.hibernate.Session;
import com.simpleprogrammer.HibernateUtilities;
import com.simpleprogrammer.User;

public class Program {

/**
 * @param args
 * @throws ClassNotFoundException 
 * @throws SQLException 
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Hello World");
    Session session = HibernateUtilities.getSessionFactory().openSession();

    session.beginTransaction();

    User user = new User();
    user.setName("Deepak");
    user.setTotal(130);
    user.setGoal(150);
    session.save(user);

    session.getTransaction().commit();

    session.close();

}


}

HibernateUtilities.java

package com.simpleprogrammer;
import org.hibernate.HibernateException;

import org.hibernate.SessionFactory;

import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;



public class HibernateUtilities {

public static SessionFactory sessionFactory;
public static ServiceRegistry serviceRegistry;

static{
    try{
        Configuration configuation = new Configuration().configure();

        serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuation.getProperties()).build();

        sessionFactory = configuation.buildSessionFactory(serviceRegistry);

    }catch(HibernateException e){
        System.out.println("Error while creating Session Factory.");
        e.printStackTrace();
    }
}

public static SessionFactory getSessionFactory(){
    return sessionFactory;
}

}

User.java

package com.simpleprogrammer;

public class User {

private int id;
private String name;
private int total;
private int goal;
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 int getTotal() {
    return total;
}
public void setTotal(int total) {
    this.total = total;
}
public int getGoal() {
    return goal;
}
public void setGoal(int goal) {
    this.goal = goal;
}


}

User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD     3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 28, 2015 8:05:49 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.simpleprogrammer.User" table="users">
    <id name="id" type="int">
        <column name="id" />
        <generator class="identity" />
    </id>
    <property name="name" type="java.lang.String">
        <column name="name" />
    </property>
    <property name="total" type="int">
        <column name="total" />
    </property>
    <property name="goal" type="int">
        <column name="goal" />
    </property>
</class>
</hibernate-mapping>

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">admin</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.default_schema">protein_tracker</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <mapping resource="com/simpleprogrammer/User.hbm.xml" />
</session-factory>

enter image description here

2 个答案:

答案 0 :(得分:1)

其实我在使用Hibernate 5罐子。但是教程实际上是针对Hibernate 4.当我使用Hibernate 4 jar时,一切似乎都运行良好。

答案 1 :(得分:0)

可能你不需要<mapping resource="com/simpleprogrammer/User.hbm.xml" />。相反,请尝试<mapping resource="User.hbm.xml" />