-->
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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: another list-index problem: list_index = 0
PostPosted: Thu Dec 07, 2006 6:59 am 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
Heya,

I'm trying to map this FilterChain 1 <---> 0..* AbstractFilter
using a list, because filter order does matter.

My problem is that my list-index is not automaticaly managed by hibernate, which result in when i try to add a concreat filter to my list and save the FilterChain the forder column is always set to 0, which break the list semantic.

Hibernate version:3.2.0ga

Mapping documents:
FilterChain.hbm.xml
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="fr.xx.model.FilterChain"
        table="FILTERCHAIN"
        dynamic-update="true"
    >

        <id
            name="id" column="id" type="java.lang.Long" unsaved-value="0">
            <generator class="sequence"/>
        </id>

        <property name="comment" type="text" update="true" insert="true" column="comment" not-null="false"/>

        <property name="date" type="java.util.Date" update="true" insert="true" column="date" not-null="true"/>

      <!-- from http://forum.hibernate.org/viewtopic.php?t=943384 -->
        <list name="filters"
            lazy="true"
            inverse="false"
            cascade="all,delete-orphan"
        >
            <key column="chain_id" not-null="true" update="false"/>
            <list-index column="forder" />
            <one-to-many class="fr.xx.model.AbstractFilter"/>
        </list>

        <property name="name" type="java.lang.String" update="true" insert="true" column="name" not-null="true" unique="true"/>

        <property name="owner" type="java.lang.String" update="true" insert="true" column="owner" not-null="true"/>
    </class>

</hibernate-mapping>



AbstractFilter.hbm.xml
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="fr.xx.model.AbstractFilter" table="FILTER" discriminator-value="A">
        <cache usage="read-write" />

        <id name="id" column="id" type="java.lang.Long" unsaved-value="0">
            <generator class="sequence"/>
        </id>

        <discriminator column="TYPE" type="string" />
   
        <many-to-one
            name="key"
            class="fr.xx.model.DefaultLabelKey"
            cascade="none"
            fetch="select"
            update="true"
            insert="true"
            column="key_id"
            not-null="true"
            lazy = "false"           
        />

      <!-- from http://forum.hibernate.org/viewtopic.php?t=943384 -->
        <many-to-one name="chain"
            column="chain_id"
            class="fr.xx.model.FilterChain"
            not-null="true"
            insert="false"
            update="false"
        />

      <!-- read-only property used by hibernate to manage list order  -->
      <property name="forder" column="forder" type="int" insert="false" update="false" not-null="true"/>
      
        <subclass
            name="fr.xx.model.GroupFilter"
            discriminator-value="G"
        />
       
        <subclass
            name="fr.xx.model.ReduceFilter"
            discriminator-value="R"
        >
        <property name="revert" type="boolean" update="true" insert="true" column="revert"/>
        <property name="value" type="java.lang.String" update="true" insert="true" column="value" />

      </subclass>

    </class>

</hibernate-mapping>



Name and version of the database you are using: PostgreSQL 8.0

The generated SQL (show_sql=true):
Code:
Hibernate: insert into FILTER (key_id, chain_id, forder, revert, value, TYPE, id) values (?, ?, ?, ?, ?, 'R', ?)
Hibernate: update FILTERCHAIN set comment=?, date=?, name=?, owner=? where id=?
Hibernate: update FILTER set key_id=?, revert=?, value=? where id=?
Hibernate: update FILTER set key_id=?, revert=?, value=? where id=?


relevant Database Schema
Code:
...
create table FILTER (id int8 not null, TYPE varchar(255) not null, key_id int8 not null, chain_id int8 not null, forder int4 not null, revert bool, value varchar(255), primary key (id));
create table FILTERCHAIN (id int8 not null, comment text, date timestamp not null, name varchar(255) not null unique, owner varchar(255) not null, primary key (id));
alter table FILTER add constraint FK7B9BBF78599119ED foreign key (key_id) references LABELKEY;
alter table FILTER add constraint FK7B9BBF78CFBD0B48 foreign key (chain_id) references FILTERCHAIN;



Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 12:07 pm 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
i found the pb. I was mapping the forder attr, which is not the right thing to do here.

Adding a new AbstractFilter works fine, Hibernate properly set forder.

the new pb is how to remove an instance which is not the Last one, and how to reorder.

Any help would be much appreciate


new mappings
FilterChain.hbm.xml
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="fr.xx.beans.FilterChain"
        table="FILTERCHAIN"
        dynamic-update="true"
    >

        <id
            name="id" column="id" type="java.lang.Long" unsaved-value="0">
            <generator class="sequence"/>
        </id>

        <property name="comment" type="text" update="true" insert="true" column="comment" not-null="false"/>

        <property name="date" type="java.util.Date" update="true" insert="true" column="date" not-null="true"/>

      <!-- from http://forum.hibernate.org/viewtopic.php?t=943384 -->
        <list name="filters"
            lazy="true"
            inverse="false"
            cascade="all,delete-orphan"
        >
            <key column="chain_id" not-null="true" update="false"/>
            <list-index column="forder" />
            <one-to-many class="fr.xx.beans.AbstractFilter"/>
        </list>

        <property name="name" type="java.lang.String" update="true" insert="true" column="name" not-null="true" unique="true"/>

        <property name="owner" type="java.lang.String" update="true" insert="true" column="owner" not-null="true"/>
    </class>

</hibernate-mapping>


AbstractFilter.hbm.xml
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="fr.xx.beans.AbstractFilter" table="FILTER" discriminator-value="A">
        <cache usage="read-write" />

        <id name="id" column="id" type="java.lang.Long" unsaved-value="0">
            <generator class="sequence"/>
        </id>

        <discriminator column="TYPE" type="string" />
   
        <many-to-one
            name="key"
            class="fr.xx.beans.DefaultLabelKey"
            cascade="none"
            fetch="select"
            update="true"
            insert="true"
            column="key_id"
            not-null="true"
            lazy = "false"           
        />

      <!-- from http://forum.hibernate.org/viewtopic.php?t=943384 -->
        <many-to-one name="chain"
            column="chain_id"
            class="fr.xx.beans.FilterChain"
            not-null="true"
            insert="false"
            update="false"
        />
      
        <subclass
            name="fr.xx.beans.GroupFilter"
            discriminator-value="G"
        />
       
        <subclass
            name="fr.xx.beans.ReduceFilter"
            discriminator-value="R"
        >
        <property name="revert" type="boolean" update="true" insert="true" column="revert"/>
        <property name="value" type="java.lang.String" update="true" insert="true" column="value" />

      </subclass>

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 11:27 am 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
well, i still have no clue on how to manage persisting reordering.

By the way, removing an item which is not the final one, does break.

Looks like hibernate has no clue the collection has changed.

how can i tell hibernate to regenerate list-index.

Should i put some code in my DAO or something?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 19, 2006 10:39 am 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
well i find out the doc here http://www.hibernate.org/hib_docs/v3/re ... tional-m21

and my mapping is really the same

Code:
<!-- from http://forum.hibernate.org/viewtopic.php?t=943384 -->
<!-- from hibernate doc http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-bidirectional-m21 -->
        <list name="filters"
            lazy="true"
            inverse="false"
            cascade="all,delete-orphan"
        >
            <key column="chain_id" not-null="true" update="false"/>
            <list-index column="forder" />
            <one-to-many class="fr.xx.AbstractFilter"/>
        </list>


Code:
<!-- from http://forum.hibernate.org/viewtopic.php?t=943384 -->
        <many-to-one name="chain"
            column="chain_id"
            class="fr.xx.FilterChain"
            not-null="true"
            insert="false"
            update="false"
        />


this sentence puzzle me:
Quote:
If you use a List (or other indexed collection) you need to set the key column of the foreign key to not null, and let Hibernate manage the association from the collections side to maintain the index of each element (making the other side virtually inverse by setting update="false" and insert="false"):


What does this means for Dao methods?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 6:18 am 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
a list of related topics:

http://forum.hibernate.org/viewtopic.php?t=958550
http://forum.hibernate.org/viewtopic.php?t=968743
http://forum.hibernate.org/viewtopic.php?t=942151
http://forum.hibernate.org/viewtopic.php?t=968508
http://forum.hibernate.org/viewtopic.php?t=943384

Unfortunatly none gives any working solutions...

@Hibernate Team: probably the Doc need an upgrade with a full working example (entities, mapping, database schema, dao code) as a lot of users get the same issue.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 9:58 am 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
new hints;

my mapping now looks like
Code:
        <list name="filters" cascade="all,delete-orphan">
            <key column="chain_id" not-null="true" update="false"/>
            <list-index column="forder" />
            <one-to-many class="fr.xx.AbstractFilter"/>
        </list>


Code:
       <many-to-one name="chain"
            column="chain_id"
            class="fr.xx.FilterChain"
            not-null="true"
            insert="false"
            update="false"
        />


according to Maning's Java Persistance with Hibernate chap 7.2.1 p292.

But my test code still does not work :(

here are my code outputs:
Code:
Hibernate: update FILTERCHAIN set comment=?, date=?, name=?, owner=? where id=?
Hibernate: update FILTER set key_id=?, revert=?, value=? where id=?
DEBUG main org.hibernate.persister.collection.AbstractCollectionPersister - Updating rows of collection: fr.xx.FilterChain.filters#1
DEBUG main org.hibernate.persister.collection.AbstractCollectionPersister - done updating rows: 0 updated
Hibernate: delete from FILTER where id=?


and the test code:
Code:
List filters = c.getFilters();
      
Assert.assertNotNull(filters);
Assert.assertEquals(2, filters.size());      

IFilter filterToRemove =  (IFilter) filters.get(0);
      
c.removeFilter(filterToRemove);
      
getFilterChainDao().save(c);

actualTable = getConnection().createQueryTable( "FILTER", "SELECT forder FROM FILTER" );
      
assertEquals("first filter forder",0, actualTable.getValue(0, "forder"));      


Last edited by meshenka on Tue Jan 02, 2007 5:44 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 11:33 am 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
Hibernate 3.2.1 did not fix it :(


Top
 Profile  
 
 Post subject: SOLVED
PostPosted: Thu Dec 21, 2006 12:24 pm 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
SOLVED!!!!

this mapping is wrong:
[code]
<list name="filters" cascade="all,delete-orphan">
<key column="chain_id" not-null="true" update="false"/>
<list-index column="forder" />
<one-to-many class="fr.xx.AbstractFilter"/>
</list>
[code]

update="false" MUST BE REMOVED

Correct mapping:
[code]
<list name="filters" cascade="all,delete-orphan">
<key column="chain_id" not-null="true"/>
<list-index column="forder" />
<one-to-many class="fr.xx.AbstractFilter"/>
</list>
[code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 26, 2006 2:47 am 
Newbie

Joined: Tue Dec 26, 2006 2:44 am
Posts: 1
Hi,
I'm having exactly the same problem... what are your 2 complete mapping files? Just by using your last snippet I cannot get it to work. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 02, 2007 5:45 am 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
Code:
        <list name="filters" cascade="all,delete-orphan">
            <key column="chain_id" not-null="true"/>
            <list-index column="forder" />
            <one-to-many class="fr.xx.AbstractFilter"/>
        </list>


Code:
       <many-to-one name="chain"
            column="chain_id"
            class="fr.xx.FilterChain"
            not-null="true"
            insert="false"
            update="false"
        />


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 12, 2007 11:01 am 
Newbie

Joined: Mon Mar 12, 2007 8:46 am
Posts: 1
Hi,

I tried to apply your solution. But it doesn't work for me.
Reason: You define 'insert' and 'update' for the to-one relation to 'false'.
Because of that each attempt to save a new Filter item results in a
GenericJdbcException: could not insert ...

I tried to modify the mapping to a bi-directional relation with 'inverse' definition - as it is documented in the reference manual:

To-many relation of the parent entity:
Code:
    <list name="filters" cascade="all,delete-orphan">
      <key column="chain_id" /> 
      <list-index column="forder"/> 
      <one-to-many class="fr.xx.AbstractFilter"/>
    </list>

Corresponding To-one relation of the child:
Code:
    <many-to-one name="chain" column="chain_id" 
         class="fr.xx.FilterChain"
    />


After that change it was possible to store new filter items.
But: The filter index was not maintained by hibernate :-(

I suppose that this is a real bug in Hibernate (3.2.2).
The reference manual promises that the list index will be maintained automatically. But the implementation doesn't.

Do you agree?
Regards, Olaf


Top
 Profile  
 
 Post subject: The fix, for my version of this problem.
PostPosted: Mon Feb 04, 2008 8:20 pm 
Newbie

Joined: Mon Feb 04, 2008 7:44 pm
Posts: 1
Code:

My problem is deletions show in the log, but dont happen in the database.
For whatever reason, the fix is to use a transaction in the code.

My tables are parent 1 to 0+ child.

parent
-------
parent_id (PK)

child
-----
child_id (PK)
parent_id (not null)


Here's the hibernate config:

    <many-to-one name ="parent"
                 class   ="Parent"
                 column  ="parent_id"
                 insert  ="false"
                 update  ="false"
                 not-null="true" />

    <set name   ="child" >
        <key column  ="parent_id"
             not-null="true" />
        <one-to-many class="Child" />
    </set>

Here's the code:

    Session session = HibernateUtil.currentSession();
    session.beginTransaction();

    q =session.createQuery(query);
    parent = q.list().iterator().next()
    child = parent.getChildren().iterator().next();

    child.setParent(null);
    parent.getChildren().remove(child);
    session.delete(child);

    HibernateUtil.commitTransaction();
    HibernateUtil.closeSession();

Be sure to enable sql logging:

<hibernate-configuration>
    <session-factory>
        <property name="show_sql">true</property>

The console logging shows roughly this:

    from parent
    left outer join child
    where parent_id = ?

    select parent_id
    from child
    where child_id = ?

    delete
    from child
    where child_id = ?

Here's where the problem occurs.
The hibernate logging shows a deletion query.

My guess is there's an exception that's swallowed somewhere.
I'm using an older version of hibernate, this may be solved already:
    1971632 Aug 21 10:29 hibernate3.jar

This fix for me, is replace this java code:
    session.delete(child);
with this:
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            session.delete(child);
            tx.commit();
        }
        catch (HibernateException e) {
            try {
                if (tx != null)
                    tx.rollback();
            } catch (Exception ee) {}

            System.out.println("Exception deleting object:" + e);
        }


I found several combinations of hibernate configurations that would work.
Some of them included the middle logging output of:
    select parent_id
while others just output the first and 3rd sql.
If you are not getting a hibernate or sql exception,
then this post might be for you:)

If you have:
    cannot update (child.child_id) to NULL
that's a config error, and is answered above and in the other links.


jkuipers post seems to be about the same issue:
http://forum.hibernate.org/viewtopic.php?t=943384


Thanks to meshenka and others for gathering lists of related posts.
These helped me.



Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 29, 2008 1:24 pm 
Newbie

Joined: Tue Apr 29, 2008 11:28 am
Posts: 2
oboede wrote:
After that change it was possible to store new filter items.
But: The filter index was not maintained by hibernate :-(



I'm also having an issue with this. The index is not being maintained by hibernate. I put some code in to maintain it myself, and got a ConstraintViolationException when I deleted one of the child objects.

This worked under Hibernate 3.1.3, and we are rolling back to that version until this bug is fixed.


Top
 Profile  
 
 Post subject: null-index column for list
PostPosted: Sat May 31, 2008 7:25 pm 
Newbie

Joined: Sat May 31, 2008 7:14 pm
Posts: 1
I am having same problem. Everything works great except the thing that hibernate inserts null for list-index. I even tried with Hibernate 3.1.3 but still it inserts null.

Do anyone know problem of the solution.

here is my mapping file


Code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping auto-import="true" default-lazy="false">

   <class name="AddressLocation" table="locations">
      <id name="id" column="id">
         <generator class="native"/>
      </id>   
      
      
      <component name="address" class="Address">
            <property name="street" type="string">
                <column name="street"/>
            </property>
            <property name="city" type="string">
                <column name="city"/>
            </property>
            <property name="state" type="string">
                <column name="state"/>
            </property>   
                   
       </component>   

        <many-to-one name="person" column="person_id" class="Person" ></many-to-one>
       
   </class>
   
   <class name="Person" table="person" >
      <id name="id" column="id" type="integer">
         <generator class="native"/>
      </id>
      <property name="name" column="name"/>
      
<list name="address" inverse="true" cascade="all-delete-orphan">
         <key column="person_id" not-null="true"/>
         <list-index column="idx"/>
         <one-to-many class="AddressLocation"/>
      </list>
      
   </class>
   
</hibernate-mapping>



Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 10:29 am 
Newbie

Joined: Mon Jun 23, 2008 6:51 am
Posts: 2
try this:

http://www.hibernate.org/116.html?cmd=c ... de=1195#A8

I particular the section: Can't I have a bidirectional association using a List?

Cheers


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

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.