mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Merge branch 'master' of github.com:ebean-orm/ebean
This commit is contained in:
@@ -54,7 +54,7 @@ public class TestTransient extends BaseTestCase {
|
||||
selected = c.getSelected();
|
||||
Assert.assertNotNull(selected);
|
||||
|
||||
String updateStmt = "update customer set name = 'Rob' where id = :id";
|
||||
String updateStmt = "update customer set name = 'testTrans2' where id = :id";
|
||||
int rows = Ebean.createUpdate(Customer.class, updateStmt).set("id", custId).execute();
|
||||
|
||||
Assert.assertTrue("changed name back", 1 == rows);
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package org.tests.json;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.tests.model.json.EBasicJsonJackson;
|
||||
import org.tests.model.json.EBasicJsonJackson2;
|
||||
import org.tests.model.json.LongJacksonType;
|
||||
import org.tests.model.json.StringJacksonType;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
|
||||
public class TestDbJson_Jackson extends BaseTestCase {
|
||||
|
||||
|
||||
/**
|
||||
* This testcase verifies if the @JsonDeserialize Annotation will work properly in ebean.
|
||||
*/
|
||||
@Test
|
||||
public void testJsonDeserializeAnnotation() throws IOException {
|
||||
|
||||
EBasicJsonJackson bean = new EBasicJsonJackson();
|
||||
|
||||
bean.setName("stuff");
|
||||
|
||||
bean.setPlainValue("plain");
|
||||
|
||||
bean.getValueSet().add("A");
|
||||
bean.getValueSet().add("B");
|
||||
|
||||
bean.getValueList().add("A");
|
||||
bean.getValueList().add("B");
|
||||
|
||||
bean.getValueMap().put(1, "one");
|
||||
bean.getValueMap().put(2, "two");
|
||||
|
||||
ObjectMapper mapper = (ObjectMapper) Ebean.getDefaultServer().getPluginApi().getServerConfig().getObjectMapper();
|
||||
|
||||
String json = mapper.writeValueAsString(bean);
|
||||
EBasicJsonJackson found = mapper.readValue(json, EBasicJsonJackson.class);
|
||||
|
||||
// here we assert, that we can serialize/deserialize that bean with jackson
|
||||
assertThat(found.getPlainValue()).isEqualTo("plain");
|
||||
assertThat(found.getValueList()).containsExactly("A", "B");
|
||||
assertThat(found.getValueSet()).containsExactly("A", "B");
|
||||
assertThat(found.getValueMap()).containsEntry(1L, "one").containsEntry(2L, "two");
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
// here we do the same and expect, that we can deserialize the bean also
|
||||
found = Ebean.find(EBasicJsonJackson.class, bean.getId());
|
||||
|
||||
assertThat(found.getPlainValue()).isEqualTo("plain");
|
||||
assertThat(found.getValueList()).containsExactly("A", "B");
|
||||
assertThat(found.getValueSet()).containsExactly("A", "B");
|
||||
assertThat(found.getValueMap()).containsEntry(1L, "one").containsEntry(2L, "two");
|
||||
}
|
||||
|
||||
/**
|
||||
* This testcase verifies if polymorph objects will work in ebean.
|
||||
*
|
||||
* for BasicJacksonType there exists two types and has a @JsonTypeInfo
|
||||
* annotation. It is expected that this information is also honored by ebean.
|
||||
*/
|
||||
@Test
|
||||
public void testPolymorph() throws IOException {
|
||||
|
||||
EBasicJsonJackson2 bean = new EBasicJsonJackson2();
|
||||
|
||||
bean.setName("stuff");
|
||||
|
||||
bean.setPlainValue(new LongJacksonType(42L));
|
||||
|
||||
bean.getValueSet().add(new StringJacksonType("A"));
|
||||
bean.getValueSet().add(new LongJacksonType(7l));
|
||||
|
||||
bean.getValueList().add(new StringJacksonType("A"));
|
||||
bean.getValueList().add(new LongJacksonType(7l));
|
||||
|
||||
bean.getValueMap().put(1, new StringJacksonType("A"));
|
||||
bean.getValueMap().put(2, new LongJacksonType(7l));
|
||||
|
||||
ObjectMapper mapper = (ObjectMapper) Ebean.getDefaultServer().getPluginApi().getServerConfig().getObjectMapper();
|
||||
|
||||
String json = mapper.writeValueAsString(bean);
|
||||
assertThat(json)
|
||||
.contains("\"type\":\"string\"")
|
||||
.contains("\"type\":\"long\"");
|
||||
EBasicJsonJackson2 found = mapper.readValue(json, EBasicJsonJackson2.class);
|
||||
|
||||
assertThat(found.getPlainValue()).isInstanceOf(LongJacksonType.class);
|
||||
assertThat(found.getValueList()).hasSize(2);
|
||||
assertThat(found.getValueSet()).hasSize(2);
|
||||
assertThat(found.getValueMap()).hasSize(2);;
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
found = Ebean.find(EBasicJsonJackson2.class, bean.getId());
|
||||
|
||||
assertThat(found.getPlainValue()).isInstanceOf(LongJacksonType.class);
|
||||
assertThat(found.getValueList()).hasSize(2);
|
||||
assertThat(found.getValueSet()).hasSize(2);
|
||||
assertThat(found.getValueMap()).hasSize(2);;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package org.tests.json;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.tests.model.json.EBasicJsonJackson2;
|
||||
import org.tests.model.json.LongJacksonType;
|
||||
import org.tests.model.json.StringJacksonType;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
|
||||
public class TestDbJson_Jackson2 extends BaseTestCase {
|
||||
|
||||
private EBasicJsonJackson2 bean = new EBasicJsonJackson2();
|
||||
|
||||
private EBasicJsonJackson2 found;
|
||||
|
||||
@Test
|
||||
public void insert() throws IOException {
|
||||
|
||||
bean.setName("stuff");
|
||||
|
||||
bean.setPlainValue(new LongJacksonType(42L));
|
||||
|
||||
bean.getValueSet().add(new StringJacksonType("A"));
|
||||
bean.getValueSet().add(new LongJacksonType(7l));
|
||||
|
||||
bean.getValueList().add(new StringJacksonType("A"));
|
||||
bean.getValueList().add(new LongJacksonType(7l));
|
||||
|
||||
bean.getValueMap().put(1, new StringJacksonType("A"));
|
||||
bean.getValueMap().put(2, new LongJacksonType(7l));
|
||||
|
||||
ObjectMapper mapper = (ObjectMapper) Ebean.getDefaultServer().getPluginApi().getServerConfig().getObjectMapper();
|
||||
|
||||
String json = mapper.writeValueAsString(bean);
|
||||
found = mapper.readValue(json, EBasicJsonJackson2.class);
|
||||
|
||||
assertThat(found.getPlainValue()).isInstanceOf(LongJacksonType.class);
|
||||
assertThat(found.getValueList()).hasSize(2);
|
||||
assertThat(found.getValueSet()).hasSize(2);
|
||||
assertThat(found.getValueMap()).hasSize(2);;
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
found = Ebean.find(EBasicJsonJackson2.class, bean.getId());
|
||||
|
||||
assertThat(found.getPlainValue()).isInstanceOf(LongJacksonType.class);
|
||||
assertThat(found.getValueList()).hasSize(2);
|
||||
assertThat(found.getValueSet()).hasSize(2);
|
||||
assertThat(found.getValueMap()).hasSize(2);;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.tests.model.json;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
|
||||
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "type")
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = LongJacksonType.class, name = "long"),
|
||||
@JsonSubTypes.Type(value = StringJacksonType.class, name = "string")
|
||||
})
|
||||
public interface BasicJacksonType<T> {
|
||||
T getValue();
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package org.tests.model.json;
|
||||
|
||||
import io.ebean.annotation.DbJson;
|
||||
import io.ebean.annotation.DbJsonB;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
public class EBasicJsonJackson {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
String name;
|
||||
|
||||
@DbJson(length = 700)
|
||||
@JsonDeserialize(contentAs = String.class)
|
||||
Set<Object> valueSet = new LinkedHashSet<>();
|
||||
|
||||
@DbJsonB
|
||||
@JsonDeserialize(contentAs = String.class)
|
||||
List<Object> valueList = new ArrayList<>();
|
||||
|
||||
@DbJson(length = 700)
|
||||
@JsonDeserialize(keyAs = Long.class, contentAs = String.class)
|
||||
Map<Object, Object> valueMap = new LinkedHashMap<>();
|
||||
|
||||
@DbJson(length = 500)
|
||||
@JsonDeserialize(as = String.class)
|
||||
Object plainValue;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<Object> getValueSet() {
|
||||
return valueSet;
|
||||
}
|
||||
|
||||
public void setValueSet(Set<Object> valueSet) {
|
||||
this.valueSet = valueSet;
|
||||
}
|
||||
|
||||
public List<Object> getValueList() {
|
||||
return valueList;
|
||||
}
|
||||
|
||||
public void setValueList(List<Object> valueList) {
|
||||
this.valueList = valueList;
|
||||
}
|
||||
|
||||
public Object getPlainValue() {
|
||||
return plainValue;
|
||||
}
|
||||
|
||||
public void setPlainValue(Object plainValue) {
|
||||
this.plainValue = plainValue;
|
||||
}
|
||||
|
||||
public Map<Object, Object> getValueMap() {
|
||||
return valueMap;
|
||||
}
|
||||
|
||||
public void setValueMap(Map<Object, Object> valueMap) {
|
||||
this.valueMap = valueMap;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package org.tests.model.json;
|
||||
|
||||
import io.ebean.annotation.DbJson;
|
||||
import io.ebean.annotation.DbJsonB;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
public class EBasicJsonJackson2 {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
String name;
|
||||
|
||||
@DbJson(length = 700)
|
||||
Set<BasicJacksonType<?>> valueSet = new LinkedHashSet<>();
|
||||
|
||||
@DbJsonB
|
||||
List<BasicJacksonType<?>> valueList = new ArrayList<>();
|
||||
|
||||
@DbJson(length = 700)
|
||||
@JsonDeserialize(keyAs = Long.class)
|
||||
Map<Number, BasicJacksonType<?>> valueMap = new LinkedHashMap<>();
|
||||
|
||||
@DbJson(length = 500)
|
||||
BasicJacksonType<?> plainValue;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Set<BasicJacksonType<?>> getValueSet() {
|
||||
return valueSet;
|
||||
}
|
||||
|
||||
public void setValueSet(Set<BasicJacksonType<?>> valueSet) {
|
||||
this.valueSet = valueSet;
|
||||
}
|
||||
|
||||
public List<BasicJacksonType<?>> getValueList() {
|
||||
return valueList;
|
||||
}
|
||||
|
||||
public void setValueList(List<BasicJacksonType<?>> valueList) {
|
||||
this.valueList = valueList;
|
||||
}
|
||||
|
||||
public BasicJacksonType<?> getPlainValue() {
|
||||
return plainValue;
|
||||
}
|
||||
|
||||
public void setPlainValue(BasicJacksonType<?> plainValue) {
|
||||
this.plainValue = plainValue;
|
||||
}
|
||||
|
||||
public Map<Number, BasicJacksonType<?>> getValueMap() {
|
||||
return valueMap;
|
||||
}
|
||||
|
||||
public void setValueMap(Map<Number, BasicJacksonType<?>> valueMap) {
|
||||
this.valueMap = valueMap;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.tests.model.json;
|
||||
|
||||
public class LongJacksonType implements BasicJacksonType<Long> {
|
||||
|
||||
private Long value;
|
||||
|
||||
public LongJacksonType() {}
|
||||
|
||||
public LongJacksonType(Long value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Long value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.tests.model.json;
|
||||
|
||||
public class StringJacksonType implements BasicJacksonType<String> {
|
||||
|
||||
private String value;
|
||||
|
||||
public StringJacksonType() {}
|
||||
|
||||
public StringJacksonType(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user