错误的语法,关键字 ‘key’ 附近有问题。

huangapple go评论74阅读模式
英文:

Incorrect syntax near the keyword 'key'

问题

我正试图将一个新对象持久化到数据库中,但是我遇到了这个错误:

SQL错误:156,SQL状态:S0001

关键字'key'附近的语法不正确。

我的操作

Bucket bucket = new Bucket();
bucket.setKey(key);
bucket.setValue(value);

entityManager.persist(bucket);

我认为这个错误是因为在 SQL 语句中使用了关键字'key'导致的。是否有一种本地查询可以用来向我的表中插入行?

我的表有这三列,其中 Id 是自动生成的。

Bucket:

@Entity
@Getter
@Setter
@ToString
public class Bucket {

    @Id @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long id; //自动生成的主键
    private String key;
    private String value;
}
英文:

I am trying to persist a new object into the database, but i am getting this error:

>SQL Error: 156, SQLState: S0001
>
>Incorrect syntax near the keyword 'key'.

What I am doing

Bucket bucket = new Bucket();
bucket.setKey(key);
bucket.setValue(value);


entityManager.persist(bucket);

I believe this error is due to the keyword 'key' being used in sql statements causing this. Is there a native query that I can use to insert rows into my table?

My table has these three columns, with Id being auto-generated.

Bucket:

@Entity
@Getter
@Setter
@ToString
public class Bucket {

    @Id @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long id; //auto allocation of PK
    private String key;
    private String value;
} 

答案1

得分: 2

谢谢Martin Smith的帮助。只需要一个简单的更改,问题就解决了。

桶(Bucket):

@Entity
@Getter
@Setter
@ToString
public class Bucket {

    @Id @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long id; //主键自动分配
    @Column(name = "key")
    private String key;
    @Column(name = "value")
    private String value;
} 

注意:我已经根据你的要求进行了代码部分的翻译,如果还有其他需要,请随时告诉我。

英文:

Thank you Martin Smith for the help. Just one simple change and the issue was resolved.

Bucket:

@Entity
@Getter
@Setter
@ToString
public class Bucket {

    @Id @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long id; //auto allocation of PK
    @Column(name = "\"key\"")
    private String key;
    @Column(name = "\"value\"")
    private String value;
} 

huangapple
  • 本文由 发表于 2020年5月30日 20:56:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/62102748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定