mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#696 - ENH: Add fallback for @DbHstore ... fallback to JSON storage in VARCHAR
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package com.avaje.tests.model.json;
|
||||
|
||||
import com.avaje.ebean.annotation.DbHstore;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
public class EBasicHstore {
|
||||
|
||||
@Id
|
||||
Long id;
|
||||
|
||||
String name;
|
||||
|
||||
// fallback to varchar(800) for non-Postgres
|
||||
@DbHstore(length = 800)
|
||||
Map<String,String> map;
|
||||
|
||||
@Version
|
||||
Long version;
|
||||
|
||||
public EBasicHstore(String name) {
|
||||
this.name = name;
|
||||
this.map = new LinkedHashMap<String, String>();
|
||||
}
|
||||
|
||||
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 Map<String, String> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setMap(Map<String, String> map) {
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.avaje.tests.types;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.json.EBasicHstore;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.avaje.ebeantest.LoggedSqlCollector;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestHstore extends BaseTestCase {
|
||||
|
||||
private EBasicHstore bean;
|
||||
|
||||
@Test
|
||||
public void insert() {
|
||||
|
||||
if (isPostgres()) {
|
||||
// run this manually for Postgres with the HSTORE extension installed
|
||||
// psql mydb -c 'create extension hstore;'
|
||||
return;
|
||||
}
|
||||
|
||||
bean = new EBasicHstore("one");
|
||||
bean.getMap().put("home", "123");
|
||||
bean.getMap().put("work", "987");
|
||||
|
||||
Ebean.save(bean);
|
||||
|
||||
json_parse_format();
|
||||
update_when_notDirty();
|
||||
update_when_dirty();
|
||||
insert_when_null();
|
||||
}
|
||||
|
||||
void json_parse_format() {
|
||||
|
||||
String asJson = Ebean.json().toJson(bean);
|
||||
assertThat(asJson).contains("\"map\":{\"home\":\"123\",\"work\":\"987\"}");
|
||||
|
||||
EBasicHstore fromJson = Ebean.json().toBean(EBasicHstore.class, asJson);
|
||||
assertEquals(bean.getId(), fromJson.getId());
|
||||
assertEquals(bean.getName(), fromJson.getName());
|
||||
Assertions.assertThat(fromJson.getMap().keySet()).containsExactly("home", "work");
|
||||
}
|
||||
|
||||
void update_when_notDirty() {
|
||||
|
||||
EBasicHstore found = Ebean.find(EBasicHstore.class, bean.getId());
|
||||
found.setName("modName");
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
Ebean.save(found);
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
// we don't update the map as it is not dirty
|
||||
assertThat(sql.get(0)).contains("update ebasic_hstore set name=?, version=? where");
|
||||
}
|
||||
|
||||
void update_when_dirty() {
|
||||
|
||||
EBasicHstore found = Ebean.find(EBasicHstore.class, bean.getId());
|
||||
found.setName("modNamePlus");
|
||||
found.getMap().put("foo","9987");
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
Ebean.save(found);
|
||||
List<String> sql = LoggedSqlCollector.stop();
|
||||
|
||||
assertThat(sql.get(0)).contains("update ebasic_hstore set name=?, map=?, version=? where id=? and version=?");
|
||||
}
|
||||
|
||||
void insert_when_null() {
|
||||
|
||||
EBasicHstore bean = new EBasicHstore("one");
|
||||
bean.setMap(null);
|
||||
Ebean.save(bean);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user