Hello,
hopefully someone can help me with this sligtly exotic setup. I have two tables here which are associated OneToOne and I want them to be mapped onto a unidirectional association. When using the hibernate schema export it forces me to have the foreign key in the table which belongs to the java Object which holds the association. Is there any way to do it the other way round?
Ok here is the example:
That's how the database should look like:
Code:
+-----------------------------------+
| Config
+-----------------------------------+
| Id | name | ...
+-----------------------------------+
+-----------------------------------+
| ConfigElement
+-----------------------------------+
| Id | content | configId (unique, references config.id)
+-----------------------------------+
and the corresponding java code:
Code:
--------------------------------------------------------
@Entity
@Table(name = "Config")
@SequenceGenerator(
name = "Config_SEQ_ID",
sequenceName = "config_seq", allocationSize = 1)
public class Config {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "Config_SEQ_ID")
long id = -1;
@OneToOne
@JoinColumn(name = "configId")
ConfigElement configElement;
@Column
String name;
...
Code:
@Entity
@Table(name = "ConfigElement")
@SequenceGenerator(
name = "ConfigElement_SEQ_ID",
sequenceName = "configElement_seq", allocationSize = 1)
public class ConfigElement {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ConfigElement_SEQ_ID")
long id = -1;
@Column
String content;
Here is what hibernate generates:
Code:
create table Config (
id int8 not null,
name varchar(255),
configId int8,
primary key (id)
)
create table ConfigElement (
id int8 not null,
content varchar(255),
primary key (id)
)
alter table Config
add constraint FK78A46F62A2070494
foreign key (configId)
references ConfigElement
create sequence configElement_seq
create sequence config_seq
I have to admit that this does not look like a too good dabase design, but I was wondering how things work if I have a Class hierarchy for ConfigElement with a OneTablePerClass strategy. In that case not foreign key is generated at all.
Well anyways: Does anyone know if the above is possible at all?
Thanks in advance for any hint.
Greetings
PeeR