openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ruslan <rusl...@gmail.com>
Subject How to make compound id via @EmbeddedId
Date Thu, 20 Nov 2008 23:31:57 GMT
Hi guys. Could you please tell me how to create compound id field using
embedded class ? I tried this (which works in Hibernate):

package db;

import javax.persistence.*;
import java.io.Serializable;

@SuppressWarnings({"ALL"})
@Entity
@Table(name = "electronic_books_data")
public class ElectronicBooksDataEntity2
{
    @EmbeddedId
    private PrimaryKey primaryKey = new PrimaryKey();

    @Basic
    @Column(name = "data")
    private
    String data;

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;

        ElectronicBooksDataEntity2 that = (ElectronicBooksDataEntity2) obj;

        if (!primaryKey.equals(that.primaryKey))
            return false;

        if ( (getData() == null) || (that.getData() == null) ||
!getData().equals(that.getData()) )
            return false;

        return true;
    }

    @Override
    public int hashCode()
    {
        int result = primaryKey != null ? primaryKey.hashCode() : 0;
        return 31*result + ((getData() != null)? getData().hashCode() : 0);
    }

    public String getData() { return data; }
    public void setData(String data) { this.data = data; }

    @Embeddable
    public static class PrimaryKey implements Serializable
    {
        @Basic
        @Column(name = "book_id")
        private int book_id;

        @Basic
        @Column(name = "format")
        private int format;

        @Override
        public boolean equals(Object obj)
        {
            if (this == obj) return true;
            if (obj == null || getClass() != obj.getClass()) return false;

            PrimaryKey that = (PrimaryKey) obj;
            return !((book_id != that.book_id)) && format == that.format;
        }

        @Override
        public int hashCode()
        {
            return (31 * format) + book_id;
        }
    }
}


But it throws exception saying:

47  mysql  INFO   [main] openjpa.Runtime - Starting OpenJPA 1.3.0-SNAPSHOT
109  mysql  INFO   [main] openjpa.jdbc.JDBC - Using dictionary class
"org.apache.openjpa.jdbc.sql.MySQLDictionary".
Exception in thread "main" <openjpa-1.3.0-SNAPSHOT-r422266:707655 fatal user
error> org.apache.openjpa.persistence.ArgumentException: The type "class
db.ElectronicBooksDataEntity2$PrimaryKey" has not been enhanced.
    at
org.apache.openjpa.meta.ClassMetaData.resolveMeta(ClassMetaData.java:1652)
    at
org.apache.openjpa.meta.ClassMetaData.resolve(ClassMetaData.java:1626)

PS. I was able to do compound Id class using @IdClass, but I'm wondering if
it's possible to do with @EmbeddedId.


THANKS !!

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message