Hello,
I am trying to map "Tasks" to "Competences" in a many-to-many relation and I want to store an extra field (is_core_competence) in the relationship.
Code:
Task
task_id (pk)
name
Competence
competence_id (pk)
name
TaskCompetenceMapping
task_id (pk)
competence_id (pk)
is_core_competence
I first tried the "intermediate entity" solution from Java Persistence with Hibernate (p 303), but when running hbm2java, the inner Id class could not be found, while I am pretty sure it was compiled and on the class path.
Now I am trying a somewhat simpler solution without an inner class. I got this one from a fellow developer. My mappings are.
TaskCode:
<hibernate-mapping>
<class name="nl.mycomp.mycomp.server.domain.Task"
table="SK_TASK">
<id name="taskId" type="long" column="TASK_ID">
<generator class="native" />
</id>
<property name="displayName" type="string" not-null="true"
length="255" column="DISPLAY_NAME" />
<!-- One-to-many to intermediate TaskCompetenceMapping class -->
<set name="taskCompetenceMappings" inverse="true">
<key column="TASK_ID" />
<one-to-many class="nl.mycomp.mycomp.server.domain.TaskCompetenceMapping" />
</set>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping>
<class name="nl.mycomp.mycomp.server.domain.Competence"
table="SK_COMPETENCE">
<id name="competenceId" type="long" column="COMPETENCE_ID">
<generator class="native" />
</id>
<property name="displayName" type="string" not-null="true"
length="255" column="DISPLAY_NAME" />
<!-- One-to-many to intermediate TaskCompetenceMapping class -->
<set name="taskCompetenceMappings" inverse="true">
<key column="COMPETENCE_ID" />
<one-to-many class="nl.mycomp.mycomp.server.domain.TaskCompetenceMapping" />
</set>
</class>
</hibernate-mapping>
TaskCompetenceMappingCode:
<hibernate-mapping>
<class
name="nl.mycomp.mycomp.server.domain.TaskCompetenceMapping"
table="TASK_COMPETENCE">
<composite-id name="taskCompetenceMappingId">
<key-many-to-one name="task" column="TASK_ID" access="field" class="nl.mycomp.mycomp.server.domain.Task" />
<key-many-to-one name="competence" column="COMPETENCE_ID" access="field" class="nl.mycomp.mycomp.server.domain.Competence" />
</composite-id>
<property name="coreCompetence" column="core_competence"
type="boolean" not-null="true">
<column name="CORE_COMPETENCE" default="false" />
</property>
</class>
</hibernate-mapping>
When I run hbm2java with these mappings I get the following error.
[hibernatetool] An exception occurred while running exporter #2:hbm2java (Generates a set of .java files)
[hibernatetool] To get the full stack trace run ant with -verbose
[hibernatetool] Failed in building configuration when adding C:\Users\rvanloen\ECLIPSE WORKSPACES\mycomp\mycomp\src\nl\marktmonitor\mycomp\server\domain\TaskCompetenceMapping.hbm.xml
[hibernatetool] org.hibernate.InvalidMappingException: Could not parse mapping document from file C:\Users\rvanloen\ECLIPSE WORKSPACES\mycomp\mycomp\src\nl\marktmonitor\mycomp\server\domain\TaskCompetenceMapping.hbm.xml
[hibernatetool] org.hibernate.MappingException: class nl.marktmonitor.mycomp.server.domain.TaskCompetenceMapping not found while looking for property: taskCompetenceMappingId[hibernatetool] java.lang.ClassNotFoundException: nl.marktmonitor.mycomp.server.domain.TaskCompetenceMapping
[hibernatetool] A class were not found in the classpath of the Ant task.
[hibernatetool] Ensure that the classpath contains the classes needed for Hibernate and your code are in the classpath.
BUILD FAILED
C:\Users\rvanloen\ECLIPSE WORKSPACES\mycomp\mycomp\build.xml:47: Failed in building configuration when adding C:\Users\rvanloen\ECLIPSE WORKSPACES\mycomp\mycomp\src\nl\marktmonitor\mycomp\server\domain\TaskCompetenceMapping.hbm.xml
Do you know why this happens and what I can do about it?