-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Mapping a Composite Foreing Key
PostPosted: Fri Feb 26, 2010 8:43 pm 
Newbie

Joined: Thu Feb 25, 2010 3:48 pm
Posts: 2
Hi,
I have a big problem on mapping composite pks into fks and I already tried a lot of manners...
My doubt is how would you map a table like this below without using an aux class to represent the composite key?

I've already tried a lot of things I found in other forums but nothing worked.
The tables are quite simple I just need to see how to do the mapping mainly for field_def.

I have the tables:
Code:
drop table field_def;
drop table field;
drop table entity;

create table entity (
   name text not null,
   constraint pk_entity primary key(name)
);
create table field (
   name text not null,
   entity_name text not null,
   constraint pk_field primary key(entity_name, name)
);

create table field_def(
   name text not null,
   entity_name text not null,
   field_name text not null,
   constraint pk_field_def primary key(entity_name, field_name, name)
);

alter table field add constraint fk_entity foreign key(entity_name) references entity(name);

alter table field_def add constraint fk_field foreign key(entity_name, field_name) references field(entity_name, name);


And the classes:
Code:
package home.model;

public class Entity {

    private String name;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

}

package home.model;

public class Field {

    private String name;
    private Entity entity;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the entity
     */
    public Entity getEntity() {
        return entity;
    }

    /**
     * @param entity the entity to set
     */
    public void setEntity(Entity entity) {
        this.entity = entity;
    }

}

package home.model;

public class FieldDef {

    private String name;
    private Field field;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the field
     */
    public Field getField() {
        return field;
    }

    /**
     * @param field the field to set
     */
    public void setField(Field field) {
        this.field = field;
    }
}


And the mappings:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="home.model.Entity">
        <id name="name" />
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="home.model.Field">
        <composite-id>
            <key-property name="name" />
            <key-property name="entity" column="name" />
        </composite-id>

        <many-to-one name="entity" class="home.model.Entity" insert="false" update="false" >
            <column name="entity_name" />
        </many-to-one>
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="home.model.FieldDef">
        <composite-id>
            <key-property name="name" />
            <key-property name="field" >
                <column name="name" />
                <column name="entity_name" />
            </key-property>
        </composite-id>

        <many-to-one name="field" class="home.model.Field" insert="false" update="false">
            <column name="field_name" />
            <column name="entity_name" />
        </many-to-one>
    </class>
</hibernate-mapping>


But i keep receiving the following error:
org.hibernate.MappingException: Foreign key (FKCC7A74CB8AA88F5D:FieldDef [field_name,entity_name])) must have same number of columns as the referenced primary key (Field [name])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:90)
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:73)

What am I doing wrong?

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.