gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:37:15 25 4
gpt4 key购买 nike

我已经使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com