【问题标题】:Foreign key (FK300D262149997B:four [three_id])) must have same number of columns as the referenced primary key (three [one_id,two_id])外键 (FK300D262149997B:four [three_id])) 的列数必须与引用的主键 (three [one_id,two_id]) 相同
【发布时间】:2013-03-20 05:45:50
【问题描述】:

目标: 我试图了解休眠映射。当我尝试映射表时,出现类似

的错误

外键(FK300D262149997B:four [three_id]))必须与引用的主键(三个 [one_id,two_id])具有相同的列数

休眠映射:

1). Created table 'one' with two columns (primary key and name field).
2). Created table 'two' with two columns (primary key and name field).
3). Created table 'three' with three columns (primary key, foreign key reference of table 'one' and foreign key reference of table 'two').
4). Created table 'four' with three columns (primary key, foreign key reference of table 'three' and name field).
5). Entity class 'One' has the set of 'Two' class.

问题:

When mapping is compiled am getting following error. 

Foreign key (FK300D262149997B:four [three_id])) must have same number of columns as the referenced primary key (three [one_id,two_id])

附加来源:

DDL:

CREATE TABLE ONE(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(45));

CREATE TABLE two(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(45));

CREATE TABLE three(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
one_id BIGINT,
two_id BIGINT,
KEY `one_id` (`one_id`),
KEY `two_id` (`two_id`),
CONSTRAINT `fk_one_three` FOREIGN KEY (`one_id`) REFERENCES `one` (`id`),
CONSTRAINT `fk_two_three` FOREIGN KEY (`two_id`) REFERENCES `two` (`id`) );

CREATE TABLE four(
id BIGINT PRIMARY KEY AUTO_INCREMENT,
three_id BIGINT,
NAME VARCHAR(45),
KEY `three_id` (`three_id`),
CONSTRAINT `fk_three_four` FOREIGN KEY (`three_id`) REFERENCES `three` (`id`));

实体:

一个.java

public class One {
    private long id;
    private String name;
    private Set<Two> twos;

    public Set<Two> getTwos() {
        return twos;
    }
    public void setTwos(Set<Two> twos) {
        this.twos = twos;
    }
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

两个.java

public class Two {
    private long id;
    private String name;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

三个.java

public class Three {
    private long id;
    private One one;
    private Two two;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public One getOne() {
        return one;
    }
    public void setOne(One one) {
        this.one = one;
    }
    public Two getTwo() {
        return two;
    }
    public void setTwo(Two two) {
        this.two = two;
    }   
}

四.java

public class Four {
    private long id;
    private Three three;
    private String name;
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public Three getThree() {
        return three;
    }
    public void setThree(Three three) {
        this.three = three;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   
}

我怎样才能让它工作?

【问题讨论】:

标签: java mysql database hibernate mapping


【解决方案1】:

您的错误是告诉您的表在列上包含一个外键,但您引用的表定义它的主键有两列,顺便说一句,这对于关系表来说很常见。

see here 了解详情

【讨论】:

  • 是的。那么这种关系将如何(ddl)?
  • 映射文件中需要映射复合主键
  • 如果我们映射复合主键,表“三”将如何引用表“四”(参见表结构和实体)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-18
  • 1970-01-01
  • 2019-11-18
  • 2019-07-30
  • 2013-01-08
  • 2016-08-22
  • 1970-01-01
相关资源
最近更新 更多