-->
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: Mapping array String[] - nulls inside are not inserted
PostPosted: Thu Sep 22, 2005 8:31 am 
Newbie

Joined: Thu Sep 22, 2005 8:15 am
Posts: 1
Hello!
I need to persist an array of String, which can contain null values. I expected that loaded array would be same as persised, but Hibernate doesn't save last null values (only these which precede not null values).
Can I set something in mapping or handle it another, but simple way?

Here is a simple test code and mapping:


//save
hsession = factory.openSession();
t = hsession.beginTransaction();
OneDimensionalStringArrayBean odsab = new ad.persistence.OneDimensionalStringArrayBean();
odsab.setList2(new String[]{null,"3",null,null});
hsession.save(odsab);
long id = odsab.getId();
hsession.flush();
t.commit();
hsession.close();

//load
hsession = factory.openSession();
t = hsession.beginTransaction();
odsab = (OneDimensionalStringArrayBean) hsession.load(ad.persistence.OneDimensionalStringArrayBean.class,new Long(id));
String[] array = odsab.getArray2();
System.out.println("length:"+array.length);
for(int i=0;i<array.length;i++)
System.out.println(array[i]);
hsession.flush();
t.commit();
hsession.close();

Output is:
length:2
null
3

instead of:
length:4
null
3
null
null



<hibernate-mapping>
<class name="ad.persistence.OneDimensionalStringArrayBean" table="OneDimensionalStringArrayBean">
<id name="id" type="long" column="id" unsaved-value="none">
<generator class="identity" />
</id>
<array name="list2" table="OneDimensionalStringArrayBean_list2" cascade="all-delete-orphan" >
<key column="list_id" />
<index column="list_index" />
<element column="value" type="string" />
</array>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 4:16 pm 
Newbie

Joined: Fri Jul 30, 2004 7:07 pm
Posts: 12
I have the same problem.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 9:14 am 
Newbie

Joined: Thu May 03, 2007 6:24 am
Posts: 8
Same here ... and in a way obvious. How should Hibernate now the length if it is omitting the null elements / values.

A simple workaround to access individual elements in the array is still possible:

Code:
@Entity
public class Foo {

    @CollectionOfElements
    @IndexColumn(name="index", base=0)
    @Column(name = "long_value")
    private Long[] longs;

    @Basic
    private int length;

    public void setLongs(Long[] longs) {
        this.longs = longs;
        this.length = longs.length;
    }

    public long getModification(int pos) {
        if (pos >= getLongs().length && pos <= getLength()) {
            return 0;
        }

        Long mod = getModifications()[pos];
        if(mod == null) return 0;
        return mod;
    }

    //private since this is not returning the correct array...
    private Long[] getLongs() {
        return longs;
    }

    public int getLength() {
        return length;
    }

}

Surely, if one is interested to get the whole array instead of individual elements then the same approach can be applied ...

Maybe this helps ...


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.