0

I want to create a table with a column called data which will store json data.How do i add that column in my xml file and also how do i make it in my Model class.Currently i am going like this but i have no idea if it is correct.

 <changeSet author="ABC" id="HUI">
        <createTable tableName="archived_table">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints primaryKey="true"
                             primaryKeyName="archive_tablePK" />
            </column>
            <column name="created" type="TIMESTAMP(6)">
            </column>
            <column name="data" type="jsonb"/>
        </createTable>
    </changeSet>

As for my Model class i am going like this

@Entity
@Table(name = "archived_table")
public class ArchiveModel extends  BaseModel{
    @Column(nullable = false)
    private JSONObject data;

public ArchiveDataModel(
            JSONObject data,
    ){
        super();
        this.data=data;
    }

 public JSONObject getData() {
        return data;
    }

    public void setData(JSONObject data) {
        this.data = data;
    }
}
Muhammad
  • 59
  • 9
  • I'm not keen on the idea of databases having field types specific to a format, but a json data type does exist in Postgres. Here is another question with several answers. https://stackoverflow.com/q/49834222/5611377 – ssimm Jun 26 '23 at 08:39

1 Answers1

2

The type of your column should be String and you should write the value using ObjectMapper.writeValueAsString(), and read it using ObjectMapper.readValue. Then it should be clear how to handle it with liquibase.

Aid Hadzic
  • 578
  • 3
  • 10
  • what i want is to create a table which will have a column of json data type.I am not going to use that table for any data fetching.I just simply want a table with json Data type column in which i can manually enter json data by sql query. – Muhammad Nov 04 '22 at 09:14
  • 1
    I agree in principal. Store it as a string and let a higher level of code deal with JSON encoding/decoding. But... some databases now have a "JSON" type. See above comment for a link to people who have answers. – ssimm Jun 26 '23 at 08:41