You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Jasmin Riemer <Ja...@gmx.de> on 2009/05/15 14:08:33 UTC
Inheritance: Subclasses are not recognised
Hello everybody,
currently, I am working with OpenJPA in the Spring framework and try to implement the following:
I inherit certain subclasses from a mainclass. All these classes should be mapped to a single table, so I use InheritanceType.SINGLE_TABLE and add a discriminator value to each subclass.
Simplified, it looks like this:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="doc_type", discriminatorType=DiscriminatorType.STRING)
public abstract class Document {
// ...
}
@Entity
@DiscriminatorValue(value="Magazine")
public class Magazine extends Document {
// ...
}
@Entity
@DiscriminatorValue(value="Book")
public class Book extends Document {
// ...
}
Unfortunately, I get the following error message when I try to load data from the database:
Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageE xception: Could not map discriminator value "Book" to any known subclasses of the requested class "project.entities.Document" (known discriminator values: [Document, Magazine]).; nested exception is <openjpa-1.2.1-r752877:753278 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Could not map discriminator value "Book" to any known subclasses of the requested class "project.entities.Document" (known discriminator values: [Document, Magazine]).
It seems that there are only known the main class and the subclass "Magazine", but there is a problem with "Book" (and some other classes inherited from "Document").
I do not have much experience with OpenJPA and Spring and thus I have no clue how to solve this problem. Anybody out there who has an idea or maybe had a similar problem?
Is there maybe something special I could have used accidentally for "Magazine", but not for the other classes?
--
Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate + Telefonanschluss für nur 17,95 Euro/mtl.!* http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a
Re: Inheritance: Subclasses are not recognised
Posted by Jasmin Riemer <Na...@gmx.de>.
Hello everybody,
after spending half of my weekend, I got it to work. I do not know exactly WHY it works that way, but I think this is marginal ;)
If someone can explain it to me, feel free to do so, I would be glad.
It seems that the cause was a combination of bad Spring configuration and some anomalies in my project. So lets have a look on how I solved the problem:
First, I wanted to try to persist one of the subclasses, because I wanted to see what discriminator value would be written to the database.
Because I only need to read that subclass from other objects and do not need to call them directly from database, I created a new Spring DAO for this persist task. I found out, that persisting did not work either and thus realised that I did not include a transaction manager (it was configured, but not linked - too bad :\ ).
After reconfiguring the Spring framework I was able to persist the entity without any problems. After that, I ran my application again and - it suddenly recognised the entity persisted before (but still not the other subclasses).
After some try-and-error, I found out:
To make Spring / OpenJPA recognise the discriminator values, the subclasses have to be
a) directly related to any entity, that can be called with a Spring DAO
or
b) directly called with a Spring DAO
If only the superclass is related / called, the discriminator value of the subclasses will not be recognised.
Maybe some OpenJPA / Spring experts will laugh at me, but please explain me, why it works that way ^^"
-------- Original-Nachricht --------
> Datum: Fri, 15 May 2009 16:47:31 +0200
> Von: "Jasmin Riemer" <Na...@gmx.de>
> An: users@openjpa.apache.org
> Betreff: Re: Inheritance: Subclasses are not recognised
> Hi Mike,
>
> yes, I have.
> This was also my first thought about that problem ;)
> Maybe other configurations in persistence.xml that may cause problems?
>
> <properties>
> <property name="openjpa.ConnectionUserName" value="root"/>
> <property name="openjpa.ConnectionPassword" value=""/>
> <property name="openjpa.ConnectionURL"
> value="jdbc:mysql://localhost:3306/ghostbase"/>
> <property name="openjpa.ConnectionDriverName"
> value="com.mysql.jdbc.Driver"/>
> <property name="openjpa.jdbc.DBDictionary"
> value="mysql(TableType=myisam)"/>
> <property name="openjpa.MaxFetchDepth" value="2"/>
> </properties>
>
> -Jasmin
>
> -------- Original-Nachricht --------
> > Datum: Fri, 15 May 2009 09:25:54 -0500
> > Von: Michael Dick <mi...@gmail.com>
> > An: users@openjpa.apache.org
> > Betreff: Re: Inheritance: Subclasses are not recognised
>
> > Hi Jasmin,
> >
> > Do you have all your entities listed in persistence.xml?
> >
> > -mike
> >
> > On Fri, May 15, 2009 at 7:08 AM, Jasmin Riemer <Ja...@gmx.de>
> > wrote:
> >
> > > Hello everybody,
> > >
> > > currently, I am working with OpenJPA in the Spring framework and try
> to
> > > implement the following:
> > >
> > > I inherit certain subclasses from a mainclass. All these classes
> should
> > be
> > > mapped to a single table, so I use InheritanceType.SINGLE_TABLE and
> add
> > a
> > > discriminator value to each subclass.
> > >
> > > Simplified, it looks like this:
> > >
> > > @Entity
> > > @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
> > > @DiscriminatorColumn(name="doc_type",
> > > discriminatorType=DiscriminatorType.STRING)
> > > public abstract class Document {
> > >
> > > // ...
> > >
> > > }
> > >
> > > @Entity
> > > @DiscriminatorValue(value="Magazine")
> > > public class Magazine extends Document {
> > >
> > > // ...
> > >
> > > }
> > >
> > > @Entity
> > > @DiscriminatorValue(value="Book")
> > > public class Book extends Document {
> > >
> > > // ...
> > >
> > > }
> > >
> > > Unfortunately, I get the following error message when I try to load
> data
> > > from the database:
> > >
> > > Exception in thread "main"
> > > org.springframework.dao.InvalidDataAccessApiUsageE xception: Could not
> > map
> > > discriminator value "Book" to any known subclasses of the requested
> > class
> > > "project.entities.Document" (known discriminator values: [Document,
> > > Magazine]).; nested exception is <openjpa-1.2.1-r752877:753278
> nonfatal
> > user
> > > error> org.apache.openjpa.persistence.ArgumentException: Could not map
> > > discriminator value "Book" to any known subclasses of the requested
> > class
> > > "project.entities.Document" (known discriminator values: [Document,
> > > Magazine]).
> > >
> > > It seems that there are only known the main class and the subclass
> > > "Magazine", but there is a problem with "Book" (and some other classes
> > > inherited from "Document").
> > >
> > > I do not have much experience with OpenJPA and Spring and thus I have
> no
> > > clue how to solve this problem. Anybody out there who has an idea or
> > maybe
> > > had a similar problem?
> > > Is there maybe something special I could have used accidentally for
> > > "Magazine", but not for the other classes?
> > > --
> > > Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate +
> > > Telefonanschluss für nur 17,95 Euro/mtl.!*
> > > http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a
> > >
>
> --
> Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate +
> Telefonanschluss für nur 17,95 Euro/mtl.!*
> http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a
--
Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate + Telefonanschluss für nur 17,95 Euro/mtl.!* http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a
Re: Inheritance: Subclasses are not recognised
Posted by Jasmin Riemer <Na...@gmx.de>.
Hi Mike,
yes, I have.
This was also my first thought about that problem ;)
Maybe other configurations in persistence.xml that may cause problems?
<properties>
<property name="openjpa.ConnectionUserName" value="root"/>
<property name="openjpa.ConnectionPassword" value=""/>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/ghostbase"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.jdbc.DBDictionary" value="mysql(TableType=myisam)"/>
<property name="openjpa.MaxFetchDepth" value="2"/>
</properties>
-Jasmin
-------- Original-Nachricht --------
> Datum: Fri, 15 May 2009 09:25:54 -0500
> Von: Michael Dick <mi...@gmail.com>
> An: users@openjpa.apache.org
> Betreff: Re: Inheritance: Subclasses are not recognised
> Hi Jasmin,
>
> Do you have all your entities listed in persistence.xml?
>
> -mike
>
> On Fri, May 15, 2009 at 7:08 AM, Jasmin Riemer <Ja...@gmx.de>
> wrote:
>
> > Hello everybody,
> >
> > currently, I am working with OpenJPA in the Spring framework and try to
> > implement the following:
> >
> > I inherit certain subclasses from a mainclass. All these classes should
> be
> > mapped to a single table, so I use InheritanceType.SINGLE_TABLE and add
> a
> > discriminator value to each subclass.
> >
> > Simplified, it looks like this:
> >
> > @Entity
> > @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
> > @DiscriminatorColumn(name="doc_type",
> > discriminatorType=DiscriminatorType.STRING)
> > public abstract class Document {
> >
> > // ...
> >
> > }
> >
> > @Entity
> > @DiscriminatorValue(value="Magazine")
> > public class Magazine extends Document {
> >
> > // ...
> >
> > }
> >
> > @Entity
> > @DiscriminatorValue(value="Book")
> > public class Book extends Document {
> >
> > // ...
> >
> > }
> >
> > Unfortunately, I get the following error message when I try to load data
> > from the database:
> >
> > Exception in thread "main"
> > org.springframework.dao.InvalidDataAccessApiUsageE xception: Could not
> map
> > discriminator value "Book" to any known subclasses of the requested
> class
> > "project.entities.Document" (known discriminator values: [Document,
> > Magazine]).; nested exception is <openjpa-1.2.1-r752877:753278 nonfatal
> user
> > error> org.apache.openjpa.persistence.ArgumentException: Could not map
> > discriminator value "Book" to any known subclasses of the requested
> class
> > "project.entities.Document" (known discriminator values: [Document,
> > Magazine]).
> >
> > It seems that there are only known the main class and the subclass
> > "Magazine", but there is a problem with "Book" (and some other classes
> > inherited from "Document").
> >
> > I do not have much experience with OpenJPA and Spring and thus I have no
> > clue how to solve this problem. Anybody out there who has an idea or
> maybe
> > had a similar problem?
> > Is there maybe something special I could have used accidentally for
> > "Magazine", but not for the other classes?
> > --
> > Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate +
> > Telefonanschluss für nur 17,95 Euro/mtl.!*
> > http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a
> >
--
Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate + Telefonanschluss für nur 17,95 Euro/mtl.!* http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a
Re: Inheritance: Subclasses are not recognised
Posted by Michael Dick <mi...@gmail.com>.
Hi Jasmin,
Do you have all your entities listed in persistence.xml?
-mike
On Fri, May 15, 2009 at 7:08 AM, Jasmin Riemer <Ja...@gmx.de> wrote:
> Hello everybody,
>
> currently, I am working with OpenJPA in the Spring framework and try to
> implement the following:
>
> I inherit certain subclasses from a mainclass. All these classes should be
> mapped to a single table, so I use InheritanceType.SINGLE_TABLE and add a
> discriminator value to each subclass.
>
> Simplified, it looks like this:
>
> @Entity
> @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
> @DiscriminatorColumn(name="doc_type",
> discriminatorType=DiscriminatorType.STRING)
> public abstract class Document {
>
> // ...
>
> }
>
> @Entity
> @DiscriminatorValue(value="Magazine")
> public class Magazine extends Document {
>
> // ...
>
> }
>
> @Entity
> @DiscriminatorValue(value="Book")
> public class Book extends Document {
>
> // ...
>
> }
>
> Unfortunately, I get the following error message when I try to load data
> from the database:
>
> Exception in thread "main"
> org.springframework.dao.InvalidDataAccessApiUsageE xception: Could not map
> discriminator value "Book" to any known subclasses of the requested class
> "project.entities.Document" (known discriminator values: [Document,
> Magazine]).; nested exception is <openjpa-1.2.1-r752877:753278 nonfatal user
> error> org.apache.openjpa.persistence.ArgumentException: Could not map
> discriminator value "Book" to any known subclasses of the requested class
> "project.entities.Document" (known discriminator values: [Document,
> Magazine]).
>
> It seems that there are only known the main class and the subclass
> "Magazine", but there is a problem with "Book" (and some other classes
> inherited from "Document").
>
> I do not have much experience with OpenJPA and Spring and thus I have no
> clue how to solve this problem. Anybody out there who has an idea or maybe
> had a similar problem?
> Is there maybe something special I could have used accidentally for
> "Magazine", but not for the other classes?
> --
> Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate +
> Telefonanschluss für nur 17,95 Euro/mtl.!*
> http://dslspecial.gmx.de/freedsl-surfflat/?ac=OM.AD.PD003K11308T4569a
>