尝试在hibernate中映射Map <integer,string>时出现唯一索引或主键冲突

时间:2016-10-09 11:40:53

标签: java hibernate h2

我正在使用H2嵌入式数据库和Hibernate 5.我正在尝试以这种方式在hibernate中映射HashMap:

@Entity
public class TestMapping
{
    @Id
    @GeneratedValue
    private Long id;


    @ElementCollection
    private Map<Integer,String> map = new HashMap<>();


}

然后我坚持TestMapping对象包含几个对键值。当我试图通过执行类似这样的事情来看Intellij中的结果时:

SELECT t.* FROM PUBLIC.TESTMAPPING t LIMIT 501

我收到此错误:

[23505][23505] Unique index or primary key violation: "PRIMARY KEY ON """".PAGE_INDEX"; SQL statement: ALTER TABLE PUBLIC.TESTMAPPING_MAP ADD CONSTRAINT PUBLIC.FK8CYRSMJWNRD21DCB8T901RHA0 FOREIGN KEY(TESTMAPPING_ID) REFERENCES PUBLIC.TESTMAPPING(ID) NOCHECK [23505-176]

我试图添加@MapKeyColumn无效。

mydb.trace.db包含:

10-09 13:38:28 database: ALTER TABLE PUBLIC.TESTMAPPING_IMAGES ADD CONSTRAINT PUBLIC.FK9V996C496B8GCB45MJI7WDA0D FOREIGN KEY(TESTMAPPING_ID) REFERENCES PUBLIC.TESTMAPPING(ID) NOCHECK org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY KEY ON """".PAGE_INDEX"; SQL statement: ALTER TABLE PUBLIC.TESTMAPPING_IMAGES ADD CONSTRAINT PUBLIC.FK9V996C496B8GCB45MJI7WDA0D FOREIGN KEY(TESTMAPPING_ID) REFERENCES PUBLIC.TESTMAPPING(ID) NOCHECK [23505-176] 10-09 13:38:29 database: ALTER TABLE PUBLIC.TESTMAPPING_IMAGES ADD CONSTRAINT PUBLIC.FK9V996C496B8GCB45MJI7WDA0D FOREIGN KEY(TESTMAPPING_ID) REFERENCES PUBLIC.TESTMAPPING(ID) NOCHECK org.h2.jdbc.JdbcSQLException: Unique index or primary key violation: "PRIMARY KEY ON """".PAGE_INDEX"; SQL statement: ALTER TABLE PUBLIC.TESTMAPPING_IMAGES ADD CONSTRAINT PUBLIC.FK9V996C496B8GCB45MJI7WDA0D FOREIGN KEY(TESTMAPPING_ID) REFERENCES PUBLIC.TESTMAPPING(ID) NOCHECK [23505-176]

1 个答案:

答案 0 :(得分:2)

在1.3.176版本的h2控制台中似乎存在一个错误。  根据建议here,升级到较新的h2版本可能有所帮助。