IllegalArgumentException : Hibernate xml 配置中的未知实体

问题描述

我正在尝试测试 Hibernate @Any 映射。但我面临一个问题:IllegalArgumentException: UnkNown entity. 我在 maven 项目中使用基于 hibernate XML 的配置。

我已经检查过这个问题:Unknown Entity 还有这个Unknown Entity 2

但我认为它们不是我要找的东西。

CFG 文件

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="connection.driver_class">com.MysqL.jdbc.Driver</property>
    <property name="hibernate.connection.url">
    jdbc:MysqL://localhost:3306/concretepage</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">2993JGda</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.hbm2ddl.auto">create</property>

    <mapping class="com.concretepage.entity.Boy"/>
    <mapping class="com.concretepage.entity.Girl"/>
    <mapping class="com.concretepage.entity.StudentInfo"/>
    <mapping class="com.concretepage.entity.College"/>
    <mapping package="com.concretepage.entity"/>
   </session-factory>
</hibernate-configuration>

实体类

学生

package com.concretepage.entity;

public interface Student {
    String getName();
    int getAge();
}

男孩

@Entity
@Table(name="boy")
public class Boy implements Student {
    @Id
    private int id;
    @Column(name="name")
    private String name;
    @Column(name="age")
    private int age;
//getter-Setter

HibernateUtil

package com.concretepage;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
     private static SessionFactory sessionFactory ;
     static {
           Configuration configuration = new Configuration().configure();
           StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
                applySettings(configuration.getProperties());
           sessionFactory = configuration.buildSessionFactory(builder.build());
    }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public static void closeSessionFactory() {
        sessionFactory.close();
    }
}

主类/执行类

public class HibernateManyToAnyDemo {
    public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Boy boy = new Boy();
        boy.setId(1);
        boy.setName("Mukesh");
        boy.setAge(30);
        session.persist(boy);
        //other codes...

现在当我运行我面临的程序时:

Exception in thread "main" java.lang.IllegalArgumentException: UnkNown entity: com.concretepage.entity.Boy
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:713)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:696)
    at com.concretepage.HibernateManyToAnyDemo.main(HibernateManyToAnyDemo.java:21)

我不明白为什么会这样。另一件事是,在配置中我给了 hibernate DDL update。所以应该在数据库中创建该表。但是没有在那里创建表。

到目前为止,我根据他们看到的两个问题:必须使用 package-scan,但我认为在我的示例原因中不需要它,我将它们映射到 cfg 文件中,并且我还使用了 {{1 }} 注释正确。

任何帮助,评论将不胜感激。

解决方法

您可以调试到 configurenew Configuration().configure() 方法以查看发生了什么。 Hibernate 尝试在类路径上查找文件名 hibernate.cfg.xml,因此如果您没有看到 DDL 导出发生,则很可能没有找到该文件。如果您使用 Maven 构建您的项目,请确保该文件直接包含在 src/main/resources 中,否则将不会被拾取。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...