带有注释的休眠映射异常

时间:2019-01-16 11:49:27

标签: java mysql hibernate orm

我正在尝试使用Hibernate和MySQL。我创建了一些测试实例并试图保存它们,但是遇到了

  

hibernate.MappingException:未知实体:....类别。

Category类如下所示:

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name = "categories_table")
public class Category {

    @Id
    @Column
    private String code;
    @Column
    private String name;
    @OneToMany(mappedBy = "category")
    private Set<Addon> addons;

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>
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/xa04?createDatabaseIfNotExist=true</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.hbm2ddl.auto">update</property>
 </session-factory>
</hibernate-configuration>

1 个答案:

答案 0 :(得分:2)

您需要使用包含全限定类名或Wildacrds的适当映射来更新配置:

<hibernate-configuration>
 <session-factory>
  ...
  <mapping class="com.mypackage.Category" />
 </session-factory>
</hibernate-configuration>