-->
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.  [ 8 posts ] 
Author Message
 Post subject: null Index column for collection
PostPosted: Sat Sep 24, 2005 7:21 am 
Newbie

Joined: Sat Sep 24, 2005 6:40 am
Posts: 3
Hi all,

I have a bidirectional @OneToMany relation in my model. I'm having trouble with the @IndexColumn annotation not beeing stored when cascading the parent of the relation.


Hibernate version:3.0.5
Annotations version: 3.0-beta2 edr2


Annotations:
Code:
@Entity
@Inheritance
@Table(name="multiple_choice_question")
public class MultipleChoiceQuestion extends Question {
    ....
    @OneToMany(cascade=CascadeType.ALL, mappedBy="question")
    @IndexColumn(name="index", base=0)
   public List<Alternative> getAlternatives() {
      return alternatives;
   }
   ....
}

Code:
@Entity
@Table(name="alternative")
public class Alternative {
    ....
    @ManyToOne()
    @JoinColumn(name="question_id")
    public MultipleChoiceQuestion getQuestion() {
        return question;
    }
    .....
}


Unittest:
Code:
public void testStoreMultipleChoiceQuestionAndAlternatives() throws Exception
        MultipleChoiceQuestion mcq = new MultipleChoiceQuestion("Will it work?", null);
        List<Alternative> alts = new LinkedList<Alternative>();
        alts.add(new Alternative("alt1", "testdata", mcq));
        alts.add(new Alternative("alt2", "testdata", mcq));
        mcq.setAlternatives(alts);
        dao.saveObject(mcq);

        // -- commit session
        commit();
       
        MultipleChoiceQuestion m = (MultipleChoiceQuestion) dao.getObject(MultipleChoiceQuestion.class, mcq.getId());
        assertEquals(2, m.getAlternatives().size());
        dao.removeObject(MultipleChoiceQuestion.class, m.getId());
}


This results in all the properties of the alternative beeing stored correct except the index column that remains null. When reading back the question i get anorg.hibernate.HibernateException:
null index column for collection

I'm using postgres 7.4.7.

I've done the same thing with a unidirectional mapping and that works fine but I'm trying to follow the guidelines and make those mappings bidirectional.

Thanks in advance
/Robert Lerner


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 30, 2005 4:20 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
indexed columns one inverse one to many are not updated. It should be the same with hbm files.

Map the indexed column on the many side and set it manually. Even better, create a utility method addMultiplequestion(index, MQ)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 10:17 am 
Newbie

Joined: Sat Sep 24, 2005 6:40 am
Posts: 3
Hi,
Thanks for your reply Emanuel.
Are index-columns going to be updated in later releases? I can accept this solution for now as our model is pretty modest so far, but in the long run I don't see it as appeling solution.

I don't feel that the "index" property has anything to do in our domainmodel (in most cases) as it's just an ordering property.
I don't like the idea of coding addXXX(int index, XXX x)/addXXX(XXX x)/remove(XXX x) methods for all our bidirectional relations of this type. It's a lot of application- and test-code...
I'm also having some problems acceppting that the getter and setter of the underlying collections is exposed as public methods, I've seen the idea of having the bean property of the collection declared as protected but then I need to provide some construct for iterating the collection in my views which implies even more code.

/Robert


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 5:34 pm 
Newbie

Joined: Wed Oct 05, 2005 5:19 pm
Posts: 1
You could make the OneToMany-side the owning side by removing the "mappedBy" element and setting the @JoinColumn on the ManyToOne-side as "insertable=false" and "updateable=false". You must also add the @JoinColumn on the OneToMany-side.

This way you don't have to handle the index or the collection by hand and it is equivalent to setting ManyToOne-side to "inverse=false" in .hbm.xml

/Jonas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 1:02 pm 
Newbie

Joined: Sat Sep 24, 2005 6:40 am
Posts: 3
Sweet!
Thank you!

/Robert


Top
 Profile  
 
 Post subject: HibernateException retrieving one-to-many Map
PostPosted: Fri Apr 03, 2009 1:15 pm 
Newbie

Joined: Fri Apr 03, 2009 12:48 pm
Posts: 1
I have a parent child relationship between two tables with a one to many relationship from the parent to the child. I have the child configured as a map in the parent. I am getting the following exception after loading the parent class (Campaign) via Hibernate and trying to get the Map of the children (CampaignJob) by calling the getter on the parent object.

org.hibernate.HibernateException: null index column for collection: com.pgi.em.hibernate.Campaign.campaignJobs

I only get this exception sometimes, and so far I have not been able to find a pattern as to when this happens. I have a theory that it happens as a result of new child rows being added while retrieving the collection, but I have not been able to prove that.
The full stacktrace is below along with the hbm.xml for the parent (Campaign) and the child (CampaignJob) classes.

Any guidance on how to resolve this would be appreciated. I have tried loading this through a stateless session and that made no difference. The hbm does not allow me to add an index (a seemingly easy work-around since I have something I can index on) to the map. The call always works when run in a unitTest, but sometimes (about 10% of the time) fails in the real application.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 31, 2007 4:55:31 PM by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="com.pgi.em.hibernate.Campaign" table="CAMPAIGN">
<id name="campaignId" type="java.lang.Long">
<column name="CAMPAIGN_ID" precision="18" scale="0" />
<generator class="sequence">
<param name="sequence">CAMPAIGN_ID_SEQUENCE</param>
</generator>
</id>
<property name="cname" type="string">
<column name="CNAME" not-null="true" />
</property>
<property name="startDte" type="timestamp">
<column name="START_DTE" length="11" not-null="true" />
</property>
<property name="endDte" type="timestamp">
<column name="END_DTE" length="11" />
</property>
<property name="campaignState" type="string">
<column name="CAMPAIGN_STATE" not-null="true" />
</property>
<property name="campaignSubState" type="string">
<column name="CAMPAIGN_SUB_STATE" />
</property>
<map name="campaignJobs" inverse="true">
<key>
<column name="CAMPAIGN_ID" precision="18" scale="0" not-null="true" />
</key>
<map-key column="JOB_ID" type="string"/>
<one-to-many class="com.pgi.em.hibernate.CampaignJob" />
</map>
</class>
</hibernate-mapping>


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 31, 2007 4:55:31 PM by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
<class name="com.pgi.em.hibernate.CampaignJob" table="CAMPAIGN_JOB">
<id name="payloadNumber" type="long">
<column name="PAYLOAD_NUMBER" precision="18" scale="0"/>
<generator class="sequence">
<param name="sequence">PAYLOAD_NUMBER_SEQUENCE</param>
</generator>
</id>
<many-to-one name="campaign" class="com.pgi.em.hibernate.Campaign" fetch="select" update="false">
<column name="CAMPAIGN_ID" precision="18" scale="0" not-null="true" />
</many-to-one>
<property name="jobId" type="string">
<column name="JOB_ID" not-null="false" />
</property>
<property name="jobSize" type="java.lang.Long" update="false">
<column name="JOB_SIZE" precision="18" scale="0"/>
</property>
<property name="jobSize" type="java.lang.Long" update="false">
<column name="JOB_SIZE" precision="18" scale="0"/>
</property>


org.hibernate.HibernateException: null index column for collection: com.pgi.em.hibernate.Campaign.campaignJobs

at org.hibernate.persister.collection.AbstractCollectionPersister.readIndex(AbstractCollectionPersister.java:710)
at org.hibernate.collection.PersistentMap.readFrom(PersistentMap.java:258)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:1008)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:646)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:591)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1994)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:565)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1716)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.AbstractPersistentCollection.readElementByIndex(AbstractPersistentCollection.java:161)
at org.hibernate.collection.PersistentMap.get(PersistentMap.java:146)
at com.pgi.cos.util.CosEventConverter.isViral(CosEventConverter.java:277)
at com.pgi.cos.util.CosEventConverter.convertSent(CosEventConverter.java:265)
at com.pgi.cos.jms.CosEventListener.process(CosEventListener.java:299)
at com.pgi.cos.jms.CosEventListener.onMessage(CosEventListener.java:218)
at org.apache.activemq.ActiveMQMessageConsumer.dispatch(ActiveMQMessageConsumer.java:967)
at org.apache.activemq.ActiveMQSessionExecutor.dispatch(ActiveMQSessionExecutor.java:122)
at org.apache.activemq.ActiveMQSessionExecutor.iterate(ActiveMQSessionExecutor.java:192)
at org.apache.activemq.thread.PooledTaskRunner.runTask(PooledTaskRunner.java:122)
at org.apache.activemq.thread.PooledTaskRunner$1.run(PooledTaskRunner.java:43)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:650)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:675)
at java.lang.Thread.run(Thread.java:595)


Top
 Profile  
 
 Post subject: Re: null Index column for collection
PostPosted: Wed Feb 16, 2011 6:39 am 
Newbie

Joined: Wed Feb 16, 2011 6:33 am
Posts: 1
Hi jcamerin,

I've got exactly the problem you described. It only happens sometimes and I'm not able to reproduce it. Have you solved the problem in the meantime or were you able to reproduce it?

Regards
Helmut


Top
 Profile  
 
 Post subject: Re: null Index column for collection
PostPosted: Wed Apr 06, 2011 9:22 am 
Newbie

Joined: Wed Apr 06, 2011 9:19 am
Posts: 1
You probably use @IndexColumn(name = "SORT_INDEX") in your parent entity and didn't implement SORT_INDEX attribute in child.
Add this code to your child entity class:

Code:
@Column(name = "SORT_INDEX")
    private Integer sortIndex = 0;


    /**
     * @return the sortIndex
     */
    public Integer getSortIndex() {
        return sortIndex;
    }

   
    /**
     * @param sortIndex the sortIndex to set
     */
    public void setSortIndex(Integer sortIndex) {
        this.sortIndex = sortIndex;
    }


Ondrej


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