Hibernate Annotations

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
 
P

Peter Horlock

Sorry! Here's the error I get:

org.hibernate.AnnotationException: Unable to find properties
(compositeId1, compositeId2) in entity annotated with @IdClass:
com.company.package1.package2.package3.MyTable

Thanks,

Peter
 
R

Roedy Green

org.hibernate.AnnotationException: Unable to find properties
(compositeId1, compositeId2) in entity annotated with @IdClass:
com.company.package1.package2.package3.MyTable

I am a Hibernate virgin, but reading the error message literally,

It seems to be looking for a field called:
com.company.package.package2.package3.MyTable.compositeId1

The actual name of the field is likely something like:
com.company.package.package2.package3.MyPrimaryKey.compositeId1

I hope that package name is just something you wrote to mask
confidential information. That is a terrible name for a real package.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Patriotism is fierce as a fever, pitiless as the grave, blind as a stone, and as irrational as a headless hen."
~ Ambrose Bierce (born: 1842-06-24 died: 1914 at age: 71)
 
S

Simon

I'm also not an expert, but since noone else replied, I will give it a try.
@Entity
@IdClass(value = MyPrimaryKey.class)
@Table(name = "MYSCEMA.MYTABLE")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
public class MyTable implements Serializable
{
@Id
private MyPrimaryKey id;

Shouldn't this be replaced by

@Id
private Long compositeId1;

@Id
private Boolean compositeId2;

@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;
[..]
}

As far as I know, you don't use the class referenced by @IdClass as a
key, but you rather take all its properties as keys, each one annotated
with @Id. Otherwise I wouldn't see why it would help to specify an
@IdClass if the @Id is actually an instance of this class.

I never tried @Embeddable as a key, but if it works I would assume you
don't need the @IdClass annotation. What happens if you omit it?

Cheers,
Simon

f'Up2 c.l.j.p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top