2013-03-20 42 views
0

目標: 我試圖瞭解hibernate映射。當我嘗試的映射表,我得到了這樣的錯誤外鍵(FK300D262149997B:four [three_id]))的列數必須與引用的主鍵相同(三[one_id,two_id])

外鍵(FK300D262149997B:四[three_id]))必須有相同數量的被引用的主鍵(列3 [one_id,two_id])

的Hibernate映射:

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`)); 

實體:

One.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; 
    } 
} 

Two.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; 
    } 
} 

Three.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; 
    } 
} 

Four.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; 
    } 
} 

我怎樣才能讓這個工作?

+0

看看下面的鏈接,它可以幫助你 http://stackoverflow.com/questions/14510547/hibernate-issue-foreign-key-must-have-同數的柱 - 如引用-PRI – AntonyManoj 2013-03-20 06:01:20

+0

稱作。但他們有不同的hibernate映射,沒有解決方案。 – Mohan 2013-03-20 07:31:12

回答

0

你的錯誤是告訴你的表包含一個外鍵列,但你引用的表定義它有兩列,它的方式是很常見的關係表的主鍵。

see here的細節

+0

是的。那麼關係將如何看起來像(ddl)? – Mohan 2013-03-20 05:55:37

+0

你需要映射覆合主鍵映射文件 – PSR 2013-03-20 05:56:22

+0

如果我們映射覆合主鍵,表「三」怎麼會參考上表中「四」(見表結構和實體)。 – Mohan 2013-03-20 07:00:05

相關問題