-->
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: Difficulty adding @IndexColumn to mapped List
PostPosted: Tue Mar 04, 2008 4:31 pm 
Newbie

Joined: Tue Mar 04, 2008 4:20 pm
Posts: 2
Hi All,

I am in a situation where I need to add an @IndexColumn annotation to a List that only had @CollectionOfElements previously. Adding the annotation is easy, the difficult part is writing the sql migration to insert the correct values into the new IndexColumn.

For instance the table underlying the property used to look like this:

--------------------------------
| DataRow_id | element |
--------------------------------
| 1 | foo |
| 1 | bar |
| 1 | baz |
| 2 | aaa |
| 2 | bbb |
| 2 | ccc |
--------------------------------

After adding the @IndexColumn(name="dataindex") annotation I need it to look like this so hibernate reads and orders the data correctly:

----------------------------------------------
| DataRow_id | element | dataindex |
----------------------------------------------
| 1 | foo | 0 |
| 1 | bar | 1 |
| 1 | baz | 2 |
| 2 | aaa | 0 |
| 2 | bbb | 1 |
| 2 | ccc | 2 |
----------------------------------------------

The best I've been able to do in sql alone (across oracle, mysql, sqlserver & derby databases) is this:

----------------------------------------------
| DataRow_id | element | dataindex |
----------------------------------------------
| 1 | foo | 1 |
| 1 | bar | 2 |
| 1 | baz | 3 |
| 2 | aaa | 4 |
| 2 | bbb | 5 |
| 2 | ccc | 6 |
----------------------------------------------

However, the issue with this data is that hibernate loads the second row as a List of size 7, with elements 0-3 being null, instead of a List of
size 3 no null elements. I can write some java code to "patch-up" the dataindex values at this point, but I would much prefer making the migration in sql alone to avoid a support headache with some customers.

Any help someone may have crafting the query to create the dataindex values for hibernate's @IndexColumn semantics would be greatly appreciated.

Thanks in advance,
Evan Leonard


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 04, 2008 5:13 pm 
Newbie

Joined: Sun Mar 02, 2008 3:43 pm
Posts: 9
Tried that in mysql5.0

I have a table test with column id,element, dataidx
I suppose the table in your last situation ie dataix goes from 1 to 6.
I assume that for a given id( or dataRow_id) I get a dataIndex in a specific range with no holes (like 4 5 6 and not like 4 7 9)

I need to duplicate table test in test2 because mysql doesn't support having a select on the same table than the update

Code:
insert into test2 select * from test;

update test t set dataidx=dataidx-(select min(t2.dataidx) from test2 t2  where t2.id=t.id);


You can drop the duplicate table.

Hope it helped.


Top
 Profile  
 
 Post subject: Thanks!
PostPosted: Tue Mar 04, 2008 6:45 pm 
Newbie

Joined: Tue Mar 04, 2008 4:20 pm
Posts: 2
That's great. I have to make a couple small changes to guarantee your assumption, but after that your suggestion works great. Thank so much.

-Evan


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.