猿问

Stringtify对象无法以JSON格式存储在mysql中

我试图对字符串列表进行字符串化,并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;

    }

}


qq_笑_17
浏览 181回答 1
1回答

拉丁的传说

问题只在于您的option列名,因为它是mysql中的关键字。您在ddl语句中引用了它,但是当hibernate生成查询时,默认情况下不会引用名称。您可以通过设置来更改此设置(这将引用所有数据库标识符):hibernate.globally_quoted_identifiers=true或者,您可以更改列映射:@Column(name="\"option\"")或者只是不要在数据库模式中使用关键字作为名称。
随时随地看视频慕课网APP

相关分类

Java
我要回答