0

I have these two tables:

CREATE TABLE IF NOT EXISTS `functions` (
`idFunction` int(11) NOT NULL,
`idLanguage` int(11) NOT NULL,
`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`idCategory` int(11) DEFAULT '1',
PRIMARY KEY (`idFunction`,`idLanguage`),
KEY `idCategory` (`idCategory`),
KEY `idLanguage` (`idLanguage`),
CONSTRAINT `FK_functions_categories` FOREIGN KEY (`idCategory`) REFERENCES            
`category_package` (`idCategory`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `FK_functions_languages` FOREIGN KEY (`idLanguage`) REFERENCES `languages`  (`idLanguage`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `subfunctions` (
`idParent` int(11) NOT NULL,
`idChild` int(11) NOT NULL,
PRIMARY KEY (`idParent`,`idChild`),
KEY `idChild` (`idChild`),
CONSTRAINT `FK_subfunctions_functions` FOREIGN KEY (`idParent`) REFERENCES `functions` (`idFunction`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_subfunctions_functions_2` FOREIGN KEY (`idChild`) REFERENCES `functions` (`idFunction`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

And when I'm trying to map these tables through Hibernate's Reverse Engineering the mapping occurs but when I try to do a query involving the two tables Hibernate throws me this exception:

Foreign key (FKFD0F515B161D5928:subfunctions [idParent])) must have same number of columns as the referenced primary key (functions [idFunction,idLanguage])

I know that the real deal is to put idLanguage as a PK in subfunctions table too, but I'm using a legacy DB and I can't modify it.

Is there any way to map this?

frikkio
  • 93
  • 2
  • 10

1 Answers1

0

You don't give a lot of details on the mapping, so it is not easy to assist you here. Generally since your primary key of both tables is a composite key, this could be mapped in an embeddable class like it shows in http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2177

A sample mapping of the above classes could be something like:

@Entity
@Table(name = "functions")
public class Functions {

    @Id
    private FunctionPK functionPK;

    @Column(name="name", nullable=false)
    private String name;

    @Column(name="idCategory", nullable=false)
    @ManyToOne(cascade=CascadeType.SAVE_UPDATE)
    @JoinColumn(name = "idCategory")
    private CategoryPackage idCategory;

...
}

@Embeddable 
  private class FunctionPK implements Serializable
  {

    @Column(name="idFunctions")
    private int idFunctions;

    @Column(name="idLanguage")
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "idLanguage")
    private Language idLanguage;

    //Some getters and setters 
    ...
  }

@Entity
@Table(name = "subfunctions")
public class SubFunctions {

    @Id
    private SubFunctionPK subFunctionPK;

...
}

@Embeddable 
  private class SubFunctionPK implements Serializable
  {

    @Column(name="idParent")
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "idFunction")
    private Functions idParent;

    @Column(name="idChild")
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "idFunction")
    private Functions idChild;

    //Some getters and setters 
    ...
  }
Makis Arvanitis
  • 1,175
  • 1
  • 11
  • 18