无法创建具有复合键列的 H2 SQL 表

问题描述

我有以下 sql 架构:

DROP TABLE IF EXISTS COUNTRY;
  
CREATE TABLE COUNTRY (
  id VARCHAR (50) NOT NULL,idFromX INT (50) NOT NULL,idFromY INT (50) NOT NULL,idFromZ INT (50) NOT NULL,name VARCHAR (50) NOT NULL,abbreviation VARCHAR (5) NOT NULL,createdAt TIMESTAMP NOT NULL,updatedAt TIMESTAMP NOT NULL,PRIMARY KEY(idFromX,abbreviation)
);

问题是没有为 PRIMARY KEY 组合创建额外的列,而是我有一个自动递增的 id 列。

我的实体表如下所示:

package com.drenski.country.entity;

import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name = "COUNTRY")
//@IdClass(CountryId.class)
public class Country {
    
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private String id;
            
//  @Id
    @Column
    private int idFromX;
    
//  @Id
    @Column
    private int idFromY;
    
//  @Id
    @Column
    private int idFromZ;    
    
    @Column
    private String abbreviation;
    
    @Column
    private String name;
    
    @Column
    private LocalDateTime createdAt;
    
    @Column
    private LocalDateTime updatedAt;
    
    public Country() {}
    
    public Country(int idFromX,int idFromY,int idFromZ,String abbreviation){
        this.idFromX = idFromX;
        this.idFromY = idFromY;
        this.idFromZ = idFromZ;
        this.abbreviation = abbreviation;
    }
    
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getIdFromX() {
        return idFromX;
    }

    public void setIdFromX(int idFromX) {
        this.idFromX = idFromX;
    }

    public int getIdFromY() {
        return idFromY;
    }

    public void setIdFromY(int idFromY) {
        this.idFromY = idFromY;
    }

    public int getIdFromZ() {
        return idFromZ;
    }

    public void setIdFromZ(int idFromZ) {
        this.idFromZ = idFromZ;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAbbreviation() {
        return abbreviation;
    }

    public void setAbbreviation(String abbreviation) {
        this.abbreviation = abbreviation;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((abbreviation == null) ? 0 : abbreviation.hashCode());
        result = prime * result + ((createdAt == null) ? 0 : createdAt.hashCode());
        result = prime * result + idFromX;
        result = prime * result + idFromY;
        result = prime * result + idFromZ;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((updatedAt == null) ? 0 : updatedAt.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Country other = (Country) obj;
        if (abbreviation == null) {
            if (other.abbreviation != null)
                return false;
        } else if (!abbreviation.equals(other.abbreviation))
            return false;
        if (createdAt == null) {
            if (other.createdAt != null)
                return false;
        } else if (!createdAt.equals(other.createdAt))
            return false;
        if (idFromX != other.idFromX)
            return false;
        if (idFromY != other.idFromY)
            return false;
        if (idFromZ != other.idFromZ)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        if (updatedAt == null) {
            if (other.updatedAt != null)
                return false;
        } else if (!updatedAt.equals(other.updatedAt))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Country [idFromX=" + idFromX + ",idFromY=" + idFromY + ",idFromZ=" + idFromZ + ",abbreviation="
                + abbreviation + ",name=" + name + ",createdAt=" + createdAt + ",updatedAt=" + updatedAt + "]";
    }
    
}

复合类如下所示:

package com.drenski.country.entity;

import java.io.Serializable;

public class CountryId implements Serializable {

    private static final long serialVersionUID = 1L;

    private int idFromX;
    
    private int idFromY;
    
    private int idFromZ;
    
    private String abbreviation;
    
    public CountryId() {}
    
    public CountryId(int idFromX,String abbreviation) {
        this.idFromX = idFromX;
        this.idFromY = idFromY;
        this.idFromZ = idFromZ;
        this.abbreviation = abbreviation;
    }

    public int getIdFromX() {
        return idFromX;
    }

    public void setIdFromX(int idFromX) {
        this.idFromX = idFromX;
    }

    public int getIdFromY() {
        return idFromY;
    }

    public void setIdFromY(int idFromY) {
        this.idFromY = idFromY;
    }

    public int getIdFromZ() {
        return idFromZ;
    }

    public void setIdFromZ(int idFromZ) {
        this.idFromZ = idFromZ;
    }

    public String getAbbreviation() {
        return abbreviation;
    }

    public void setAbbreviation(String abbreviation) {
        this.abbreviation = abbreviation;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((abbreviation == null) ? 0 : abbreviation.hashCode());
        result = prime * result + idFromX;
        result = prime * result + idFromY;
        result = prime * result + idFromZ;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        CountryId other = (CountryId) obj;
        if (abbreviation == null) {
            if (other.abbreviation != null)
                return false;
        } else if (!abbreviation.equals(other.abbreviation))
            return false;
        if (idFromX != other.idFromX)
            return false;
        if (idFromY != other.idFromY)
            return false;
        if (idFromZ != other.idFromZ)
            return false;
        return true;
    }
    
}

我想要实现的是将 String id 列作为我在架构 PRIMARY KEY 中的列的组合。

目前看起来是这样的: db table

PS:我尝试了@IdClass 和@Embeddedid 方法,但我无法弄清楚。我做错了什么,应该怎么做?

PS2:我不想使用 @GeneratedValue 并让它自动递增,否则我会收到此处提到的错误Hibernate error: ids for this class must be manually assigned before calling save():

预先感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)