0

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

我检查了这个问题:Unknown Entity and this one Unknown Entity 2

但我认为它们不是我正在寻找的东西。

配置文件

<?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

休眠实用程序

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 文件中,并且我也@Entity正确使用了注释。

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

4

1 回答 1

0

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

于 2021-03-12T11:13:34.670 回答