Home » Support » Forum » Query on Embedded Primary Key?

Query on Embedded Primary Key?



I am trying to use Embedded Primary Key - but seems I can't retrieve them correctly with the query below - the query seem to work fine for other field (in the code = primkeycopy). The error I get when is below - it complains about a casting issue.

Let me know how to fix it - (full reproducible code here - just run it)




package DBbug;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;

public class OBJ2 implements Serializable {
    @EmbeddedId private Primkey2 primkey;
    private Primkey2 primkeycopy;

    public OBJ2() {
    public OBJ2(long field1, long field2) {
        primkey = new Primkey2(field1, field2);
        primkeycopy = primkey;
    public static void main(String[] args){
        EntityManager em = Persistence.createEntityManagerFactory("$objectdb/db/TEST.odb").createEntityManager();
        em.persist( new OBJ2(10, 11)); 
        TypedQuery<Primkey2> query = em.createQuery("SELECT mif.primkeycopy FROM OBJ2 mif", Primkey2.class);
        List<Primkey2> ret = query.getResultList();
        String q = ret.get(0).getClass().getCanonicalName();
        TypedQuery<Primkey2> query2 = em.createQuery("SELECT mif.primkey FROM OBJ2 mif", Primkey2.class);
        List<Primkey2> ret2 = query2.getResultList();
        String q2 = ret2.get(0).toString();
public static class Primkey2 implements Serializable {
    public long field1;
    public long field2;

    public Primkey2() {

    public Primkey2(long field1, long field2) {
        this.field1 = field1;
        this.field2 = field2;

    public boolean equals(Object obj) {
        Primkey2 x = (Primkey2)obj;
        return field1 == x.field1 && field2 == x.field2;

    public int hashCode() {
        int hash = 5;
        hash = 79 * hash + (int) (this.field1 ^ (this.field1 >>> 32));
        hash = 79 * hash + (int) (this.field2 ^ (this.field2 >>> 32));
        return hash;


Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to DBbug.OBJ2$Primkey2
at DBbug.OBJ2.main(OBJ2.java:37)
Java Result: 1





Thanks for the clear test case that demonstrates the issue.

Please try version 2.5.6 that should fix it.

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

Perfect - it is fixed - Thanks a lot - was very quick - I am impressed.

Post Reply

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