You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate @OneToMany注解报错:目标类未映射问题求助

解决Hibernate @OneToMany关联的AnnotationException问题

Hey there, let's break down why you're hitting that org.hibernate.AnnotationException error. The key part of the error message says:

Use of @OneToMany or @ManyToMany targeting an unmapped class: com.neostencil.modules.testmanagement.model.entities.quiz.Quiz.questionList[com.neostencil.modules.testmanagement.model.entities.question.Question]

This means Hibernate doesn't recognize your Question class as a properly mapped entity when it processes the @OneToMany association in the Quiz class.

核心问题所在

Looking at your hibernate.cfg.xml configuration, the mapping entries for your test management entities are missing the neostencil segment in the package path:

<!-- 配置里的错误路径 -->
<mapping class="com.modules.testmanagement.model.entities.quiz.Quiz"/>
<mapping class="com.modules.testmanagement.model.entities.question.Question"/>

But from the error log and your entity code, the actual full package path for these classes starts with com.neostencil.modules... (e.g., com.neostencil.modules.testmanagement.model.entities.question.Question). Hibernate can't locate the Question class because the mapping path is incorrect, so it treats it as an "unmapped class".

解决方案

1. 修正Hibernate配置中的实体映射路径

Update all the mapping entries in hibernate.cfg.xml to use the correct full package paths that include neostencil:

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<!-- 其他原有配置保持不变 -->
<!-- 修正后的映射路径 -->
<mapping class="com.neostencil.framework.model.entities.User" />
<mapping class="com.neostencil.modules.teacher.model.entities.Teacher" />
<mapping class="com.neostencil.framework.model.entities.common.Duration" />
<mapping class="com.neostencil.modules.testmanagement.model.entities.quiz.Quiz"/>
<mapping class="com.neostencil.modules.testmanagement.model.entities.question.Question"/>
</session-factory>
</hibernate-configuration>

2. (可选但推荐)初始化关联集合

To avoid potential null pointer issues when working with the questionList in Quiz, initialize the list when declaring it:

@OneToMany(mappedBy="quiz")
private List<Question> questionList = new ArrayList<>();

3. 验证注解导入(如果问题仍存在)

Double-check that both your Quiz and Question classes are using the correct @Entity annotation. Depending on your Hibernate version, this should be either:

  • javax.persistence.Entity (for older Hibernate versions)
  • jakarta.persistence.Entity (for Hibernate 5.5+ / Jakarta EE)

总结

The root cause here is a simple typo in your Hibernate configuration file—missing the neostencil part of the package path for your entities. Fixing that will let Hibernate recognize both Quiz and Question as mapped entities, resolving the association error.

内容的提问来源于stack exchange,提问作者Kartikeya Garg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:47:23