<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: TaharaKanta</title>
    <description>The latest articles on Forem by TaharaKanta (@taharakanta).</description>
    <link>https://forem.com/taharakanta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2896098%2Fee754fc2-32ab-4520-9127-9abf00df8b56.png</url>
      <title>Forem: TaharaKanta</title>
      <link>https://forem.com/taharakanta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/taharakanta"/>
    <language>en</language>
    <item>
      <title>Insert into a jsonb Column in Spring Boot 3 + JPA + PostgreSQL</title>
      <dc:creator>TaharaKanta</dc:creator>
      <pubDate>Mon, 24 Feb 2025 06:16:44 +0000</pubDate>
      <link>https://forem.com/taharakanta/insert-into-a-jsonb-column-in-spring-boot-3-jpa-postgresql-1o5f</link>
      <guid>https://forem.com/taharakanta/insert-into-a-jsonb-column-in-spring-boot-3-jpa-postgresql-1o5f</guid>
      <description>&lt;h1&gt;
  
  
  Prerequisites
&lt;/h1&gt;

&lt;p&gt;We will check the database tables for the given environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment
&lt;/h2&gt;

&lt;p&gt;Spring Boot ver 3.3.1&lt;br&gt;
gradle&lt;br&gt;
kotlin&lt;br&gt;
jdk 21&lt;br&gt;
psql (PostgreSQL) 14.16&lt;/p&gt;
&lt;h2&gt;
  
  
  Database
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;SAMPLE_TABLE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="nb"&gt;SERIAL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;json_data&lt;/span&gt; &lt;span class="n"&gt;JSONB&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Please ensure that Spring Boot is connected to the PostgreSQL server by inserting data into the database using tools such as &lt;code&gt;psql&lt;/code&gt;, and confirm it using JPA's &lt;code&gt;.findAll()&lt;/code&gt;, etc.&lt;/p&gt;
&lt;h1&gt;
  
  
  Sample Code
&lt;/h1&gt;
&lt;h2&gt;
  
  
  build.gradle.kts
&lt;/h2&gt;

&lt;p&gt;Add the necessary dependencies&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```kotlin name=build.gradle.kts&lt;br&gt;
dependencies {&lt;br&gt;
    // Required for setting the jsonb type&lt;br&gt;
    // Adjust the version of hypersistence-utils-hibernate as needed for your environment&lt;br&gt;
    implementation "io.hypersistence:hypersistence-utils-hibernate-60:3.5.2"&lt;br&gt;
}&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


## SampleEntity.kt



```kotlin name=SampleEntity.kt
import io.hypersistence.utils.hibernate.type.json.JsonType
import jakarta.persistence.*
import org.hibernate.annotations.Type

@Entity
@Table(name = "SAMPLE_TABLE")
data class SampleEntity(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    val id: Long?,

    // Declaring @Type will convert the String to Json
    @Type(JsonType::class)
    // Specify jsonb with columnDefinition
    @Column(name = "json_data", columnDefinition = "jsonb")
    val jsonData: String
){
    // Add a no-argument constructor
    constructor() : this(null,  "")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  SampleRepository.kt
&lt;/h2&gt;



&lt;p&gt;```kotlin name=SampleRepository.kt&lt;br&gt;
import com.sample.SampleEntity&lt;br&gt;
import org.springframework.data.jpa.repository.JpaRepository&lt;br&gt;
import org.springframework.stereotype.Repository&lt;/p&gt;

&lt;p&gt;@Repository&lt;br&gt;
interface SampleRepository : JpaRepository&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


## SampleService.kt



```kotlin name=SampleService.kt
            val entity = SampleEntity(
                // Convert json to string before passing
                jsonData = "{\"name\":\"dummyName\"}"
            )
            val savedEntity = sampleEntityRepository.save(entity)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>springboot</category>
      <category>postgres</category>
      <category>json</category>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
