You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by ruslan <ru...@gmail.com> on 2008/11/21 00:31:57 UTC
How to make compound id via @EmbeddedId
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 !!
Re: AW: How to make compound id via @EmbeddedId
Posted by ruslan <ru...@gmail.com>.
That is surprise to me. Thanks a lot for pointing out. I didn't know that
@Embeddable classes have to be registered in persistence.xml because I
thought it's necessary only for entity classes. Perhaps it would be great to
mention it in section "1.4. Embeddable" of the documentation.
Another interesting thing is that I can't register inner class in
persistence.xml like
<class>db.ElectronicBooksDataEntity2.PrimaryKey</class>
Here is example of the class: http://pastebin.ca/1263719
Is it by design or a bug ?
OpenJPA doesn't enhance it. So I had to make PrimaryKey public and move to
upper level. Now it works but only if I use primitives in PrimaryKey. Here
are my classes:
ElectronicBooksDataEntity2: http://pastebin.ca/1263723
PrimaryKey: http://pastebin.ca/1263728
Could you please tell me what I need to modify in PrimaryKey in order to
make PrimaryKey.book an object of ElectronicBooksEntity rather than
numerical int ?
Here is what I tried:
http://pastebin.ca/1263733
I also tried to define @OneToMany relation in ElectronicBooksEntity (even
though I want to have unidirectional relation) as:
@OneToMany(mappedBy="primaryKey.book", cascade = CascadeType.ALL, fetch
= FetchType.EAGER)
private List<ElectronicBooksDataEntity2> datas;
but it didn't help either.
PS. There was another person who asked exactly the same question:
http://n2.nabble.com/How-to-make-compound-id-via-%40EmbeddedId-td1559454.html
but looks like he wasn't able to crack it down either.
Mark Struberg wrote:
>
> All other classes have been enhanced?
>
> Let's start with the most obvious: is the <class> PrimaryKey registered in
> your persistence.xml? @Embeddable also have to be registered there.
>
> LieGrue,
> strub
>
--
View this message in context: http://n2.nabble.com/How-to-make-compound-id-via-%40EmbeddedId-tp1559454p1563278.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
AW: How to make compound id via @EmbeddedId
Posted by Mark Struberg <st...@yahoo.de>.
All other classes have been enhanced?
Let's start with the most obvious: is the <class> PrimaryKey registered in your persistence.xml? @Embeddable also have to be registered there.
LieGrue,
strub
--- ruslan <ru...@gmail.com> schrieb am Fr, 21.11.2008:
> Von: ruslan <ru...@gmail.com>
> Betreff: How to make compound id via @EmbeddedId
> An: users@openjpa.apache.org
> Datum: Freitag, 21. November 2008, 0:31
> 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 !!