P
Peter Horlock
Hi,
I am kinda helpless, trying to get a Hibernate Mapping to work! :-(
I've got a table which has a composite Primary Key (2 values).
One of those 2 values is also a foreign key to another table
Then I got another table, where the composite primary key is a foreign
key.
How can this be done in Hibernate, using Annotations?
Do I always have to design the foreign key constraints, even if I
don't really need them?
(Well, I guess it wouldn't hurt to have all data, but I guess I could
do without, too).
Currently, as I couldn't get it to work, I tried to easiest set up
without mapping
the FK constraints.
@Entity
@IdClass(value = MyPrimaryKey.class)
@Table(name = "MYSCEMA.MYTABLE")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class MyTable implements Serializable
{
@Id
private MyPrimaryKey id;
@Column(name = "COLUMN3")
private String column3;
@Column(name = "COLUMN4")
private String column4;
@Column(name = "COLUMN5")
private String column5;
[..]
}
@Embeddable
public class MyPrimaryKey implements Serializable
{
@Column(name = "COMPOSITE_ID1_WHICH_IS_ALSO_A_FK")
private Long compositeId1;
@Column(name = "COMPOSITE_ID2")
private Boolean compositeId2;
[..]
}
@Entity
@Table(name = "MYSCEMA.MYTABLE2")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class MyTable2 implements java.io.Serializable
{
@Id
@Column(name = "ID")
private String id;
@Column(name = "COLUMN1")
private String column1;
@Column(name = "COMPOSITE_ID1_WHICH_IS_ALSO_A_FK")
private Integer compositeId1;
@Column(name = "COMPOSITE_ID2")
private Boolean compositeId2;
@Column(name = "TIME")
@Temporal(TemporalType.TIMESTAMP)
private Calendar time;
[..]
}
My Create Statmeents (Oracle) look like this:
CREATE TABLE MYSCHEMA.MYTABLE (
COMPOSITE_ID1_WHICH_IS_ALSO_A_FK INTEGER NOT NULL,
COMPOSITE_ID2 NUMBER(1,0) DEFAULT 0 NOT NULL,
COLUMN3 VARCHAR(200) NOT NULL,
COLUMN4 VARCHAR(20) NOT NULL,
COLUMN5 VARCHAR(20) NOT NULL,
CONSTRAINT CONS1 PRIMARY KEY (COMPOSITE_ID1_WHICH_IS_ALSO_A_FK,
COMPOSITE_ID2),
CONSTRAINT CONS2 FOREIGN KEY (COMPOSITE_ID1_WHICH_IS_ALSO_A_FK)
REFERENCES MYSCHEMA.MYTABLE3(ID),
CONSTRAINT CONS3 UNIQUE (COLUMN3),
CONSTRAINT CONS4 CHECK(COMPOSITE_ID2 IN(1,0) )
) ;
CREATE SEQUENCE MYSCHEMA.MY_SEQ
START WITH 1
INCREMENT BY 1
NOMAXVALUE;
CREATE TABLE MYSCHEMA.MYTABLE2 (
ID NUMBER NOT NULL,
COLUMN1 VARCHAR(6) NOT NULL,
COMPOSITE_ID1_WHICH_IS_ALSO_A_FK NUMBER NOT NULL,
COMPOSITE_ID2 NUMBER(1,0) NOT NULL,
TIME TIMESTAMP(0) NULL,
CONSTRAINT CONS5 PRIMARY KEY (ID),
CONSTRAINT CONS6 FOREIGN KEY (COMPOSITE_ID1_WHICH_IS_ALSO_A_FK,
COMPOSITE_ID2) REFERENCES MYSCHEMA.MYTABLE
(COMPOSITE_ID1_WHICH_IS_ALSO_A_FK, COMPOSITE_ID2)
) ;
Thanks in advance,
Peter
I am kinda helpless, trying to get a Hibernate Mapping to work! :-(
I've got a table which has a composite Primary Key (2 values).
One of those 2 values is also a foreign key to another table
Then I got another table, where the composite primary key is a foreign
key.
How can this be done in Hibernate, using Annotations?
Do I always have to design the foreign key constraints, even if I
don't really need them?
(Well, I guess it wouldn't hurt to have all data, but I guess I could
do without, too).
Currently, as I couldn't get it to work, I tried to easiest set up
without mapping
the FK constraints.
@Entity
@IdClass(value = MyPrimaryKey.class)
@Table(name = "MYSCEMA.MYTABLE")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class MyTable implements Serializable
{
@Id
private MyPrimaryKey id;
@Column(name = "COLUMN3")
private String column3;
@Column(name = "COLUMN4")
private String column4;
@Column(name = "COLUMN5")
private String column5;
[..]
}
@Embeddable
public class MyPrimaryKey implements Serializable
{
@Column(name = "COMPOSITE_ID1_WHICH_IS_ALSO_A_FK")
private Long compositeId1;
@Column(name = "COMPOSITE_ID2")
private Boolean compositeId2;
[..]
}
@Entity
@Table(name = "MYSCEMA.MYTABLE2")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class MyTable2 implements java.io.Serializable
{
@Id
@Column(name = "ID")
private String id;
@Column(name = "COLUMN1")
private String column1;
@Column(name = "COMPOSITE_ID1_WHICH_IS_ALSO_A_FK")
private Integer compositeId1;
@Column(name = "COMPOSITE_ID2")
private Boolean compositeId2;
@Column(name = "TIME")
@Temporal(TemporalType.TIMESTAMP)
private Calendar time;
[..]
}
My Create Statmeents (Oracle) look like this:
CREATE TABLE MYSCHEMA.MYTABLE (
COMPOSITE_ID1_WHICH_IS_ALSO_A_FK INTEGER NOT NULL,
COMPOSITE_ID2 NUMBER(1,0) DEFAULT 0 NOT NULL,
COLUMN3 VARCHAR(200) NOT NULL,
COLUMN4 VARCHAR(20) NOT NULL,
COLUMN5 VARCHAR(20) NOT NULL,
CONSTRAINT CONS1 PRIMARY KEY (COMPOSITE_ID1_WHICH_IS_ALSO_A_FK,
COMPOSITE_ID2),
CONSTRAINT CONS2 FOREIGN KEY (COMPOSITE_ID1_WHICH_IS_ALSO_A_FK)
REFERENCES MYSCHEMA.MYTABLE3(ID),
CONSTRAINT CONS3 UNIQUE (COLUMN3),
CONSTRAINT CONS4 CHECK(COMPOSITE_ID2 IN(1,0) )
) ;
CREATE SEQUENCE MYSCHEMA.MY_SEQ
START WITH 1
INCREMENT BY 1
NOMAXVALUE;
CREATE TABLE MYSCHEMA.MYTABLE2 (
ID NUMBER NOT NULL,
COLUMN1 VARCHAR(6) NOT NULL,
COMPOSITE_ID1_WHICH_IS_ALSO_A_FK NUMBER NOT NULL,
COMPOSITE_ID2 NUMBER(1,0) NOT NULL,
TIME TIMESTAMP(0) NULL,
CONSTRAINT CONS5 PRIMARY KEY (ID),
CONSTRAINT CONS6 FOREIGN KEY (COMPOSITE_ID1_WHICH_IS_ALSO_A_FK,
COMPOSITE_ID2) REFERENCES MYSCHEMA.MYTABLE
(COMPOSITE_ID1_WHICH_IS_ALSO_A_FK, COMPOSITE_ID2)
) ;
Thanks in advance,
Peter