Created
May 5, 2011 15:09
-
-
Save masayuki038/957224 to your computer and use it in GitHub Desktop.
morphia test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test.entity; | |
| import java.util.List; | |
| import com.google.code.morphia.annotations.Embedded; | |
| @Embedded | |
| public class Bar { | |
| private long id; | |
| private List<String> list; | |
| public long getId() { | |
| return id; | |
| } | |
| public void setId(long id) { | |
| this.id = id; | |
| } | |
| public List<String> getList() { | |
| return list; | |
| } | |
| public void setList(List<String> list) { | |
| this.list = list; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test.entity; | |
| import java.util.Date; | |
| import java.util.List; | |
| import org.bson.types.ObjectId; | |
| import com.google.code.morphia.annotations.Embedded; | |
| import com.google.code.morphia.annotations.Entity; | |
| import com.google.code.morphia.annotations.Id; | |
| @Entity | |
| public class Foo { | |
| @Id private ObjectId objectId; | |
| private int id; | |
| private String name; | |
| private Date created; | |
| @Embedded | |
| private List<Bar> bars; | |
| public ObjectId getObjectId() { | |
| return objectId; | |
| } | |
| public void setObjectId(ObjectId objectId) { | |
| this.objectId = objectId; | |
| } | |
| public int getId() { | |
| return id; | |
| } | |
| public void setId(int id) { | |
| this.id = id; | |
| } | |
| public String getName() { | |
| return name; | |
| } | |
| public void setName(String name) { | |
| this.name = name; | |
| } | |
| public Date getCreated() { | |
| return created; | |
| } | |
| public void setCreated(Date created) { | |
| this.created = created; | |
| } | |
| public List<Bar> getBars() { | |
| return bars; | |
| } | |
| public void setBars(List<Bar> bars) { | |
| this.bars = bars; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test; | |
| import java.net.UnknownHostException; | |
| import java.util.Arrays; | |
| import java.util.Date; | |
| import java.util.List; | |
| import org.junit.Test; | |
| import com.google.code.morphia.Datastore; | |
| import com.google.code.morphia.Morphia; | |
| import com.mongodb.Mongo; | |
| import com.mongodb.MongoException; | |
| import test.entity.Bar; | |
| import test.entity.Foo; | |
| public class MongoTest { | |
| @Test | |
| public void testSaveFoo() throws UnknownHostException, MongoException{ | |
| Morphia morphia = new Morphia(); | |
| morphia.map(Foo.class).map(Bar.class); | |
| Datastore ds = morphia.createDatastore(new Mongo(), "test"); | |
| ds.save(createFoo()); | |
| } | |
| protected Foo createFoo() { | |
| Foo foo = new Foo(); | |
| foo.setId(1); | |
| foo.setName("hoge"); | |
| foo.setCreated(new Date()); | |
| foo.setBars(createBarList()); | |
| return foo; | |
| } | |
| protected List<Bar> createBarList() { | |
| Bar bar1 = new Bar(); | |
| bar1.setId(2); | |
| bar1.setList(Arrays.asList("foo", "bar", "hoge")); | |
| Bar bar2 = new Bar(); | |
| bar2.setId(3); | |
| bar2.setList(Arrays.asList("mungbean", "test")); | |
| return Arrays.asList(bar1, bar2); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment