You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Handerson <ha...@gmail.com> on 2008/12/31 22:30:40 UTC

Generic Id on MappedSupperclass

I have an Identifiable supper class that I want to use as a holder for Id for
my persistent subclasses. The subclasses may use a String or Long as the
primary key.

When OpenJPA tries to create the table it is mapping a Long id in my class
to a Blob field on oracle.
The query generated looks like this, note the Primary Key parenthesis is
empty:

CREATE TABLE Group (id BLOB, createdBy VARCHAR2(255), createdDateTime
TIMESTAMP, updatedBy VARCHAR2(255), updatedDateTime TIMESTAMP, name
VARCHAR2(255), PRIMARY KEY ())



Have anyone successfully tried this kind of mapping on OpenJPA? 

I'm migrating from Hibernate to OpenJPA and this was working using standard
JPA annotations.

I'm using OpenJPA 1.2.0 and Spring 2.5.


This is the OpenJPA Log with TRACE enabled:

2594  persistenceUnit  TRACE  [main] openjpa.MetaData - 	Resolving field
"com.vo.admin.Group@353244430.id".
2594  persistenceUnit  TRACE  [main] openjpa.MetaData - 	"id" has mapping
strategy "org.apache.openjpa.jdbc.meta.strats.MaxEmbeddedBlobFieldStrategy".



The Identifiable mappedSupperclass

@MappedSuperclass
public abstract class Identifiable&lt;T extends Serializable&gt; 
                implements Comparable, Serializable {
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private T id;
        
        public Identifiable() {
		super();
	}

	public Identifiable(T id) {
		super();
		this.id = id;
	}

	public T getId() {
		return id;
	}

	public void setId(T id) {
		this.id = id;
	}
}



The Subclass

@Entity
public abstract class Group extends Identifiable&lt;Long&gt; {
        String name;
        public String getName(){ return name; }
        public void setName(String name) { this.name= name; }
}


Any help is appreciated.
Thanks,
Handerson
-- 
View this message in context: http://n2.nabble.com/Generic-Id-on-MappedSupperclass-tp2097331p2097331.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Generic Id on MappedSupperclass

Posted by Dinkar Rao <di...@gmail.com>.
I tried both PCEnhancer and javaagent, with the same result. The SQL
type of id continues to be BLOB.

Thanks,
Dinkar

Re: Generic Id on MappedSupperclass

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  Good analysis.
  
  Did you investigate when runtime enhancement is in effect, can OpenJPA
determine the runtime type of id field from a definition of Group<Long>?

Pinaki
-- 
View this message in context: http://n2.nabble.com/Generic-Id-on-MappedSupperclass-tp2097331p2115123.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Generic Id on MappedSupperclass

Posted by Dinkar Rao <di...@gmail.com>.
Hi,

I don't have a solution to your problem, but here is the cause of it:

In the parameterized type Identifiable, after type erasure, the
resulting type of id is the upper bound of the type parameter T. In
this case, the upper bound is Serializable. If you look at the
disassembled code for Identifiable, you will see id defined like this:

private Serializable id;

It is this id that is inherited by Group. And OpenJPA maps this to the
SQL type BLOB, which can't be used for primary keys.

The type argument Long, or any of the subclasses of Serializable, that
is passed into the instantiation of Identifiable (in the definition of
Group) has no effect on the compile-time type of id, which will always
be Serializable. All instantiations of the parameterized type
Identifiable have this same type for the id field and all subclasses
of Identifiable will inherit this id field.

You mentioned that this works with Hibernate with the standard JPA
annotations, but there must be other information that is somehow being
passed to Hibernate for this to work as expected.

Thanks,
Dinkar