ArrayIndexOutOfBoundsException for Integer id in 2.1
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)
Could you please share your entity definition and code snippet as well?
-Vivek
/**
-
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; } }
CQL:
create table languages( id ascii PRIMARY KEY, name text, code ascii, iso_names text ) WITH comment='Lanuages';
Code snippet to perform persist operation?
Data:
id | code | iso_names | name ----+------+-----------+--------- 1 | en | null | English
code:
List
My cassandra server version is 1.1.3
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
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
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
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?
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.
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)
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
String message="\u0000\u0000\u0000\u0001";
Integer.valueOf(message);
I guess this will always result into NFE.
-Vivek
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.
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 ?
Closing this issue, as this is working fine.
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;
}
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?
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