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.  [ 1 post ] 
Author Message
 Post subject: OneToOne, Using same PrimaryKey Problem
PostPosted: Wed Mar 25, 2009 11:01 am 
Newbie

Joined: Sat Jan 03, 2009 8:03 pm
Posts: 3
Hey there :),


I've got following simplified Objects


~-------------------~
|.......Status.........|
|---------------------|
|Id id....................|
|ExtendedInfo exI| --------> ~------------------~
|...........................|.............| ExtendedInfo.....|
-----------------------...............|---------------------|
............................................| Id id..................|
...........................................----------------------

And two subclasses of ExtendedInfo:

~-------------------~ ~-------------------~
|..........Y.............| ..|..........X............ |
|--------------------| ..|------------------- |
|String y.............| ..| int x.................|
---------------------- ..---------------------


My thoughts were, if i need some extendedInfo which i haven't thought about during creation, let the new Class (e.g. Z) extend ExtendedInfo and I can put it into to the ExtendedInfo "Slot" in my Statusobject

I mapped it succesfully, but i Hibernate created 4 tables:

1. Table status, columns: id
2.Table ExtendedInfo, columns: id, status id //<--- this looks like 1;1 , 2;2 , 3;3 aso.
3. Table Y, with id and String
and 4.Table X with id and x

But i think its possible to use the PK from status within the Table X and Y without using the extendedInfo table.

e.g. Status Table with a status with the id 1,
the attached Extendedinfo Object, (e.g. a X) holds the same Primary key, so they are mapped together.


My code looks as follows:
Status:
Code:
@Entity
@Table(name="Status_test")
public class Status {

   long id;
        ExtendedInfo extended;
        [...]
     
      //id generation
       @Id
       @TableGenerator(name = "puma_gen", table="primary_keys",allocationSize=1)
  @GeneratedValue(strategy = GenerationType.TABLE, generator = "puma_gen")
   public long getId() {
      return id;
   }

        [...]

        //the extendedInfo Part
   @OneToOne(cascade = CascadeType.ALL )
   @PrimaryKeyJoinColumn
   public ExtendedInfo getExtension() {
      return extension;
   }


Class ExtendedInfo:

Code:
@Entity
@Table(name="ExtendedInfo")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ExtendedInfo {
   
   
   long id;

   @Id
   public long getId() {
      return id;
   }
[...]



And X, the subclass of ExtendedInfo :
Code:
@Entity
@Table(name="X")
public class MailAttachement extends ExtendedInfo{

int x;

//no id here :)

[...]
}



The ExtendedInfo Table is not created anymore, but the id from the Status is not written into X or Y , the id from them is always 0.

Now the Code for the Data changes:

Code:

//just a simple handler
StatusHandler sHandler = new StatusHandler(config, log);
sHandler.init("Test_x");
      
Status s = new Status();
      
      
sHandler.persist(s);
      
ExtendedInfo x = new X(1234);
      
s.setExtension(x);
      
sHandler.merge(s);


After this there is a new Status in the table, and a new Y-Object in the Y-Table, but the id of the Y is always 0 :(

Its exactly like the Body/Heart example from hibernate, but it just won't work :(,

I hope i can spend some credits in here ;),

thanks for reading anyways :)


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

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.