-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: use TABLE_PER_CLASS mit same data-id
PostPosted: Tue May 16, 2006 7:05 pm 
Newbie

Joined: Mon Mar 13, 2006 6:15 pm
Posts: 2
Hibernate 3.20.cr1:

i have 2 classes extends from an root abstract class,i use
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
for the root class.
so the class hierarchy seems like that:
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
Order.....

OrderA extends Order...
OrderB extends Order...

so,now i have 2 table,orderA and orderB
now i create two Objects from OrderA and OrderB and assign them the same id. so when i persist OrderA and OrderB ,i have got an error:

Caused by: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [events.OrderB#1]

how can i assign the two Object same id?


Top
 Profile  
 
 Post subject: Re: use TABLE_PER_CLASS mit same data-id
PostPosted: Mon Sep 21, 2009 1:26 am 
Newbie

Joined: Mon Sep 21, 2009 1:13 am
Posts: 1
same problem


Top
 Profile  
 
 Post subject: Re: use TABLE_PER_CLASS mit same data-id
PostPosted: Tue Sep 22, 2009 6:26 am 
Beginner
Beginner

Joined: Wed Jul 09, 2008 5:34 am
Posts: 41
Location: Brno, Czech Republic
In Hibernate, the mapping strategy you used is known as "Table per concrete class with unions". What you probably want to do is "Table per concrete class with implicit polymorphism". Refer to "Java Persistence with Hibernate" for further information, but basically, you have to remove the annotations @Inheritance and add this annotation to your class: @MappedSuperclass . The subclasses won't have any additional mapping, other than @Entity.

Note that "@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)" in Hibernate shares the ID between the implementations of the parent class (for the union): "The database identifier and its mapping have to be present in the superclass, to be shared across all subclasses and their tables. "

It would be something like this (note that I tested with MySQL 5.1 and Hibernate Core 3.2.6.ga with Hibernate Annotations 3.3.1.GA):

Order:
Code:
@Entity
@MappedSuperclass
public abstract class Order {
   @Id
   private Long id;

   @Column
   private String name;
...
}


OrderA:
Code:
@Entity
public class OrderA extends Order {
   @Column
   private String nameOrderA;
...
}


OrderB:
Code:
@Entity
public class OrderB extends Order {
   @Column
   private String nameOrderB;
...
}


Service:
Code:
      Session s = getSession();
      Transaction tx = s.beginTransaction();
      
      OrderA orderA = new OrderA();
      orderA.setId(1L);
      orderA.setName("Name of the order A");
      orderA.setNameOrderA("Name of the order A");
      
      OrderB orderB = new OrderB();
      orderB.setId(1L);
      orderB.setName("Name of the order B");
      orderB.setNameOrderB("Name of the order B");
      
      s.saveOrUpdate(orderA);
      s.saveOrUpdate(orderB);
      tx.commit();
      
      s.close();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.