-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Adding a field to an association table
PostPosted: Fri Aug 20, 2010 9:44 am 
Newbie

Joined: Fri Jul 02, 2010 9:00 am
Posts: 9
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.

Task
Code:
<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>



TaskCompetenceMapping
Code:
<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?


Last edited by rubenski on Sun Feb 10, 2013 5:44 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Adding a field to an association table
PostPosted: Fri Aug 20, 2010 11:35 am 
Newbie

Joined: Fri Jul 02, 2010 9:00 am
Posts: 9
Hah, I solved this by removing the name attribute on the composite-id tag in TaskCompetenceMapping.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.