mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Support Postgres JSONB/JSON with String - @DbJsonb String content;
This commit is contained in:
@@ -5,12 +5,12 @@ import io.ebean.test.containers.PostgresContainer;
|
||||
public class StartPostgres {
|
||||
|
||||
public static void main(String[] args) {
|
||||
PostgresContainer.builder("13")
|
||||
.port(5432)
|
||||
PostgresContainer.builder("14")
|
||||
.dbName("unit")
|
||||
.user("unit")
|
||||
.password("unit")
|
||||
.containerName("pg13x")
|
||||
//.port(6432)
|
||||
//.user("unit")
|
||||
//.password("test")
|
||||
//.containerName("ut_postgres")
|
||||
.extensions("hstore,pgcrypto")
|
||||
.build()
|
||||
.startWithDropCreate();
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.tests.json;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.test.LoggedSql;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.json.EBasicJsonBString;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class TestDbJsonBStringType {
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
var bean = new EBasicJsonBString("hi").content("{\"mykey\": 42}"); // JsonB format with space
|
||||
DB.save(bean);
|
||||
|
||||
var found = DB.find(EBasicJsonBString.class, bean.id());
|
||||
// Note that Postgres JsonB will format the result
|
||||
assertThat(found.content()).isEqualTo("{\"mykey\": 42}");
|
||||
|
||||
LoggedSql.start();
|
||||
|
||||
// change title only, expect content not in update
|
||||
found.title("changeTitleOnly");
|
||||
DB.save(found);
|
||||
List<String> sql = LoggedSql.collect();
|
||||
// update does NOT contain our json content
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_bstring set title=?, version=? where id=? and version=?");
|
||||
|
||||
// change title and content
|
||||
found.title("changeAgain");
|
||||
found.content("{\"mykey\": 92}");
|
||||
DB.save(found);
|
||||
sql = LoggedSql.collect();
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_bstring set title=?, content=?, version=? where id=? and version=?");
|
||||
|
||||
|
||||
// change content only
|
||||
found.content("{\"mykey\": 95}");
|
||||
DB.save(found);
|
||||
sql = LoggedSql.stop();
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_bstring set content=?, version=? where id=? and version=?");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.tests.json;
|
||||
|
||||
import io.ebean.DB;
|
||||
import io.ebean.test.LoggedSql;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.tests.model.json.EBasicJsonString;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class TestDbJsonStringType {
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
var bean = new EBasicJsonString("hi").content("{\"mykey\": 52}"); // JsonB format with space
|
||||
DB.save(bean);
|
||||
|
||||
var found = DB.find(EBasicJsonString.class, bean.id());
|
||||
// Note that Postgres JsonB will format the result
|
||||
assertThat(found.content()).isEqualTo("{\"mykey\": 52}");
|
||||
|
||||
LoggedSql.start();
|
||||
|
||||
// change title only, expect content not in update
|
||||
found.title("changeTitleOnly");
|
||||
DB.save(found);
|
||||
List<String> sql = LoggedSql.collect();
|
||||
// update does NOT contain our json content
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_string set title=?, version=? where id=? and version=?");
|
||||
|
||||
// change title and content
|
||||
found.title("changeAgain");
|
||||
found.content("{\"mykey\": 92}");
|
||||
DB.save(found);
|
||||
sql = LoggedSql.collect();
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_string set title=?, content=?, version=? where id=? and version=?");
|
||||
|
||||
|
||||
// change content only
|
||||
found.content("{\"mykey\": 95}");
|
||||
DB.save(found);
|
||||
sql = LoggedSql.stop();
|
||||
assertThat(sql.get(0)).contains("update ebasic_json_string set content=?, version=? where id=? and version=?");
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.tests.model.json;
|
||||
|
||||
import io.ebean.annotation.DbJsonB;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class EBasicJsonBString {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String title;
|
||||
|
||||
@DbJsonB
|
||||
String content;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public EBasicJsonBString(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public long id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public EBasicJsonBString id(long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String title() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public EBasicJsonBString title(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String content() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public EBasicJsonBString content(String content) {
|
||||
this.content = content;
|
||||
return this;
|
||||
}
|
||||
|
||||
public long version() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public EBasicJsonBString version(long version) {
|
||||
this.version = version;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.tests.model.json;
|
||||
|
||||
import io.ebean.annotation.DbJson;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class EBasicJsonString {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String title;
|
||||
|
||||
@DbJson
|
||||
String content;
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public EBasicJsonString(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public long id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public EBasicJsonString id(long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String title() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public EBasicJsonString title(String title) {
|
||||
this.title = title;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String content() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public EBasicJsonString content(String content) {
|
||||
this.content = content;
|
||||
return this;
|
||||
}
|
||||
|
||||
public long version() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public EBasicJsonString version(long version) {
|
||||
this.version = version;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user