facebook

Can a primary key be a composite key?

  1. MyEclipse IDE
  2.  > 
  3. Java EE Development (EJB, JSP, Struts, XDoclet, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #241998 Reply

    pcleung
    Member

    Database: Oracle 10g
    Hibernate: 3
    I have 4 tables with foreign keys.
    Tables has composite keys.

    In DB Browser, I right click the table.
    select “Generate Hibernate Mapping”
    ID Generator: native
    Types: Use Java Types

    New table java files and Key java files are auto generated.

    But when I run the webapp, the following errors display:

    org.hibernate.MappingException: Foreign key (FK9DB7177B3984DB05:FUNCTIONS [PROGRAM_HREF])) must have same number of columns as the referenced primary key (ROLE_PROGRAM [PROGRAM_HREF,ROLE_HREF,USERNM])

    <hibernate-mapping package=”com.erp.hibernate.user”>
    <class name=”Functions” table=”FUNCTIONS”>
    <composite-id name=”id” class=”FunctionsKey”>
    <key-property name=”functionHref” column=”FUNCTION_HREF” type=”java.lang.String”/>
    <key-many-to-one name=”roleProgram2″ column=”PROGRAM_HREF” class=”RoleProgram”/>
    <key-many-to-one name=”roleProgram1″ column=”ROLE_HREF” class=”RoleProgram”/>
    <key-many-to-one name=”roleProgram” column=”USERNM” class=”RoleProgram”/>
    </composite-id>
    <property name=”messageKey” column=”MESSAGE_KEY” type=”java.lang.String” not-null=”true” />
    <property name=”creationDate” column=”CREATION_DATE” type=”java.util.Date” not-null=”true” />
    <property name=”createdBy” column=”CREATED_BY” type=”java.lang.String” not-null=”true” />
    <property name=”lastUpdateDate” column=”LAST_UPDATE_DATE” type=”java.util.Date” not-null=”true” />
    <property name=”lastUpdatedBy” column=”LAST_UPDATED_BY” type=”java.lang.String” not-null=”true” />
    </class>
    </hibernate-mapping>

    <hibernate-mapping package=”com.erp.hibernate.user”>
    <class name=”RoleProgram” table=”ROLE_PROGRAM”>
    <composite-id name=”id” class=”RoleProgramKey”>
    <key-property name=”programHref” column=”PROGRAM_HREF” type=”java.lang.String”/>
    <key-many-to-one name=”userRole1″ column=”ROLE_HREF” class=”UserRole”/>
    <key-many-to-one name=”userRole” column=”USERNM” class=”UserRole”/>
    </composite-id>
    <property name=”messageKey” column=”MESSAGE_KEY” type=”java.lang.String” not-null=”true” />
    <property name=”creationDate” column=”CREATION_DATE” type=”java.util.Date” not-null=”true” />
    <property name=”createdBy” column=”CREATED_BY” type=”java.lang.String” not-null=”true” />
    <property name=”lastUpdateDate” column=”LAST_UPDATE_DATE” type=”java.util.Date” not-null=”true” />
    <property name=”lastUpdatedBy” column=”LAST_UPDATED_BY” type=”java.lang.String” not-null=”true” />
    <set name=”functionsSet” inverse=”true”>
    <key column=”USERNM”/>
    <one-to-many class=”Functions”/>
    </set>
    <set name=”functions1Set” inverse=”true”>
    <key column=”ROLE_HREF”/>
    <one-to-many class=”Functions”/>
    </set>
    <set name=”functions2Set” inverse=”true”>
    <key column=”PROGRAM_HREF”/>
    <one-to-many class=”Functions”/>
    </set>
    </class>
    </hibernate-mapping>

    I have no errors when tables do not have composite and foreign key!

    #242164 Reply

    Riyad Kalla
    Member

    This should work, we did replace our mapping generation in what will be our 4.1 Milestone 2 release in about a week, would you be able to wait until that time and try the new mapping generator and see if it works with your DB?

    #242248 Reply

    pingcheungl
    Member

    @support-rkalla wrote:

    This should work, we did replace our mapping generation in what will be our 4.1 Milestone 2 release in about a week, would you be able to wait until that time and try the new mapping generator and see if it works with your DB?

    Will the next release teach me which ID generator I should choose?

    #242252 Reply

    Riyad Kalla
    Member

    Not likely, the type of generator you use depends on your table. For example, my PK fields always tend to be autoincrement fields, so I always use the “native” generator. I usually prefer to let the DB do what it is good at and let Hibernate worry about persistence.

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: Can a primary key be a composite key?

You must be logged in to post in the forum log in