-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem using <index column
PostPosted: Thu Aug 16, 2007 4:47 am 
Newbie

Joined: Wed Aug 15, 2007 9:20 am
Posts: 5
Hi

This post was first posted in the Tools forum but after suggestion from max on the Hibernate Team I've reposted it here (would have moved it if I could). Any moderator, feel free to remove the post in the Tools forum.

The class FoodListMeal has a List of FoodListItem objects. To keep them ordered I'm using <index column="position"/>.

Normally a user has a couple of FoodListItems in a FoodListMeal, i.e. the food_list_item table contains a couple of rows for a particular FoodListMeal.

Now I discovered a user that has two rows where the first row has the value 0 in the position column and the second row has the value 2. The value 1 is missing.
The user originally had three rows with the positions: 0, 1 and 2. Now the middle row is gone and only the two rows with positions 0 and 2 are left.

When the List of FoodListItems is loaded in FoodListMeal, the List gets loaded with two FoodListItem objects and one null object.
foodListItems = [FoodListITem, null, FoodListItem].

I don't know why the user is suddenly lacking a row in the table but is there a way to make Hibernate not load the missing row? Because the null reference later leeds to a NullPointerException.

Hibernate version: 3.1.3

Mapping documents:
Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

    <class name="com.mypackage.FoodListMeal" table="food_list_meal">
   
        <id name="id"
            column="food_list_meal_id"
            type="int"
            unsaved-value="-1">
            <generator class="native"></generator>
        </id>

        <version name="version" column="version" type="int"/>

        <property name="mealCategoryId"
           type="java.lang.String"
            update="true"
            insert="true"
            column="mealCategoryId"/>

        <list name="foodListItems"
           lazy="true"
           inverse="true"
            cascade="all-delete-orphan"
            batch-size="28">
            <key column="food_list_meal_id"></key>
            <index column="position"/>
            <one-to-many class="com.mypackage.FoodListItem"/>
        </list>

        <many-to-one name="parent"
           class="com.mypackage.FoodListDay"
            cascade="save-update"
            outer-join="auto"
            update="true"
            insert="true"
            column="food_list_day_id"
            not-null="true"/>

    </class>

</hibernate-mapping>


Full stack trace of any exception that occurs:

Name and version of the database you are using:
MySQL 5


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 17, 2007 11:23 am 
Newbie

Joined: Thu Aug 16, 2007 2:13 pm
Posts: 1
The possible reason is that you mark the lineitems list as inverse="true" for the one-to-many relationship as you want to make it bidirectional. Read page 292-294 of book "Java Persistence with Hibernate" authored by Bauer and King.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 2:23 am 
Newbie

Joined: Wed Aug 15, 2007 9:20 am
Posts: 5
TerryW wrote:
The possible reason is that you mark the lineitems list as inverse="true" for the one-to-many relationship as you want to make it bidirectional. Read page 292-294 of book "Java Persistence with Hibernate" authored by Bauer and King.


Hi, thanks for your help. I had a look at "Bidirectional associations" in Hibernate Quickly. Is says that Hibernate only supports bidirectional one-to-many collections for sets and bags. We are using list.

It also says that "If you try to map a list, map or arrays as inverse, the index column of the collection will not be populated when it is persisted. Hibernate won't tell you about this mistake - it will just fail silently.".

So I guess we have a bug in our mapping. Thanks for pointing it out. We will now try to modify this to use a set instead. Maby it will solve the problem.

Thanks!

Best regards,
Mattias


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 21, 2007 5:41 am 
Newbie

Joined: Thu Dec 21, 2006 5:18 am
Posts: 3
Location: Bangalore
May this will help you if you still want to use List. http://hibernate.org/116.html#A12


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