459 words

Problem with byte arrays in JDO - ClassCastException

#1
2017-09-13 03:33

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>
sjzlondon
sjzlondon's picture
Joined on 2013-04-21
User Post #39
#2
2017-09-14 16:43

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

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
support
support's picture
Joined on 2010-05-03
User Post #2,814
#3
2017-09-15 00:11

build 2.7.1_08 does not fix the problem.

sjzlondon
sjzlondon's picture
Joined on 2013-04-21
User Post #40
#4
2017-09-15 00:50

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)
support
support's picture
Joined on 2010-05-03
User Post #2,815
#5
2017-09-15 00:55

That did it!  Thanks for your fast response.

sjzlondon
sjzlondon's picture
Joined on 2013-04-21
User Post #41

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel