我使用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.MySQLDialect com.mysql.jdbc.Driver JDBC:MySQL的://本地主机: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;
}
wHEN RUNNING我有错误org.hibernate.exception.SQLGrammarException,实际上是加载了entiti,但是set&lt;&gt;不.., 当测试错误时 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>
...
答案 0 :(得分:0)
这个错误 org.hibernate.MappingException:表users_roles中的关联引用了未映射的类:com.antoiovi.jobprograms.entity.Roles
来自hibernate配置时不知道实体(Roles)映射,要么你错过了对hibernate配置引入enitity Roles,要么你没有使用正确的名称,请检查configuraiton文件并包含com.antoiovi.jobprograms.entity.Roles作为资源
答案 1 :(得分:0)
我认为已经解决了这个问题。实际上问题是未映射的实体。但这是因为数据库没有“良好形成”。确实数据库ha 3表用户,users_roles,角色;表角色只有一列作为主键(varchar),表user_roles从其列user_roles引用它;当forword enginering ide(在netbeans的情况下)只创建2个实体:'Users'和'Roles';所以我重新设计了模式,放置了一些虚拟主键,并创建了唯一的constaint。现在我有3个实体:Users,UserRoles和Roles。