[休眠]错误:找不到实体类:
Posted
技术标签:
【中文标题】[休眠]错误:找不到实体类:【英文标题】:[Hibernate]Error: entity class not found: 【发布时间】:2011-10-05 07:01:46 【问题描述】:我厌倦了很长时间。我不知道是什么导致了这个错误。这是我的文件:
Uzytkownik.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Uzytkownik" table="uzytkownicy">
<id column="id" name="id" type="int"/>
<property column="login" generated="never" lazy="false" name="login" type="string"/>
<property column="haslo" generated="never" lazy="false" name="haslo" type="string"/>
</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 name="">
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/sprawozdania</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping resource="com/vaannila/uzytkownik/Uzytkownik.hbm.xml"/>
</session-factory>
</hibernate-configuration>
我使用 mysql 5.5。
我收到以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.vaannila.util.HibernateUtil.<clinit>(HibernateUtil.java:14)
at com.vaannila.uzytkownik.Main.saveUzyt(Main.java:22)
at com.vaannila.uzytkownik.Main.main(Main.java:16)
Caused by: org.hibernate.MappingException: entity class not found: Uzytkownik
这是我的课程: main.java
package com.vaannila.uzytkownik;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import javax.persistence.Entity;
import com.vaannila.util.HibernateUtil;
public class Main
/**
* @param args
*/
public static void main(String[] args)
Main obj = new Main();
String uzytkownikLogin = obj.saveUzyt("Adam", "Malysz");
public String saveUzyt(String login, String haslo)
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
String uzytLog = null;
try
transaction = session.beginTransaction();
Uzytkownik uzyt = new Uzytkownik();
uzyt.setLogin(login);
uzyt.setHaslo(haslo);
uzytLog = (String) session.save(uzyt);
transaction.commit();
catch (HibernateException e)
transaction.rollback();
e.printStackTrace();
finally
session.close();
return uzytLog;
Uzytkownik.java:
package com.vaannila.uzytkownik;
// default package
// Generated 2011-07-14 13:39:18 by Hibernate Tools 3.4.0.CR1
/**
* Uzytkownik generated by hbm2java
*/
public class Uzytkownik implements java.io.Serializable
private int id;
private String login;
private String haslo;
public Uzytkownik()
public Uzytkownik(int id)
this.id = id;
public Uzytkownik(int id, String login, String haslo)
this.id = id;
this.login = login;
this.haslo = haslo;
public int getId()
return this.id;
public void setId(int id)
this.id = id;
public String getLogin()
return this.login;
public void setLogin(String login)
this.login = login;
public String getHaslo()
return this.haslo;
public void setHaslo(String haslo)
this.haslo = haslo;
HibernateUtil.java:
package com.vaannila.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil
private static final SessionFactory sessionFactory;
static
try
sessionFactory = new Configuration().configure().buildSessionFactory();
catch (Throwable ex)
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
public static SessionFactory getSessionFactory()
return sessionFactory;
【问题讨论】:
【参考方案1】:也许您的映射文件不完整,但其他情况下应该是:
<class name="com.vaannila.uzytkownik.Uzytkownik" table="uzytkownicy">
=> 需要设置全限定类名(带包)
【讨论】:
【参考方案2】:我认为指定完全限定的实体类名是有意义的:
<class name="com.vaannila.uzytkownik.Uzytkownik" table="uzytkownicy">
【讨论】:
【参考方案3】:不要忘记使用映射标签在休眠配置文件中提及您的类作为实体类使用!!
例子:
<session-factory>
//database configeration goes here
<mapping class="org.fbis.models.Form3A"/>
</session-factory>
【讨论】:
【参考方案4】:Stijn Geukens 回答正确,但我想指出有关此问题的更多信息。
我知道导致此问题的原因有两个:找不到实体类
-
首先,正如 Stijn Geukens 所回答的,您的 Hibernate 映射不正确,标签类的 name 属性的值应该是带有包的 Java 类。
其次,如果你的Java类中有boolean字段,则该字段不能以
is
开头。否则,当java Debug模式或Run模式运行时,hibernate会抛出异常getter method is not found...
。但是当它来时到web项目,你以Server Application
运行你的项目,消息变成entity class not found
。这让我困惑了很长时间。所以不要将你的布尔字段命名为is
。
【讨论】:
以上是关于[休眠]错误:找不到实体类:的主要内容,如果未能解决你的问题,请参考以下文章