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