我試圖對(duì)字符串列表進(jìn)行字符串化,并JSON使用hibernate將其存儲(chǔ)在mysql中,其格式為:List<String> options = new ArrayList<>();String first = "What is your name?";options.add(first);String option = objectMapper.writeValueAsString(options);// option value is: ["what is your name?"]我的表結(jié)構(gòu):CREATE TABLE question( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, label VARCHAR(150) NULL, question_type VARCHAR(200) NULL, mandatory TINYINT DEFAULT 0, editable TINYINT DEFAULT 0, `option` JSON NULL);實(shí)體:@Entity@Table(name = "question")public class Question implements Serializable { /** * */ private static final long serialVersionUID = 2209760405736391727L; private int id; private String label; private QuestionType questionType; private boolean mandatory; private boolean editable; private String options; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "label") public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } @Column(name = "question_type") @Enumerated(EnumType.STRING) public QuestionType getQuestionType() { return questionType; } public void setQuestionType(QuestionType questionType) { this.questionType = questionType; } @Column(name = "mandatory") public boolean isMandatory() { return mandatory; } public void setMandatory(boolean mandatory) { this.mandatory = mandatory; } @Column(name = "editable") public boolean isEditable() { return editable; } public void setEditable(boolean editable) { this.editable = editable; } @Column(name = "option") public String getOptions() { return options; } public void setOptions(String options) { this.options = options; }}
1 回答

拉丁的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
問題只在于您的option
列名,因?yàn)樗莔ysql中的關(guān)鍵字。您在ddl語(yǔ)句中引用了它,但是當(dāng)hibernate生成查詢時(shí),默認(rèn)情況下不會(huì)引用名稱。您可以通過設(shè)置來(lái)更改此設(shè)置(這將引用所有數(shù)據(jù)庫(kù)標(biāo)識(shí)符):
hibernate.globally_quoted_identifiers=true
或者,您可以更改列映射:
@Column(name="\"option\"")
或者只是不要在數(shù)據(jù)庫(kù)模式中使用關(guān)鍵字作為名稱。
添加回答
舉報(bào)
0/150
提交
取消