Java Hibernate:无法加载在 Hibernate 配置 <mapping/> 条目中声明的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23314412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:48:52  来源:igfitidea点击:

Hibernate: Unable to load class declared in Hibernate configuration <mapping/> entry

javahibernate

提问by Zimy

I am completely new in Hibernate and got such an stacktrace:

我是 Hibernate 的新手,并得到了这样的堆栈跟踪:

hql> from TracksEntity 
[2014-04-26 21:13:45] org.hibernate.MappingException: Unable to load class [ model.TracksEntity] declared in Hibernate configuration <mapping/> entry
[2014-04-26 21:13:45] java.lang.ClassNotFoundException: model.TracksEntity
    at java.net.URLClassLoader.run(URLClassLoader.java:372)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:259)
    at org.hibernate.internal.util.ReflectHelper.classForName(ReflectHelper.java:192)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2188)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2139)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2119)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2072)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2045)
    at com.intellij.hibernate.remote.impl.RemoteConfigurationImpl.configure(RemoteConfigurationImpl.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
    at sun.rmi.transport.Transport.run(Transport.java:178)
    at sun.rmi.transport.Transport.run(Transport.java:175)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:174)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:557)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:812)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:671)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:744)

My hibernate.cfg.xml and TracksEntity.java was automatically generated by Intellij Idea:

我的 hibernate.cfg.xml 和 TracksEntity.java 是由 Intellij Idea 自动生成的:

TracksEntity.java: package model;

TracksEntity.java:包模型;

import javax.persistence.*;

@Entity
@Table(name = "TRACKS", schema = "APP", catalog = "")
public class TracksEntity {
    private int id;
    private String artist;
    private int duration;
    private String title;
    private String url;

    @Id
    @Column(name = "ID")
    public int getId () {
        return id;
    }

    public void setId (int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "ARTIST")
    public String getArtist () {
        return artist;
    }

    public void setArtist (String artist) {
        this.artist = artist;
    }

    @Basic
    @Column(name = "DURATION")
    public int getDuration () {
        return duration;
    }

    public void setDuration (int duration) {
        this.duration = duration;
    }

    @Basic
    @Column(name = "TITLE")
    public String getTitle () {
        return title;
    }

    public void setTitle (String title) {
        this.title = title;
    }

    @Basic
    @Column(name = "URL")
    public String getUrl () {
        return url;
    }

    public void setUrl (String url) {
        this.url = url;
    }

    @Override
    public boolean equals (Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TracksEntity that = (TracksEntity) o;

        if (duration != that.duration) return false;
        if (id != that.id) return false;
        if (artist != null ? !artist.equals(that.artist) : that.artist != null) return false;
        if (title != null ? !title.equals(that.title) : that.title != null) return false;
        if (url != null ? !url.equals(that.url) : that.url != null) return false;

        return true;
    }

    @Override
    public int hashCode () {
        int result = id;
        result = 31 * result + (artist != null ? artist.hashCode() : 0);
        result = 31 * result + duration;
        result = 31 * result + (title != null ? title.hashCode() : 0);
        result = 31 * result + (url != null ? url.hashCode() : 0);
        return result;
    }
}

hibernate.cfg.xml:

hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.url">jdbc:derby:/home/zimy/Documents/drbdb;create=true;create=true</property>
        <property name="connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
        <mapping class="model.TracksEntity"/>
    </session-factory>
</hibernate-configuration>

What the problem is? I see that it is not found, but why?

问题是什么?我看到它没有找到,但为什么呢?

采纳答案by chiperortiz

Sounds quite silly but i was putting my entity Class in a package which is not the same i put in the mapping.. and hibernate of course could not find it... just for reference.. maybe a recheck can solve your problem best regards.

听起来很傻,但我把我的实体类放在一个包中,这与我放在映射中的不一样......并且休眠当然找不到它......仅供参考......也许重新检查可以解决你的问题最好的问候.

回答by nik

write in this pattern

 <session-factory>

    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/dairy
    </property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.current_session_context_class">thread</property>

    <!-- MAPPING -->
    <!-- <mapping class="com.om.model.Test" /> -->
    <mapping class="com.om.model.RateCalculationBean" />

</session-factory>

and also no need to mention scema

也无需提及 scema

回答by Mohit

Worked for me:

为我工作

Changing position of mapping tag in hibernate.cfg.xml for class which was not loading previously.

为之前未加载的类更改 hibernate.cfg.xml 中映射标记的位置。

I kept that at top now.

我现在把它放在首位。

回答by Kasi subramaniam

I had a similar problem when the mapping file wouldn′t load correctly. The answer to this problem seems to be that we need to use the correct import statements in our Model classes.

当映射文件无法正确加载时,我遇到了类似的问题。这个问题的答案似乎是我们需要在我们的模型类中使用正确的导入语句。

Changing the import to import javax.persistence.Entityinstead of using the one from hibernate package ( ie org.hibernate.annotations.Entity), fixed the issue in my case.

将导入更改为import javax.persistence.Entity而不是使用来自 hibernate 包(即org.hibernate.annotations.Entity)的导入,解决了我的问题。

This issue is a real pain , and I spent more than 1/2 day looking at all corners, when a simple import fixed the issue.

这个问题真的很痛苦,当一个简单的导入解决了这个问题时,我花了超过 1/2 天的时间查看各个角落。

Hope this helps.

希望这可以帮助。

回答by arn-arn

i recently experienced this. I checked everything and didn't find anything that might have caused it. In desperation, i just moved the "/>" closer in the mapping and saved it. And it worked. Mysterious...

我最近经历了这个。我检查了一切,没有发现任何可能导致它的原因。无奈之下,我只是将映射中的“/>”移近并保存了它。它奏效了。神秘...

回答by cljk

My mistake was a camel case problem. I had an embedded entity "IndexPK" and wrote "IndexPk" in the mapping file. I compared it ~20 times.... till I figured this out.

我的错误是骆驼案例问题。我有一个嵌入式实体“IndexPK”并在映射文件中写了“IndexPk”。我比较了大约 20 次......直到我想通了。

回答by Atequer Rahman

In my case, I have just redeployed project. Then problem solved!

就我而言,我刚刚重新部署了项目。然后问题解决了!

回答by Abhishek KM

You can do it by two methods, 2nd one definitely works.

您可以通过两种方法来做到这一点,第二种方法绝对有效。

  1. Either adding a mapping entry in hibernate.cfg.xml should make it work
  1. 在 hibernate.cfg.xml 中添加映射条目应该可以使它工作

<mapping class="com.sample.yourClass" />

<mapping class="com.sample.yourClass" />

  1. In your code, After you create Configuration object, Do add class like,
  1. 在您的代码中,创建配置对象后,请添加类,例如,
Configuration config = new Configuration();
config.addClass(com.sample.yourClass);
Configuration config = new Configuration();
config.addClass(com.sample.yourClass);