kundera icon indicating copy to clipboard operation
kundera copied to clipboard

ArrayIndexOutOfBoundsException for Integer id in 2.1

Open linux-china opened this issue 14 years ago • 22 comments

I use Integer as primary key, and it is ok in 2.0.7, and I got following exception in 2.1:

Caused by: java.lang.ArrayIndexOutOfBoundsException: 1 at com.impetus.kundera.property.accessor.IntegerAccessor.fromBytes(IntegerAccessor.java:42) at com.impetus.kundera.property.accessor.IntegerAccessor.fromBytes(IntegerAccessor.java:26) at com.impetus.kundera.property.PropertyAccessorHelper.getObject(PropertyAccessorHelper.java:473) at com.impetus.client.cassandra.CassandraClientBase.computeEntityViaColumns(CassandraClientBase.java:220) at com.impetus.client.cassandra.pelops.PelopsClient.find(PelopsClient.java:604) at com.impetus.client.cassandra.query.CassQuery.populateEntities(CassQuery.java:112) at com.impetus.kundera.query.QueryImpl.getResultList(QueryImpl.java:148) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310) at $Proxy18.getResultList(Unknown Source)

linux-china avatar Sep 25 '12 09:09 linux-china

Could you please share your entity definition and code snippet as well?

-Vivek

mevivs avatar Sep 25 '12 09:09 mevivs

/**

  • language * / @SuppressWarnings("JpaDataSourceORMInspection") @Entity(name = "Language") @Table(name = "languages", schema = "guest@guest_pu") public class Language { /*

    • ID / @Id private Integer id; /*
    • name / @Column(name = "name") private String name; /*
    • custom code for language / @Column(name = "code") private String code; /*
    • ISO 639-1 names */ @Column(name = "iso_names") private String isoNames;

    public Integer getId() { return id; }

    public void setId(Integer id) { this.id = id; }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public String getCode() { return code; }

    public void setCode(String code) { this.code = code; }

    public void setIsoNames(String isoNames) { this.isoNames = isoNames; }

    public String getIsoNames() { return isoNames; } }

linux-china avatar Sep 25 '12 09:09 linux-china

CQL:

create table languages( id ascii PRIMARY KEY, name text, code ascii, iso_names text ) WITH comment='Lanuages';

linux-china avatar Sep 25 '12 09:09 linux-china

Code snippet to perform persist operation?

mevivs avatar Sep 25 '12 09:09 mevivs

Data:

id | code | iso_names | name ----+------+-----------+--------- 1 | en | null | English

code: List languages = entityManager.createQuery("select lang from Language lang").getResultList();

linux-china avatar Sep 25 '12 09:09 linux-china

My cassandra server version is 1.1.3

linux-china avatar Sep 25 '12 09:09 linux-china

verified with cassandra version 1.1.2 and 1.1.3 both This is what i have tried

step 1 :- create column family

create table languages(
id ascii PRIMARY KEY,
name text,
code ascii,
iso_names text
) WITH comment='Lanuages'; 

step 2 :-


    static EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandra_pu");

    static EntityManager entityManager;

    public static void main(String[] args)
    {
        entityManager = emf.createEntityManager();
        Language language = new Language();
        language.setId(1);
        language.setCode("en");
        language.setName("English");

        entityManager.persist(language);
        System.out.println("persisted");
        List<Language> languages = entityManager.createQuery("select lang from Language lang").getResultList();
        System.out.println(languages.get(0).getName());
        System.out.println(languages.get(0).getIsoNames());
        System.out.println(languages.get(0).getId());
        System.out.println(languages.get(0).getCode());
    }

Output :-

cqlsh:guest> select * from languages; id | code | name ------------------+------+--------- \x00\x00\x00\x01 | en | English

working fine for me.

Thanks Kuldeep

kkmishra avatar Sep 25 '12 09:09 kkmishra

I am bit suspicious abbout :


id | code | iso_names | name
----+------+-----------+---------
1 | en | null | English

As Kundera simply ignore and does not persist "null" values into DB. Not sure how you got into DB!

-Vivek

mevivs avatar Sep 25 '12 10:09 mevivs

Tested for key as text and int as primary key data type.

create table languages(
         ...             id int PRIMARY KEY,
         ...             name text,
         ...             code ascii,
         ...             iso_names text
         ...             ) WITH comment='Lanuages';
create table languages(
         ...             id text PRIMARY KEY,
         ...             name text,
         ...             code ascii,
         ...             iso_names text
         ...             ) WITH comment='Lanuages';

tried even with

      language.setIsoNames("null");

working fine.

Thanks Kuldeep

kkmishra avatar Sep 25 '12 10:09 kkmishra

com.impetus.kundera.property.PropertyAccessException: java.lang.NumberFormatException: For input string: " \u0000\u0000\u0000\u0001"
java.lang.NumberFormatException: For input string: "\u0000\u0000\u0000\u0001"
it is problem caused by data?

linux-china avatar Sep 25 '12 10:09 linux-china

entityManager.find(Language.class, 1) is ok, but entityManager.createQuery("select lang from Language lang").getResultList() failed, and I don't know the reason. Maybe I should make a full debug.

linux-china avatar Sep 25 '12 10:09 linux-china

I make a test:

String message="\u0000\u0000\u0000\u0001"; Integer.valueOf(message);

and the stack is : java.lang.NumberFormatException: For input string: "���" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Integer.parseInt(Integer.java:449) at java.lang.Integer.valueOf(Integer.java:554)

linux-china avatar Sep 25 '12 10:09 linux-china

This is what Kuldeep has tried above. FindByQuery.I guess you must be having cassandra-thrift 1.1.2 and Kundera 2.1 jars in your classpath, not anything else. Data should not be any problem if it is mapped correctly in byte[]. As cassandra's interpretation for byte[] varies with data types.

In case you want us to further dig into this, can you please email or share your complete source code on drop box?

-Vivek

mevivs avatar Sep 25 '12 10:09 mevivs

String message="\u0000\u0000\u0000\u0001";
Integer.valueOf(message);

I guess this will always result into NFE.

-Vivek

mevivs avatar Sep 25 '12 10:09 mevivs

Vivek, you are right, and I truncate the table, and insert the meta data again. Now all is right. So I am very sorry to give your trouble, and it caused by illegal data. I am very sorry.

linux-china avatar Sep 25 '12 10:09 linux-china

That's absolutely fine. I am glad it's working for you. This was an issue with 2.0.7, which is working as expected in 2.1 now. Shall i mark it as close ?

mevivs avatar Sep 25 '12 10:09 mevivs

Closing this issue, as this is working fine.

mevivs avatar Sep 26 '12 04:09 mevivs

This issue still occurs in 2.9-snapshot

I didn't not have time to debug it completely...but noticed the issue when I tried reading data I imported into Cassandra with the COPY command.

I fixed it by changing the method below

 @Override
    public final Integer fromBytes(Class targetClass, byte[] b)
    {
        if (b == null)
        {
            return null;
        }

        //return ((b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8) + (b[3] & 0xFF));

        int value = 0;
        for (int i = 0; i < b.length; i++)
            value = (value << 8) + (b[i] & 0xFF);

        return value;
    }

mavencode01 avatar Nov 19 '13 06:11 mavencode01

I suspect this is because of incorrect mapping. Please check if field type defined in entity and on database end are in sync? Means, field is of int data type in Cassandra as well?

mevivs avatar Nov 19 '13 06:11 mevivs

Yeah...they are both in sycn. I did compare the output from cqlsh with my entity...and that solved my problem with the right data type.

describe table table_name

mavencode01 avatar Nov 19 '13 06:11 mavencode01