mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#299 - ENH: Add support for mapping Jackson JsonNode as JSON content to DB including Postgres JSON and JSON types
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package com.avaje.tests.json;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.json.EBasicJsonNode;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TestJsonNodeBasic extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testInsertUpdateDelete() throws IOException {
|
||||
|
||||
String s0 = "{\"docId\":18,\"contentId\":\"asd\",\"active\":true,\"contentType\":\"pg-hello\",\"content\":{\"name\":\"rob\",\"age\":45}}";
|
||||
String s1 = "{\"docId\":99,\"contentId\":\"asd\",\"active\":true,\"contentType\":\"pg-hello\",\"content\":{\"name\":\"rob\",\"age\":45}}";
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
JsonNode content = objectMapper.readTree(s0);
|
||||
|
||||
EBasicJsonNode bean = new EBasicJsonNode();
|
||||
bean.setName("one");
|
||||
bean.setContent(content);
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
EBasicJsonNode bean1 = Ebean.find(EBasicJsonNode.class, bean.getId());
|
||||
|
||||
assertEquals(bean.getId(), bean1.getId());
|
||||
assertEquals(bean.getName(), bean1.getName());
|
||||
|
||||
assertEquals(bean.getContent().path("contentType").asText(), bean1.getContent().path("contentType").asText());
|
||||
assertEquals(18L, bean1.getContent().get("docId").asLong());
|
||||
|
||||
bean1.setName("just change name");
|
||||
Ebean.save(bean1);
|
||||
|
||||
JsonNode content1 = objectMapper.readTree(s1);
|
||||
|
||||
bean1.setName("two");
|
||||
bean1.setContent(content1);
|
||||
Ebean.save(bean1);
|
||||
|
||||
EBasicJsonNode bean2 = Ebean.find(EBasicJsonNode.class, bean.getId());
|
||||
|
||||
// name changed and docId changed
|
||||
assertEquals("two", bean2.getName());
|
||||
assertEquals(99L, bean2.getContent().path("docId").asLong());
|
||||
|
||||
// content persisted even though it has not changed (as it's loaded)
|
||||
// we can't do dirty detection on JsonNode (like we do on Map<String,Object>)
|
||||
bean1.setName("three");
|
||||
Ebean.save(bean1);
|
||||
|
||||
EBasicJsonNode bean3 = Ebean.find(EBasicJsonNode.class, bean.getId());
|
||||
|
||||
assertEquals("three", bean3.getName());
|
||||
assertEquals(99L, bean3.getContent().path("docId").asLong());
|
||||
|
||||
// write whole bean with content as JSON
|
||||
String fullBeanJson = Ebean.json().toJson(bean3);
|
||||
|
||||
// parse JSON into whole bean with content
|
||||
EBasicJsonNode beanJsonRestored = Ebean.json().toBean(EBasicJsonNode.class, fullBeanJson);
|
||||
|
||||
assertEquals(99L,beanJsonRestored.getContent().path("docId").asLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazyLoading() throws IOException {
|
||||
|
||||
String s0 = "{\"docId\":19,\"contentId\":\"asd\",\"content\":{\"name\":\"rob\",\"age\":45}}";
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
JsonNode content = objectMapper.readTree(s0);
|
||||
|
||||
EBasicJsonNode bean = new EBasicJsonNode();
|
||||
bean.setName("lazyLoadTest");
|
||||
bean.setContent(content);
|
||||
Ebean.save(bean);
|
||||
|
||||
EBasicJsonNode bean1 = Ebean.find(EBasicJsonNode.class)
|
||||
.select("name")
|
||||
.setId(bean.getId())
|
||||
.findUnique();
|
||||
|
||||
Set<String> loadedProps = Ebean.getBeanState(bean1).getLoadedProps();
|
||||
assertTrue(loadedProps.contains("name"));
|
||||
assertFalse(loadedProps.contains("content"));
|
||||
|
||||
JsonNode lazyLoadedContent = bean1.getContent();
|
||||
assertNotNull(lazyLoadedContent);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.avaje.tests.json;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.json.EBasicJsonNodeBlob;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TestJsonNodeBlob extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testInsertUpdateDelete() throws IOException {
|
||||
|
||||
String s0 = "{\"docId\":18,\"contentId\":\"asd\",\"active\":true,\"contentType\":\"pg-hello\",\"content\":{\"name\":\"rob\",\"age\":45}}";
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
JsonNode content = objectMapper.readTree(s0);
|
||||
|
||||
EBasicJsonNodeBlob bean = new EBasicJsonNodeBlob();
|
||||
bean.setName("one");
|
||||
bean.setContent(content);
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
EBasicJsonNodeBlob bean1 = Ebean.find(EBasicJsonNodeBlob.class, bean.getId());
|
||||
|
||||
assertEquals(bean.getId(), bean1.getId());
|
||||
assertEquals(bean.getName(), bean1.getName());
|
||||
|
||||
assertEquals(bean.getContent().path("contentType").asText(), bean1.getContent().path("contentType").asText());
|
||||
assertEquals(18L, bean1.getContent().get("docId").asLong());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazyLoading() throws IOException {
|
||||
|
||||
String s0 = "{\"docId\":19,\"contentId\":\"asd\",\"content\":{\"name\":\"rob\",\"age\":45}}";
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
JsonNode content = objectMapper.readTree(s0);
|
||||
|
||||
EBasicJsonNodeBlob bean = new EBasicJsonNodeBlob();
|
||||
bean.setName("lazyLoadTest");
|
||||
bean.setContent(content);
|
||||
Ebean.save(bean);
|
||||
|
||||
EBasicJsonNodeBlob bean1 = Ebean.find(EBasicJsonNodeBlob.class)
|
||||
.select("name")
|
||||
.setId(bean.getId())
|
||||
.findUnique();
|
||||
|
||||
Set<String> loadedProps = Ebean.getBeanState(bean1).getLoadedProps();
|
||||
assertTrue(loadedProps.contains("name"));
|
||||
assertFalse(loadedProps.contains("content"));
|
||||
|
||||
JsonNode lazyLoadedContent = bean1.getContent();
|
||||
assertNotNull(lazyLoadedContent);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.avaje.tests.json;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.json.EBasicJsonNodeJsonB;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TestJsonNodeJsonb extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testInsertUpdateDelete() throws IOException {
|
||||
|
||||
String s0 = "{\"docId\":18,\"contentId\":\"asd\",\"active\":true,\"contentType\":\"pg-hello\",\"content\":{\"name\":\"rob\",\"age\":45}}";
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
JsonNode content = objectMapper.readTree(s0);
|
||||
|
||||
EBasicJsonNodeJsonB bean = new EBasicJsonNodeJsonB();
|
||||
bean.setName("one");
|
||||
bean.setContent(content);
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
EBasicJsonNodeJsonB bean1 = Ebean.find(EBasicJsonNodeJsonB.class, bean.getId());
|
||||
|
||||
assertEquals(bean.getId(), bean1.getId());
|
||||
assertEquals(bean.getName(), bean1.getName());
|
||||
|
||||
assertEquals(bean.getContent().path("contentType").asText(), bean1.getContent().path("contentType").asText());
|
||||
assertEquals(18L, bean1.getContent().get("docId").asLong());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazyLoading() throws IOException {
|
||||
|
||||
String s0 = "{\"docId\":19,\"contentId\":\"asd\",\"content\":{\"name\":\"rob\",\"age\":45}}";
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
JsonNode content = objectMapper.readTree(s0);
|
||||
|
||||
EBasicJsonNodeJsonB bean = new EBasicJsonNodeJsonB();
|
||||
bean.setName("lazyLoadTest");
|
||||
bean.setContent(content);
|
||||
Ebean.save(bean);
|
||||
|
||||
EBasicJsonNodeJsonB bean1 = Ebean.find(EBasicJsonNodeJsonB.class)
|
||||
.select("name")
|
||||
.setId(bean.getId())
|
||||
.findUnique();
|
||||
|
||||
Set<String> loadedProps = Ebean.getBeanState(bean1).getLoadedProps();
|
||||
assertTrue(loadedProps.contains("name"));
|
||||
assertFalse(loadedProps.contains("content"));
|
||||
|
||||
JsonNode lazyLoadedContent = bean1.getContent();
|
||||
assertNotNull(lazyLoadedContent);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.avaje.tests.json;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.json.EBasicJsonNodeVarchar;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TestJsonNodeVarchar extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void testInsertUpdateDelete() throws IOException {
|
||||
|
||||
String s0 = "{\"docId\":18,\"contentId\":\"asd\",\"active\":true,\"contentType\":\"pg-hello\",\"content\":{\"name\":\"rob\",\"age\":45}}";
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
JsonNode content = objectMapper.readTree(s0);
|
||||
|
||||
EBasicJsonNodeVarchar bean = new EBasicJsonNodeVarchar();
|
||||
bean.setName("one");
|
||||
bean.setContent(content);
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
EBasicJsonNodeVarchar bean1 = Ebean.find(EBasicJsonNodeVarchar.class, bean.getId());
|
||||
|
||||
assertEquals(bean.getId(), bean1.getId());
|
||||
assertEquals(bean.getName(), bean1.getName());
|
||||
|
||||
assertEquals(bean.getContent().path("contentType").asText(), bean1.getContent().path("contentType").asText());
|
||||
assertEquals(18L, bean1.getContent().get("docId").asLong());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazyLoading() throws IOException {
|
||||
|
||||
String s0 = "{\"docId\":19,\"contentId\":\"asd\",\"content\":{\"name\":\"rob\",\"age\":45}}";
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
JsonNode content = objectMapper.readTree(s0);
|
||||
|
||||
EBasicJsonNodeVarchar bean = new EBasicJsonNodeVarchar();
|
||||
bean.setName("lazyLoadTest");
|
||||
bean.setContent(content);
|
||||
Ebean.save(bean);
|
||||
|
||||
EBasicJsonNodeVarchar bean1 = Ebean.find(EBasicJsonNodeVarchar.class)
|
||||
.select("name")
|
||||
.setId(bean.getId())
|
||||
.findUnique();
|
||||
|
||||
Set<String> loadedProps = Ebean.getBeanState(bean1).getLoadedProps();
|
||||
assertTrue(loadedProps.contains("name"));
|
||||
assertFalse(loadedProps.contains("content"));
|
||||
|
||||
JsonNode lazyLoadedContent = bean1.getContent();
|
||||
assertNotNull(lazyLoadedContent);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.avaje.tests.model.json;
|
||||
|
||||
import com.avaje.ebean.annotation.DbJson;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
public class EBasicJsonNode {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
String name;
|
||||
|
||||
@DbJson
|
||||
JsonNode content;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public JsonNode getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(JsonNode content) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.avaje.tests.model.json;
|
||||
|
||||
import com.avaje.ebean.annotation.DbJson;
|
||||
import com.avaje.ebean.annotation.DbJsonType;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class EBasicJsonNodeBlob {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
String name;
|
||||
|
||||
@DbJson(storage = DbJsonType.BLOB)
|
||||
JsonNode content;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public JsonNode getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(JsonNode content) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.avaje.tests.model.json;
|
||||
|
||||
import com.avaje.ebean.annotation.DbJson;
|
||||
import com.avaje.ebean.annotation.DbJsonB;
|
||||
import com.avaje.ebean.annotation.DbJsonType;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class EBasicJsonNodeJsonB {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
String name;
|
||||
|
||||
//@DbJson(storage = DbJsonType.JSONB)
|
||||
@DbJsonB
|
||||
JsonNode content;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public JsonNode getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(JsonNode content) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.avaje.tests.model.json;
|
||||
|
||||
import com.avaje.ebean.annotation.DbJson;
|
||||
import com.avaje.ebean.annotation.DbJsonType;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
@Entity
|
||||
public class EBasicJsonNodeVarchar {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
String name;
|
||||
|
||||
@DbJson(storage = DbJsonType.VARCHAR, length = 1000)
|
||||
JsonNode content;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public JsonNode getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(JsonNode content) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user