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 :)