当前位置: 首页 > 知识库问答 >
问题:

commandAcceptanceException:执行DDL“CREATE table TABLE_JSON”时出错

竺翰海
2023-03-14
import org.json.JSONObject;

@Entity
@Table(name = "TABLE_JSON")
public class DataJson {

    @Id
    @Type(type = "uuid-char")
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "id", updatable = false, nullable = false)
    private UUID id;

    @NonNull
    private String check;

    @NonNull
    @Column(columnDefinition = "TEXT")
    @Convert(converter= JSONObjectConverterForMe.class)
    private JSONObject json_data;

}
import org.json.JSONObject;

@Converter
public class JSONObjectConverterForMe implements AttributeConverter<JSONObject, String> {
    @Override
    public String convertToDatabaseColumn(JSONObject jsonData) {
        String json;
        try{
            json = jsonData.toString();
        }
        catch (NullPointerException ex)
        {
            //extend error handling here if you want
            json = "";
        }
        return json;
    }

    @Override
    public JSONObject convertToEntityAttribute(String jsonDataAsJson) {
        JSONObject jsonData;
        try {
            jsonData = new JSONObject(jsonDataAsJson);
        } catch (JSONException ex) {
            //extend error handling here if you want
            jsonData = null;
        }
        return jsonData;
    }
}
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table TABLE_JSON (id varchar(255) not null, check varchar(255), json_data TEXT, primary key (id))" via JDBC Statement

以下是完整的错误详细信息

共有1个答案

桂玉石
2023-03-14

check是SQL保留关键字,因此不能将check用作没有引号/反勾的列名。默认情况下,hibernate不使用quote/backtick作为查询中的列名。您可以这样显式地使用backtick/quote

@Column(name="`check`")
private String check;

在某些数据库中,backtick不起作用例如:postgresql使用引用

@Column(name="\"check\"")
private String check;

也可以强制hibernate在配置文件中使用引号来表示列和表名

hibernate.globally_quoted_identifiers=true
@Column(name = "id", length = 36 , updatable = false, nullable = false)
private UUID id;
 类似资料: