Java Stringtify对象无法在mysql中作为JSON存储
我试图使用hibernate将字符串列表字符串化并以Java Stringtify对象无法在mysql中作为JSON存储,java,mysql,json,hibernate,Java,Mysql,Json,Hibernate,我试图使用hibernate将字符串列表字符串化并以JSON格式存储在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 na
JSON
格式存储在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?"]
实体:
@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;
}
}
但它无法持久保存到mysql中:
错误:无法执行语句
详细错误:原因:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,了解使用“选项,问题类型”值(0,“标签1”,1,[“您的名字是什么?”],“SINGL”在第1行的正确语法
在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
我试图在Internet上搜索,但没有得到任何提示。问题在于您的
选项的名称,因为它是mysql中的一个关键字。您在ddl语句中引用了它,但当hibernate生成查询时,它不会引用默认名称。
您可以通过设置更改此设置(这将引用所有db标识符):
或者,您可以更改列映射:
@Column(name="\"option\"")
或者不要在数据库模式中使用关键字作为名称。请共享完整的代码,如实体代码和您试图使用hibernate保存对象的代码。啊!没有意识到这个简单的错误,它可以工作,谢谢您的帮助。
hibernate.globally_quoted_identifiers=true
@Column(name="\"option\"")