Database Design Problem with Hibernate Primary Key and Foreign Key

From: Tung Chau (tungchau81_at_yahoo.com)
Date: 09/03/04

  • Next message: Robert Bowen: "Re: Why does Oracle store HTML tags in BLOB as symbols?"
    Date: 2 Sep 2004 21:08:17 -0700
    
    

    hi,
    I am a newbie to Hibernate. It seems to me that Hibernate does not
    allow you to declare both id and composite-id in .hbm.xml mapping.
    That was why i have a triple primary key in my ENROLLED table as
    following:
    CREATE TABLE ENROLLED ( SID INTEGER NOT NULL,
        CID INTEGER NOT NULL,
        ENROLL_SINCE DATETIME NOT NULL,
        EID INTEGER NOT NULL auto_increment,
        COURSE_ROLE_ID INTEGER DEFAULT 0,
        LAST_LESSON_ID INTEGER,
        COMPLETE_BY_DATE DATETIME,
        GRADE VARCHAR(10),
        STATUS_ID MEDIUMINT(9),
        IS_AVAILABLE CHAR(1) DEFAULT 'Y',
        CREATION_DATE DATETIME NOT NULL,
        LAST_UPDATED DATETIME NOT NULL,
        PRIMARY KEY (SID, CID, ENROLL_SINCE),
        UNIQUE KEY (EID),
        FOREIGN KEY(SID) REFERENCES USERS .....
    and my QUIZ_GRADES table need to reference the ENROLLED table. That
    was why I had EID as unique key.
    CREATE TABLE QUIZ_GRADES (QUIZ_ID INTEGER NOT NULL,
          EID INTEGER NOT NULL,
          PASS CHAR(1),
          CREATION_DATE DATETIME NOT NULL,
          PRIMARY KEY(QUIZ_ID, EID),
          FOREIGN KEY (QUIZ_ID)
          REFERENCES QUIZES
          ON DELETE CASCADE,
          FOREIGN KEY (EID)
          REFERENCES ENROLLED(EID)
          ON DELETE CASCADE)TYPE=INNODB;
    However, Hibernate complained as following:
    "******Foreign key (QUIZ_GRADES [EID])) must have same number of
    columns as the r
    eferenced primary key (ENROLLED [SID,CID,ENROLL_SINCE])
    net.sf.hibernate.MappingException: Foreign key (QUIZ_GRADES [EID]))
    must have sa
    me number of columns as the referenced primary key (ENROLLED
    [SID,CID,ENROLL_SIN
    CE])"
    I can't decleare both id and composite-id in Enrolled.hbm.xml. My
    original design of the ENROLLED table was as following:
    CREATE TABLE ENROLLED (EID INTEGER NOT NULL auto_increment PRIMARY
    KEY,
                                                    SID INTEGER NOT NULL,
                                                    CID INTEGER NOT NULL,
                                                    COURSE_ROLE_ID INTEGER DEFAULT 0,
                                                    LAST_LESSON_ID INTEGER,
                                                    ENROLL_SINCE DATETIME NOT NULL,
                                                    COMPLETE_BY_DATE DATETIME,
                                                    GRADE VARCHAR(10),
                                                    STATUS_ID MEDIUMINT(9),
                                                    IS_AVAILABLE CHAR(1) DEFAULT 'Y',
                                                    CREATION_DATE DATETIME NOT NULL,
                                                    LAST_UPDATED DATETIME NOT NULL,
                                                    UNIQUE(SID, CID, ENROLL_SINCE),
                                                    FOREIGN KEY(SID) REFERENCES USERS(USER_ID)
                                                    ON DELETE CASCADE,
                                                    FOREIGN KEY(CID) REFERENCES COURSES(ID)
                                                    ON DELETE CASCADE,
                                                    FOREIGN KEY(COURSE_ROLE_ID)
                                                    REFERENCES COURSE_ROLES(ROLE_ID) ON DELETE SET NULL,
                                                    FOREIGN KEY(LAST_LESSON_ID)
                                                    REFERENCES LESSONS(LESSON_ID)
                                                    ON DELETE SET NULL,
                                                    FOREIGN KEY(STATUS_ID) REFERENCES STATUS
                                                    )TYPE=INNODB;
    I couldn't find any "unique" declaration syntax in Hibernate's
    mapping. That was why I had to stick with composite-id. I like using
    composite-id though.

    Do you have any suggestion to go around this problem?
    I appreciate any help.
    Thanks,
    -tungchau


  • Next message: Robert Bowen: "Re: Why does Oracle store HTML tags in BLOB as symbols?"

    Relevant Pages

    • Re: [Hibernate] wechselnde Fehler bei Update
      ... >>Der Primary Key ist für neue Events 0. ... Hibernate kann also eigentlich ... Prev by Date: ... Next by Date: ...
      (de.comp.lang.java)
    • Re: [Hibernate] wechselnde Fehler bei Update
      ... >Der Primary Key ist für neue Events 0. ... Hibernate kann also eigentlich ... Prev by Date: ... Next by Date: ...
      (de.comp.lang.java)
    • Re: nhibernate - id change
      ... > You just can't do that - Hibernate would get very confused about which ... > It sounds like you really need a different primary key, ... after setting new key id in the database object but before saving to ... Prev by Date: ...
      (microsoft.public.dotnet.languages.csharp)
    • Hibernate : efficient way to load an object.
      ... I am using hibernate 3.0 and would like to know about the comparative ... performance of its features. ... if the primary key is a combination of two columns (composite ... Prev by Date: ...
      (comp.lang.java.programmer)
    • Re: hibernate to legacy DB with no ID columns
      ... I'm not sure what Hibernate means by "id" but in my ... unique identifier not related to the purpose of the table. ... A primary key on the other hand _can_ be related to the table contents ... for example an employee number or even an employee name. ...
      (comp.lang.java.programmer)