java - Hibernate org.hibernate.MappingException ...关联..未映射的类- Manytomany

标签 java hibernate tomcat

我已经使用 netbeans 自动生成代码来创建 Hibernate 配置;所以我有两个像这样映射的表(多对多):

<hibernate-mapping auto-import="true">
<class name="com.antoiovi.jobprograms.entity.Roles" table="roles"   catalog="jobprograms">
    <id name="rolesName" type="string">
        <column name="roles_name" length="20" />
        <generator class="assigned" />
    </id>
    <set name="userses" table="users_roles" inverse="true" lazy="false" fetch="select" cascade="all">
        <key>
            <column name="role_name" length="20" not-null="true" />
        </key>
        <many-to-many entity-name="com.antoiovi.jobprograms.entity.Users">
            <column name="user_name" length="15" not-null="true" />
        </many-to-many>
    </set>
</class>

 <hibernate-mapping  auto-import="true">
<class name="com.antoiovi.jobprograms.entity.Users" table="users" catalog="jobprograms">
    <id name="idusers" type="java.lang.Integer">
        <column name="idusers" />
        <generator class="identity" />
    </id>
    <property name="userName" type="string">
        <column name="user_name" length="15" not-null="true" unique="true" />
    </property>
    <property name="userPass" type="string">
        <column name="user_pass" length="15" not-null="true" />
    </property>
    <property name="firstName" type="string">
        <column name="first_name" length="20" />
    </property>
    <property name="lastName" type="string">
        <column name="last_name" length="25" />
    </property>
    <set name="roleses" table="users_roles" inverse="true" lazy="false" fetch="select" cascade="all">
        <key>
            <column name="user_name" length="15" not-null="true" />
        </key>
        <many-to-many entity-name="com.antoiovi.jobprograms.entity.Roles">
            <column name="role_name" length="20" not-null="true" />
        </many-to-many>
    </set>
    <set name="jobprograms" table="jobprogram" inverse="true" lazy="false" fetch="select" cascade="all">
        <key>
            <column name="users_idusers" not-null="true" />
        </key>
        <one-to-many class="com.antoiovi.jobprograms.entity.Jobprogram" />
    </set>
</class>

我做了一些修改,如你在上面看到的(auto-import=true,lazy=false),因为我有错误消息

rg.hibernate.MappingException: An association from the table users_roles refers to an unmapped class: com.antoiovi.jobprograms.entity.Roles
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1824)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1756)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)

配置文件是

<hibernate-configuration>

org.hibernate.dialect.MySQL方言 com.mysql.jdbc.驱动程序 jdbc:mysql://localhost:3306/jobprograms?zeroDateTimeBehavior=convertToNull jobprograms_ad xxxxx 线 真的 org.hibernate.hql.classic.ClassicQueryTranslatorFactory 真的 这些类有这样的引用:

'@ManyToMany(fetch=FetchType.LAZY, mappedBy="roleses")
public Set<Users> getUserses() {
    return this.userses;
} '

@ManyToMany(fetch=FetchType.EAGER) @JoinTable(name="users_roles", catalog="jobprograms", joinColumns = { @JoinColumn(name="user_name", nullable=false, updatable=false) }, inverseJoinColumns = { @JoinColumn(name="role_name", nullable=false, updatable=false) }) public Set<Roles> getRoleses() { return this.roleses; }

运行时出现错误 org.hibernate.exception.SQLGrammarException,实际上实体已加载,但 set<> 没有.., 测试错误时是 org.hibernate.MappingException:来自表 users_roles 的关联引用了未映射的类:com.antoiovi.jobprograms.entity.Roles,并且 HSQL 未执行。

我试图查看其他帖子,但找不到答案。谁能帮帮我?

在配置文件中,实体都已声明; 我认为问题在映射中:

e <set name="roleses" table="users_roles" inverse="false" lazy="true" fetch="select" >
        <key >
            <column name="user_name" length="15" not-null="true"  />
        </key>
        <many-to-many entity-name="test.Roles" property-ref="rolesName">
            <column name="role_name" length="20" not-null="true" />
        </many-to-many>
    </set>
    <set name="jobprograms" table="jobprogram" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="users_idusers" not-null="true" />
        </key>
        <one-to-many class="test.Jobprogram" />
    </set>

enter  <class name="test.Roles" table="roles" catalog="jobprograms">
    <id name="rolesName" type="string">
        <column name="roles_name" length="20" />
        <generator class="assigned" />
    </id>
    <set name="userses" table="users_roles" inverse="true" lazy="true" fetch="select">
        <key property-ref="rolesName">
            <column name="role_name" length="20" not-null="true" />
        </key>
        <many-to-many entity-name="test.Users" property-ref="userName">
            <column name="user_name" length="15" not-null="true" />
        </many-to-many>
    </set>
</class>

....

最佳答案

这个错误 org.hibernate.MappingException:来自表 users_roles 的关联引用未映射的类:com.antoiovi.jobprograms.entity.Roles

当 hibernate 配置不知道实体(角色)映射时出现,要么你错过了将实体角色引入到 hibernate 配置,要么你没有使用正确的名称,请检查配置文件并包含 com.antoiovi.jobprograms.entity.Roles作为资源

关于java - Hibernate org.hibernate.MappingException ...关联..未映射的类- Manytomany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23520459/

相关文章:

java - CXF 在 WEBSPHERE 上运行

java - Spring Boot 数据 JPA : No qualifying bean of type 'java.util.Set<javax.persistence.EntityManager>' available

ruby-on-rails - 在 Tomcat 中运行 jRuby + Rails

jsp - 如何在 Windows tomcat 中部署在 RHEL 5 中制作的 java tomcat 项目?

java - 如何在 Hibernate 中访问标准中的 Object.field

java - 在 Tomcat 应用程序之间共享一个小程序

java - 将用户输入从 editText 传递到 xml 中的 editText

java - 二级缓存 + Spring Boot + Java Hibernate 中的问题

java - 如何在 Android 上使用 swi-prolog

spring - Grails + hibernate + Controller sessionFactory空对象?