java - JPA 2.0: TYPE expression exception -
i have inheritance structure classes, let's parent (as root class) , child subclass.
so jpa 2.0 no can select parent class using
select p parent p type(p) = parent
this should return entries of parent , not entries of child.
but eclipselink 2.1.1 , mysql on glassfish v3, following error:
"invalid type expression on [my.domain.parent]. class not have descriptor, or descriptor not use inheritance or uses classexctractor inheritance".
additionally, define no orm-mapping hand. done automatically on deployment, think.
is there have add parent /child class (an annotation i.e.) declare inheritance structure? (but think shouldn't necessary, because inheritance declared java, it?)
edit:
1 important aspect i've didn't mentioned i'm using inheritance method "table_per_class".
forget said before. work single_table
strategy:
@entity @table(name="person") @inheritance(strategy=inheritancetype.single_table) @discriminatorcolumn(name="gender", discriminatortype=discriminatortype.string, length=6) public abstract class person implements serializable { private static final long serialversionuid = 1l; @id @sequencegenerator(name="person_personid_generator", sequencename="person_id_seq") @generatedvalue(strategy=generationtype.sequence, generator="person_personid_generator") @column(name="person_id", updatable=false, unique=true, nullable=false, precision=22) private long personid; @column(nullable=false, length=32) private string surname; @column(name="given_name", nullable=false, length=32) private string givenname; // ... } @entity @discriminatorvalue("female") public class daughter extends person implements serializable { @column(name="number_of_dolls", precision=22) private int numberofdolls; // ... } @entity @discriminatorvalue("male") public class son extends person implements serializable { @column(name="number_of_toy_cars", precision=22) private integer numberoftoycars; // ... } // junit test method public void testinheritance() { entitymanager em = createnewentitymanagerinstance(); entitytransaction tx = em.gettransaction(); tx.begin(); daughter d = new daughter(); d.setgivenname("sue"); d.setsurname("smith"); d.setnumberofdolls(5); em.persist(d); son s = new son(); s.setgivenname("joe"); s.setsurname("smith"); s.setnumberoftoycars(8); em.persist(s); tx.commit(); query q; list<?> personlist; person p; q = em.createquery("select p person p type(p) = daughter"); personlist = q.getresultlist(); assertequals(1, personlist.size()); p = (person)personlist.get(0); system.out.println( "this daughter is: " + p.getgivenname() + " " + p.getsurname()); q = em.createquery("select p person p type(p) = son"); personlist = q.getresultlist(); assertequals(1, personlist.size()); p = (person)personlist.get(0); system.out.println( "this son is: " + p.getgivenname() + " " + p.getsurname()); q = em.createquery("select p person p"); personlist = q.getresultlist(); assertequals(2, personlist.size()); (object o : personlist) { asserttrue(o instanceof person); p = (person)o; system.out.println( "this person is: " + p.getgivenname() + " " + p.getsurname()); } em.close(); }
the database (i'm using oracle) ddl looks this:
create table "dev"."person" ( "person_id" number not null enable, "given_name" varchar2(32 byte) not null enable, "surname" varchar2(32 byte) not null enable, "gender" varchar2(6 byte) not null enable, "number_of_dolls" number, "number_of_toy_cars" number, constraint "person_pk" primary key ("person_id") );
now said you're trying use table_per_class
strategy. can't there, since the jpa 2.0 spec says vendors not required support it. implementation may not support via jpa interfaces.
Comments
Post a Comment