我试图对字符串列表进行字符串化,并JSON使用hibernate将其存储在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?"]我的表结构: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);实体:@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 回答
![?](http://img1.sycdn.imooc.com/5458478b0001f01502200220-100-100.jpg)
拉丁的传说
TA贡献1789条经验 获得超8个赞
问题只在于您的option
列名,因为它是mysql中的关键字。您在ddl语句中引用了它,但是当hibernate生成查询时,默认情况下不会引用名称。您可以通过设置来更改此设置(这将引用所有数据库标识符):
hibernate.globally_quoted_identifiers=true
或者,您可以更改列映射:
@Column(name="\"option\"")
或者只是不要在数据库模式中使用关键字作为名称。
添加回答
举报
0/150
提交
取消