Problem with byte arrays in JDO - ClassCastException

#1

A bizarre ClassCastException is thrown if I attempt to obtain a byte array field as the result of a Query:

Exception in thread "main" java.lang.ClassCastException: java.lang.Boolean cannot be cast to [B
at spiffy.test.ObjectdbTest.main(ObjectdbTest.java:44)

package spiffy.test;

import java.util.List;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import spiffy.test.model.Example;

/**
*
* @author steve
*/
public class ObjectdbTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("test.odb");
        PersistenceManager pm = pmf.getPersistenceManager();
        // Clear out existing objects
        pm.currentTransaction().begin();
        Query query = pm.newQuery(Example.class);
        query.deletePersistentAll();
        pm.currentTransaction().commit();
        pm.currentTransaction().begin();
        Example example1 = new Example();
        example1.setId(1);
        example1.setName("name1");
        example1.setData(new byte[1]);
        example1.getData()[0] = 123;
        Example example2 = new Example();
        example2.setId(2);
        example2.setName("name2");
        example2.setData(new byte[2]);
        example2.getData()[0] = 12;
        pm.makePersistent(example1);
        pm.makePersistent(example2);
        pm.currentTransaction().commit();
        query = pm.newQuery(Example.class, "name == :name");
        query.setResult("data");
        List<byte[]> dataList = (List<byte[]>) query.execute("name1");
        for (byte[] data : dataList) {
            System.out.print(data);
        }
    }
}


package spiffy.test.model;

/**
*
* @author steve
*/
public class Example {

    private long id;
    private String name;
    private byte[] data;

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the data
     */
    public byte[] getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(byte[] data) {
        this.data = data;
    }

}

<?xml version="1.0"?>
<jdo xmlns="http://xmlns.jcp.org/xml/ns/jdo/jdo"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/jdo/jdo
        http://xmlns.jcp.org/xml/ns/jdo/jdo_2_0.xsd" version="2.0">
    <package name="spiffy.test.model">
        <class name="Example">
            <field name="id" primary-key="true"/>
            <field name="name" persistence-modifier="persistent"/>
            <field name="data" persistence-modifier="persistent"/>
        </class>
   </package>
</jdo>
#2

Thank you for this report. Please try build 2.7.1_08.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

build 2.7.1_08 does not fix the problem.

#4

You are right, sorry. It fixed a simplified test case but not your exact test case.

Build 2.7.1_09 should fix your test case as well.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#5

That did it!  Thanks for your fast response.


Post Reply

To post a reply and/or subscribe to update notifications - please login