0

我正在将实体映射从注释应用程序迁移到 hbm.xml

我出错了这个错误并没有找到解决方案。谢谢

java.lang.Exception: Not an entity: class com.enterprise.package.user.domain.User

我在资源文件夹中有 hibernate.cfg.xml,在一个包中有 User.hbm.xml,在另一个包中有 User.class

用户.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.enterprise.package.role.domain.User" table="user">
        <id name="id" type="java.lang.Long">
            <column name="id" />
            <generator class="identity" />
        </id>
        <property name="username" column="username" type="java.lang.String" unique="true"/>
        <property name="password" column="password" type="java.lang.String"/>
        <many-to-one name="role" column="role_id" class="com.enterprise.package.role.domain.Role" not-null="true"/>
    </class>
</hibernate-mapping>

用户类

public final class User {
    private Long   id;
    private String username;
    private String password;
    private Role   role;

    public User() {
    }

    public User(Long id) {
        this.id = id;
    }

    public User(Long id, String username, String password, Role role) {
        this.id       = id;
        this.username = username;
        this.password = password;
        this.role     = role;
    }
...
}


休眠.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">**</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
        <property name="show_sql">true</property>


        <mapping package="com/enterprise/package/user/infrastructure/persistence/hibernate/Role.hbm.xml"/>
        <mapping package="com/enterprise/package/user/infrastructure/persistence/hibernate/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

4

1 回答 1

0

我认为 User 类的包在 User.hbm.xml 中可能是错误的(我们在你的代码片段中看不到这个类的包)......

在 User.hbm.xml 而不是

com.enterprise.package.role.domain.User

或许

com.enterprise.package.user.domain.User.

于 2020-09-18T22:20:48.297 回答