-->
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: Deleting from a List and Update the index column
PostPosted: Sun Jul 02, 2006 3:18 pm 
Newbie

Joined: Wed Jul 20, 2005 3:42 am
Posts: 3
Having a problem with List when deleting due to LIST-INDEX. How can I regenerate the LIST-INDEX column in the DB tables after deleting?

So I was able to map a parent object and children objects using uni-directiona mapping. I used List for my collection and thus, as per the documentation created a list-index column.

All is well, CREATING, UPDATING, and RETRIEVING until I implemented the DELETE function.

I already know the problem, its that when I first create the children (List of object), hibernate fills up the list-index column with a sequence of integers.

However, when you delete an object that is in the middle of the sequence there will be a NullPointerException.

Example you have this data
ID --- Name ---- IDX

1 --- AAA ---- 0
2 --- BBB ---- 1
3 --- CCC --- 2
4 --- CCC --- 3

When you delete ID# 2 which is BBB, the resulting data int the table is
ID --- Name ---- IDX

1 --- AAA ---- 0
3 --- CCC --- 2
4 --- CCC --- 3

and thus, when you retrieve the data or you get the size of the data,
list.size() = 4 when it should be 3. and thus, I keep on getting a NPE.

What I need help on, is that, how will I re-generate the LIST-INDEX COLUMN whenever I delete an item in the middle of the sequence?

After Deleting #2 I can regenerete the IDX table
ID --- Name ---- IDX

1 --- AAA ---- 0
3 --- CCC --- 2
4 --- CCC --- 3

Re generated IDX table
ID --- Name ---- IDX

1 --- AAA ---- 0
3 --- CCC --- 1
4 --- CCC --- 2


By the way, I deleted it this way :
1. Remove Item from List in Parent Object
2. Remove Item
3. Update Parent Object


Need help fast.

Thanks in advance,

ice


Hibernate version: 3.1

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.eti.pojo">
<class name="Booking" table="BOOKING">
<id name="bookingId" column="BOOKINGID" type="java.lang.Integer">
<generator class="native"/>
</id>
<many-to-one name="bookerName" column="BOOKERID" not-null="true" lazy="false"/>
<property name="bookingDate" column="BOOKING_DATE" type="java.util.Date"/>
<many-to-one name="customer" column="CUSTOMERID" not-null="true" lazy="false"/>
<property name="remarks" column="REMARKS" type="java.lang.String"/>
<list name="documents" lazy="false" inverse="false" cascade="all">
<key column="FK_BOOKINGID"/>
<list-index column="DOCUMENTIDX"/>
<one-to-many class="Document"/>
</list>
</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">
<hibernate-mapping package="com.eti.pojo">
<class name="Document" table="DOCUMENT">
<id name="documentId" column="DOCUMENTID" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="description" column="DESCRIPTION" type="java.lang.String"/>
<property name="returned" column="RETURNED" type="java.lang.Boolean"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.eti.ui.transaction.booking.DocumentsTableModel.setTableData(DocumentsTableModel.java:155)
at com.eti.ui.transaction.BookingUI.updateFieldsFromTableRow(BookingUI.java:621)
at com.eti.ui.transaction.BookingUI.tableBookingsMousePressed(BookingUI.java:628)
at com.eti.ui.transaction.BookingUI.access$200(BookingUI.java:38)
at com.eti.ui.transaction.BookingUI$2.mousePressed(BookingUI.java:170)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5485)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.eti.ui.transaction.booking.DocumentsTableModel.getValueAt(DocumentsTableModel.java:78)
at javax.swing.JTable.getValueAt(JTable.java:1902)
at javax.swing.JTable.prepareRenderer(JTable.java:3907)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2070)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1972)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1895)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
at javax.swing.JComponent.paintComponent(JComponent.java:742)
at javax.swing.JComponent.paint(JComponent.java:1005)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JViewport.paint(JViewport.java:728)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintChildren(JComponent.java:842)
at javax.swing.JComponent.paint(JComponent.java:1014)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4963)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4916)
at javax.swing.JComponent._paintImmediately(JComponent.java:4859)
at javax.swing.JComponent.paintImmediately(JComponent.java:4666)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:451)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:114)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)


Name and version of the database you are using:

Oracle XE

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 12:38 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I have just double checked this, and I can verify that the problem is in your code. When you remove an item from the middle of a list, the indices are recomputed, list.size() is correct, etc. All works as expected. In fact, hibernate is a little too conservative in ensuring that all is well, because the generated SQL (for my test tables) is:
Code:
Hibernate: delete from ConfigToLoggerLink where CollectionConfigurationID=?
Hibernate: insert into ConfigToLoggerLink (CollectionConfigurationID, SequenceNumber, LoggerID) values (?, ?, ?)
Hibernate: insert into ConfigToLoggerLink (CollectionConfigurationID, SequenceNumber, LoggerID) values (?, ?, ?)
As you can see, it completely wipes out the link table, reinserting all values, even though it could get away with deleting one row and updating all rows with indices higher than the deleted one.

Could you be working with a detached instance, or a mixture of detached and persistent instances? Can you include (in code tags) the relevant code from updateFieldsFromTableRow()?

_________________
Code tags are your friend. Know them and use them.


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.