-->
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.  [ 3 posts ] 
Author Message
 Post subject: Help needed declaring a list that contains subclasses
PostPosted: Fri Apr 14, 2006 6:39 pm 
Newbie

Joined: Fri Apr 07, 2006 6:04 pm
Posts: 12
Hibernate version: 3.1

Due to some excellent help from a forum member I was able to define a class containing a subclass that contains a list of the class:

Code:
<class name="ActivityStep" lazy="true">
      <id name="stepID">
         <generator class="native"/>
      </id>
      <discriminator column="classID"/>
      <property name="Type" not-null="true"/>
[b]...[/b]
      <subclass name="ForStep">
         <property name="InitialCondition" not-null="false"/>
         <property name="EndCondition" not-null="false"/>
         <property name="StepDelta" not-null="false"/>
         <property name="Condition" column="ConditionValue" not-null="false"/>
         <list name="Steps" cascade="all,delete-orphan">
            <key column="stepID"/>
            <index column="listIndex"/>
            <many-to-many class="ActivityStep" column="stepchildID"/>
         </list>
      </subclass>
[b]...[/b]
</class>



and hibernate 'likes' this and I can create instances of Activitystep subclasses and their lists and save them into the database just fine.

The problem arises when I have another different class that wants to declare/use a list of Activitystep. My current definition is:

Code:
   <class table="ACTIVITIES" name="Activities">
      <id name="activityGroup">
         <generator class="native"/>
      </id>
      <property name="Name" type="text" not-null="true"/>
[b]...[/b]
      <list name="Steps" cascade="all,delete-orphan" >
         <key column="activityGroup" not-null="true"/>
         <list-index column="stepIndex"/>
         <many-to-many class="ActivityStep" column="stepID" unique="true"/>
      </list>
   </class>


The list is supposed to be a list of steps, that may themselves contain a list of steps, that may also contain ...

At runtime, I get the following error:

Code:
WARN  - SQL Error: 1364, SQLState: HY000
ERROR - Field 'stepchildID' doesn't have a default value


Is there a better or proper way to define the Steps list to achieve my goal. Obviously I'm still a relative newbie at Hibernate.

Any help would be appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 14, 2006 8:06 pm 
Regular
Regular

Joined: Wed Jul 07, 2004 2:00 pm
Posts: 64
I believe that your Lists that contain many-to-many relationships must specify the table attribute, to indicate the cross-reference table.

EG,

In your first example showing ActivityStep and ForStep, you could use a table named ForStepParents. This contains 3 columns: parent_id (foreign key to a ForStep), child_id (foreign key to the child ActivitySteps), and listIndex. In this case, the list tag should be:
<list name="Steps" table="ForStepParents" cascade="all,delete-orphan">
<key column="parent_id"/>
<index column="listIndex"/>
<many-to-many class="ActivityStep" column="child_id"/>
</list>

Your second example, with Activites and Steps, would also need a similar table.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 16, 2006 1:18 am 
Newbie

Joined: Fri Apr 07, 2006 6:04 pm
Posts: 12
The ActivityStep table has multiple subclasses (lots actually) but works fine without specifying a table whe your just creating lots of ActivityStep entities. One index table is created automatically.

I think the problem is that the stepchildID must exist before the Activities list can be stored as there is a constraint based upon that key but the stepchildID gets created as part of the list (or something to that effect). Is this a chicken and egg problem?

As a follow-on to the first message is that Activities sort-of wants to define a list and just refer to another class for the contents. The definition of ActivityStep should not have to be redone. Perhaps I am doing this wrong. Maybe what I want can't be done.

Again, still looking for help adn all is appreciated.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.