开发者

[Hibernate]Error: entity class not found:

开发者 https://www.devze.com 2023-03-20 12:11 出处:网络
I get tired of this for a long time. I do not know what caused this error. Here are my files: Uzytkownik.hbm.xml

I get tired of this for a long time. I do not know what caused this error. Here are my files:

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>

I use mysql 5.5.

I get the following error:

 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

This are my classes: 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-开发者_Go百科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;
        }

}


Maybe your mapping file is not complete but other wise it should be:

<class name="com.vaannila.uzytkownik.Uzytkownik" table="uzytkownicy">

=> need to set fully qualified class name (with package)


I think it makes sense to specify full-qualified entity class name:

<class name="com.vaannila.uzytkownik.Uzytkownik" table="uzytkownicy">


Don't forget to mention your class using as a entity classes in hibernate configeration file using the mapping tag !!

Example:

<session-factory>
//database configeration goes here

<mapping class="org.fbis.models.Form3A"/>
</session-factory>


Stijn Geukens answers right,but I want to point out more information about this question.

There are two reasons I know causing this problem: entity class not found

  1. First, As Stijn Geukens answers, Your Hibernate mapping is not right, the value of name attribute for the tag class should be the Java class with package ahead.
  2. Second, if you have boolean filed in your Java class, this field can't start with is.Otherwise, the hibernate throws an exception getter method is not found... when run by Java Debug mode or Run mode.But when it comes to web project and you run your project as Server Application,the message becomes entity class not found.It made me puzzled for a long time.So do not name your boolean field with is ahead.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号