You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Marc Logemann <li...@logemann.org> on 2009/03/06 13:32:35 UTC

same @Id on mapped superclass

Hi,

following domain model:

@MappedSuperclass
public class BasicMetaData  {

     @Id
     @Column(name = "oid")
     private long oid;
...
}

@Entity
@VersionColumn(name = "jdoversion")
@Table(name = "foo1")
public class MetaData1 extends BasicMetaData {
}

@Entity
@VersionColumn(name = "jdoversion")
@Table(name = "foo2")
public class MetaData2 extends BasicMetaData {
}


Now  i have a oid value of "1" in the DB for MetaData1. When i try to  
use also "1" as oid for MetaData2 and try to persist, i am getting an  
exception because Entity MetaData1 is somehow in the same "ID space".  
So is it correct that one cant define an ID field in a mapped  
Superclass when the IDs have their own "counter".

In this example, OpenJPA even thinks that MetaData2 is a detached  
instance of type MetaData1 because it looks in its cache and sees an  
OID with value 1 but for  complete different entity.

To make it short? I must define the OID field on the subclasses right?


Marc




Re: same @Id on mapped superclass

Posted by Rick Curtis <cu...@gmail.com>.
I'm unable to recreate your failure, can you try a newer view of OpenJPA? ...I tested with 1.2 and 2.0.

-Rick
-- 
View this message in context: http://n2.nabble.com/same-%40Id-on-mapped-superclass-tp2435374p2576289.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: same @Id on mapped superclass

Posted by Kaayan <po...@gmail.com>.
Thanks for your reply.
The DB is MySQL 5.1 win32 and AppServer is "Tomcat integrated OpenEJB 3.1"
(equipped openjpa-1.1.0.jar).
And this is my scenario...
I have an abstract class MasterObjectBase as

@MappedSuperclass
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class MasterObjectBase implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.TABLE)
  private int id;
  ...some other fields...
}

And some other classes like these that extends the super class...

@Entity
@Table(name="TableName1")
public class TableName1 extends MasterObjectBase {
  @Column(name="description",nullable =false,length =255)
  private String description;
  ...some other fields...
}

@Entity
@Table(name="TableName2")
public class TableName2 extends MasterObjectBase {
  @Column(name="description",nullable =false,length =255)
  private String description;
  ...some other fields...
}

Those subclasses are generated by a code generator, so they must be in the
same coding schema, and they at least worked under JBoss + Hibernate.

About the DB data...I have some old data in DB (was running good under
Hibernate), like this:

TableName1:
id    description   ...
-----------------------------
1     Desc 1        ...
2     Desc 2
...

TableName2:
id    description   ...
-----------------------------
1     Desc 3        ...
2     Desc 4

Because my application utilize Remote EJB to communicate, I have to use
detached Entity objects.
OpenJPA did its work while reading from DB. But when I merge a detached
TableName1(with an existing id value, say 1) object via EntityManager, it
throwed...

org.apache.openjpa.persistence.EntityNotFoundException: Attempted to attach
instance "1" of type "class TableName1", but this instance is already in the
datastore as type "class TableName2".

Seems like OpenJPA is treating those subclasses under a same
'MasterObjectBase' space that they should have a unique ID instead giving
each subclass' own space.

Also, I did another experiment. I dropped all the DB tables, leting OpenJPA
to create them again. Inserted some data objects of TableName1 and
TableName2. I discovered TableName1's id was starting from 1 and
TableName2's was 51. Would the persistence layer crash if TableName1's
records exceeds 51?

And the last experiment is, I run this SQL:
UPDATE TableName2 SET id=1 WHERE id=51

Then when I merge() a detached TableName2 object with id=1, same exception
throwed again.

Sorry for the long text, hope you could understand.

Kaayan





2009/4/2 Rick Curtis (via Nabble)
<ml...@n2.nabble.com>
>

> Kaayan -
>
> We're going to need a more detailed description of your scenario to figure
> out whats going on.
>
> -Rick
>
> Kaayan wrote:
> Hello, I have a similar problem here.
>
> My DB is MySQL, and was running JBoss(which uses Hibernate as Persistence
> Provider), and with some data in DB.
>
> Now I have switched to 'tomcat with openejb 3.1 integration', persistence
> provider is openjpa.
>
> Following the instructions, I have marked the superclass with
> @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS).
>
> But when I call merge(), openjpa throwed an exception '
> org.apache.openjpa.persistence.EntityNotFoundException: Attempted to attach
> instance "3" of type "class A", but this instance is already in the
> datastore as type "class B".'
>
> Any work-arounds on this?
>
> Many thanks!
>
>
>
> ------------------------------
>  This email is a reply to your post @
> http://n2.nabble.com/same-%40Id-on-mapped-superclass-tp2435374p2574796.html
> You can reply by email or by visting the link above.
>
>

-- 
View this message in context: http://n2.nabble.com/same-%40Id-on-mapped-superclass-tp2435374p2575770.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: same @Id on mapped superclass

Posted by Rick Curtis <cu...@gmail.com>.
Kaayan -

We're going to need a more detailed description of your scenario to figure out whats going on.

-Rick

Hello, I have a similar problem here.

My DB is MySQL, and was running JBoss(which uses Hibernate as Persistence Provider), and with some data in DB.

Now I have switched to 'tomcat with openejb 3.1 integration', persistence provider is openjpa.

Following the instructions, I have marked the superclass with @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS).

But when I call merge(), openjpa throwed an exception ' org.apache.openjpa.persistence.EntityNotFoundException: Attempted to attach instance "3" of type "class A", but this instance is already in the datastore as type "class B".'

Any work-arounds on this? 

Many thanks!


-- 
View this message in context: http://n2.nabble.com/same-%40Id-on-mapped-superclass-tp2435374p2574796.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: same @Id on mapped superclass

Posted by Kaayan <po...@gmail.com>.
Hello, I have a similar problem here.

My DB is MySQL, and was running JBoss(which uses Hibernate as Persistence Provider), and with some data in DB.

Now I have switched to 'tomcat with openejb 3.1 integration', persistence provider is openjpa.

Following the instructions, I have marked the superclass with @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS).

But when I call merge(), openjpa throwed an exception ' org.apache.openjpa.persistence.EntityNotFoundException: Attempted to attach instance "3" of type "class A", but this instance is already in the datastore as type "class B".'

Any work-arounds on this? 

Many thanks!
-- 
View this message in context: http://n2.nabble.com/same-%40Id-on-mapped-superclass-tp2435374p2574513.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: same @Id on mapped superclass

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  You should define a Inheritance strategy. If you are using SINGLE_TABLE
strategy then you should also define a Discrimnator value for each subclass.
 

Marc Logemann wrote:
> 
> Hi,
> 
> following domain model:
> 
> @MappedSuperclass
> public class BasicMetaData  {
> 
>      @Id
>      @Column(name = "oid")
>      private long oid;
> ...
> }
> 
> @Entity
> @VersionColumn(name = "jdoversion")
> @Table(name = "foo1")
> public class MetaData1 extends BasicMetaData {
> }
> 
> @Entity
> @VersionColumn(name = "jdoversion")
> @Table(name = "foo2")
> public class MetaData2 extends BasicMetaData {
> }
> 
> 
> Now  i have a oid value of "1" in the DB for MetaData1. When i try to  
> use also "1" as oid for MetaData2 and try to persist, i am getting an  
> exception because Entity MetaData1 is somehow in the same "ID space".  
> So is it correct that one cant define an ID field in a mapped  
> Superclass when the IDs have their own "counter".
> 
> In this example, OpenJPA even thinks that MetaData2 is a detached  
> instance of type MetaData1 because it looks in its cache and sees an  
> OID with value 1 but for  complete different entity.
> 
> To make it short? I must define the OID field on the subclasses right?
> 
> 
> Marc
> 
> 
> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/same-%40Id-on-mapped-superclass-tp2435374p2445639.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.