Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Hibernate Annotations

Reply
Thread Tools

Hibernate Annotations

 
 
Peter Horlock
Guest
Posts: n/a
 
      08-03-2009
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
 
Reply With Quote
 
 
 
 
Peter Horlock
Guest
Posts: n/a
 
      08-03-2009
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-04-2009
On Mon, 3 Aug 2009 10:15:04 -0700 (PDT), Peter Horlock
<> wrote, quoted or indirectly quoted
someone who said :

>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.comp ositeId1

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)
 
Reply With Quote
 
Simon
Guest
Posts: n/a
 
      08-05-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL from Hibernate Annotations Steve Java 0 06-26-2008 02:13 PM
Hibernate cannot find hibernate.cfg.xml jstorta Java 1 01-19-2008 01:45 PM
hibernate annotations I get a MappingNotFoundException kal Java 2 12-31-2006 11:06 AM
Hibernate Syncronizer now generates code for Hibernate 3.0 msenin@covad.net Java 0 07-14-2005 05:47 AM
[HIBERNATE] [EVALUATION] - Gavin King Censors Hibernate Forum Ilias Lazaridis Java 0 12-27-2004 04:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57