Hibernate Annotations

Discussion in 'Java' started by Peter Horlock, Aug 3, 2009.

  1. 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
     
    Peter Horlock, Aug 3, 2009
    #1
    1. Advertising

  2. 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
     
    Peter Horlock, Aug 3, 2009
    #2
    1. Advertising

  3. Peter Horlock

    Roedy Green Guest

    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.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)
     
    Roedy Green, Aug 4, 2009
    #3
  4. Peter Horlock

    Simon Guest

    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
     
    Simon, Aug 5, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    0
    Views:
    1,723
    Ilias Lazaridis
    Dec 27, 2004
  2. Replies:
    0
    Views:
    3,718
  3. kal
    Replies:
    2
    Views:
    4,493
    Tor Iver Wilhelmsen
    Dec 31, 2006
  4. jstorta
    Replies:
    1
    Views:
    11,764
  5. Steve
    Replies:
    0
    Views:
    329
    Steve
    Jun 26, 2008
Loading...

Share This Page