我的Java Spring Boot项目中有一个类:
@Entity
@Table(name = "attribute_table")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Attribute {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "attribute_type")
private AttributeType attributeType;
@Column(name = "value")
private byte[] value;
@ManyToOne
@JoinColumn(name = "asset_category_id")
private AssetCategory assetCategory;
}
运行后,我得到一个异常:
org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "create table test1234 (attribute_type tinyint check (attribute_type between 0 and 3), asset_category_id bigint, id bigint generated by default as identity, name varchar(255), [*]value varbinary(255), primary key (id))"; expected "identifier";
我发现,当表的名称与保留的SQL关键字相同时,就会发生这种类型的错误,但我try 了几个@Table注释的名称,这显然不是原因.
我不知道它是否重要,但在我的属性类中,我有value
个字段,它是byte[],因为它的类型取决于AttributeType枚举.此外,AssetCategory中还有BaseClass categoryOf
个没有明确定义的类,因为它可以与从BaseClass继承的其他类映射.
以下是我的项目中的其他类:
@Entity
@Table(name = "asset_categories")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class AssetCategory {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Lob
@Column(name = "description")
private String description;
@OneToMany(mappedBy = "assetCategory")
private List<Attribute> attributes;
@OneToOne(mappedBy = "assetCategory")
private BaseClass categoryOf;
public void addAttribute(Attribute attribute) {
attributes.add(attribute);
}
}
@Getter
@Setter
@Entity
public abstract class BaseClass {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
protected Long id;
@Column(name = "name")
protected String name;
@OneToOne
@JoinColumn(name = "asset_category", referencedColumnName = "id")
protected AssetCategory assetCategory;
@Column(name = "archived")
protected Boolean archived;
}
public enum AttributeType {
NUMBER,
STRING,
DATE,
BOOL
}