首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hibernate org.hibernate.MappingException...关联类未映射的类-多个toMany

问Hibernate org.hibernate.MappingException...关联类未映射的类-多个toMany
EN

Stack Overflow用户
提问于 2014-05-07 22:22:49
回答 2查看 1.3K关注 0票数 0

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

代码语言:javascript
复制
<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>
代码语言:javascript
复制
 <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),因为我有错误消息

代码语言:javascript
复制
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)

配置文件是

代码语言:javascript
复制
<hibernate-configuration>
代码语言:javascript
复制
 org.hibernate.dialect.MySQLDialect     com.mysql.jdbc.Driver     jdbc:mysql://localhost:3306/jobprograms?zeroDateTimeBehavior=convertToNull     jobprograms\_ad     xxxxx     thread     true     org.hibernate.hql.classic.ClassicQueryTranslatorFactory     true                    the classes have refernce like this :
代码语言:javascript
复制
'@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.MappingException时,我运行的wHEN有错误org.hibernate.exception.SQLGrammarException,实际上entiti已加载,但set<>没有..:来自表users_roles的关联引用了一个未映射的类: com.antoiovi.jobprograms.entity.Roles,并且未执行HSQL。

我试着在其他帖子中寻找,但我找不到答案。有人能帮我吗?

在配置文件中,所有的实体都被声明了;我想这个promlem在映射中:

代码语言:javascript
复制
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>

和

代码语言:javascript
复制
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>

……

EN

回答 2

Stack Overflow用户

发布于 2014-05-07 23:03:28

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

当hibernate配置不知道实体(角色)映射时,您可能错过了将enitity角色引入hibernate配置,或者没有使用正确的名称,请检查配置文件并将com.antoiovi.jobprograms.entity.Roles作为资源包含在内

票数 0
EN

Stack Overflow用户

发布于 2014-05-08 18:19:12

我想已经解决了这个问题。实际上,问题是未映射的实体。但发生这种情况是因为数据库不是“格式良好”的。实际上,数据库有3个表users,users_roles,roles;表roles只有一列作为主键(varchar),并且表user_roles从它的列user_roles;中引用它;当向前引擎ide (在本例中是netbeans)只创建两个实体时:'Users‘和'Roles';所以我重新设计了模式,放置了一些虚拟主键,并创建了唯一的constaint。现在我有3个实体: Users、UserRoles和Roles。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23520459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档