mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#115 - Mapping - Add support for @ElementCollection enhancement
Initial support - add simple Set support and Json support for simple List and Set.
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package org.tests.model.elementcollection;
|
||||
|
||||
import javax.persistence.CollectionTable;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Version;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
public class EcsPerson {
|
||||
|
||||
@Id
|
||||
long id;
|
||||
|
||||
String name;
|
||||
|
||||
@ElementCollection
|
||||
@CollectionTable(name = "ecs_person_phone")
|
||||
@Column(name = "phone")
|
||||
Set<String> phoneNumbers = new LinkedHashSet<>();
|
||||
|
||||
@Version
|
||||
long version;
|
||||
|
||||
public EcsPerson(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "person id:" + id + " name:" + name + " phs:" + phoneNumbers;
|
||||
}
|
||||
|
||||
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<String> getPhoneNumbers() {
|
||||
return phoneNumbers;
|
||||
}
|
||||
|
||||
public void setPhoneNumbers(Set<String> phoneNumbers) {
|
||||
this.phoneNumbers = phoneNumbers;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public class TestElementCollectionBasic extends BaseTestCase {
|
||||
Ebean.find(EcPerson.class).where()
|
||||
.startsWith("name", "Fiona0")
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
.findList();
|
||||
|
||||
List<String> phoneNumbers0 = found.get(0).getPhoneNumbers();
|
||||
List<String> phoneNumbers1 = found.get(1).getPhoneNumbers();
|
||||
@@ -46,6 +46,12 @@ public class TestElementCollectionBasic extends BaseTestCase {
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
System.out.println(found2);
|
||||
assertThat(found2).hasSize(2);
|
||||
EcPerson foundFirst = found2.get(0);
|
||||
String asJson = Ebean.json().toJson(foundFirst);
|
||||
|
||||
EcPerson fromJson = Ebean.json().toBean(EcPerson.class, asJson);
|
||||
|
||||
assertThat(fromJson.getPhoneNumbers()).containsAll(foundFirst.getPhoneNumbers());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package org.tests.model.elementcollection;
|
||||
|
||||
import io.ebean.BaseTestCase;
|
||||
import io.ebean.Ebean;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class TestElementCollectionBasicSet extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
EcsPerson person = new EcsPerson("Fiona021");
|
||||
person.getPhoneNumbers().add("021 1234");
|
||||
person.getPhoneNumbers().add("021 4321");
|
||||
Ebean.save(person);
|
||||
|
||||
|
||||
EcsPerson person1 = new EcsPerson("Fiona09");
|
||||
person1.getPhoneNumbers().add("09 1234");
|
||||
person1.getPhoneNumbers().add("09 4321");
|
||||
person1.getPhoneNumbers().add("09 9876");
|
||||
Ebean.save(person1);
|
||||
|
||||
List<EcsPerson> found =
|
||||
Ebean.find(EcsPerson.class).where()
|
||||
.startsWith("name", "Fiona0")
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
Set<String> phoneNumbers0 = found.get(0).getPhoneNumbers();
|
||||
Set<String> phoneNumbers1 = found.get(1).getPhoneNumbers();
|
||||
phoneNumbers0.size();
|
||||
|
||||
assertThat(phoneNumbers0).containsExactly("021 1234", "021 4321");
|
||||
assertThat(phoneNumbers1).containsExactly("09 1234", "09 4321", "09 9876");
|
||||
|
||||
|
||||
List<EcsPerson> found2 =
|
||||
Ebean.find(EcsPerson.class)
|
||||
.fetch("phoneNumbers")
|
||||
.where()
|
||||
.startsWith("name", "Fiona0")
|
||||
.order().asc("id")
|
||||
.findList();
|
||||
|
||||
assertThat(found2).hasSize(2);
|
||||
EcsPerson foundFirst = found2.get(0);
|
||||
String asJson = Ebean.json().toJson(foundFirst);
|
||||
|
||||
EcsPerson fromJson = Ebean.json().toBean(EcsPerson.class, asJson);
|
||||
|
||||
assertThat(fromJson.getPhoneNumbers()).containsAll(foundFirst.getPhoneNumbers());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user