Java+MongoDB+Morphiaã使ã£ã¦ã¿ãããã®1
Java+MongoDB+Morphiaã使ã£ã¦MongoDBãæä½ãããã¨æãã¾ãã
MongoDBã®æä½ã«ã¯ãå ¬å¼ã®Javaãã©ã¤ãã¼ã使ç¨ããäºãã§ãã¾ãã
å ¬å¼ãã©ã¤ãã¼ã§ã¯ãBasicDBObjectã¨ããMapå®è£ ã使ç¨ãã¦ããããPOJOãä¿åãããåå¾ã§ããæ¹ãã¡ãªããã大ããã
ããã§Morphiaã使ã£ã¦ã¿ãã
Morphiaã¯ããã®å½¹å²ãæããããã«ãJavaãã©ã¤ãã¼ã®ã©ããã¼ãæä¾ãã¦ãã¾ãã
æ©é使ã£ã¦ã¿ã¾ãã
Morphiaã®ãã¦ã³ãã¼ã
2011å¹´6æ21æ¥ã®ææ°çãmorphia-1.00-SNAPSHOT.jarãåå¾ãã¾ãã
ãã£ãããªã®ã§ãQuickStartãå©ç¨ãã¦ã¿ã¾ãã
QuickStart
test ãâdto ãããâAddress.java ãããâHotel.java ãâHotelTest.java
Hotel.java
package test.dto; import org.bson.types.ObjectId; import com.google.code.morphia.annotations.Entity; import com.google.code.morphia.annotations.Id; @Entity public class Hotel { @Id private ObjectId id; private String name; private int starts; private Address address; public ObjectId getId() { return id; } public void setId(ObjectId id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getStarts() { return starts; } public void setStarts(int starts) { this.starts = starts; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
Address.java
package test.dto; import com.google.code.morphia.annotations.Embedded; @Embedded public class Address { private String street; private String city; private String postCode; private String country; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }
ä¸è¨2ã¤ã®ã¯ã©ã¹ãæºåãã¾ãã
Morphiaã使ç¨ãã¦ãªãã¸ã§ã¯ããä¿åãã
ãã¼ã¿ãã¼ã¹åãhotel_database
ã³ã¬ã¯ã·ã§ã³åãHotel
ã¨ãã¦ãå®éã«ä¿åãã¦ã¿ã¾ãã
HotelTest.java
package test; import test.dto.Address; import test.dto.Hotel; import com.google.code.morphia.Datastore; import com.google.code.morphia.Morphia; import com.mongodb.Mongo; public class HotelTest { public static void main(String[] args) throws Exception{ //æºå Mongo mongo = new Mongo("127.0.0.1", 27018); Morphia morphia = new Morphia(); morphia.map(Hotel.class).map(Address.class); Datastore datastore = morphia.createDatastore(mongo, "hotel_database"); Hotel hotel = new Hotel(); hotel.setName("My Hotel"); hotel.setStarts(4); Address address = new Address(); address.setStreet("123 Some Street"); address.setCity("Some Sity"); address.setPostCode("123 4567"); address.setCountry("Some Country"); hotel.setAddress(address); datastore.save(hotel); } }
HotelTestãå®è¡ããã°å®äºã§ãã
ãã¼ã¿ãåå¾ãã¦ã¿ã¾ãã
> db.Hotel.findOne() { "_id" : ObjectId("4dfedf9950755d6dd2819102"), "className" : "test.dto.Hotel", "name" : "My Hotel", "starts" : 4, "address" : { "street" : "123 Some Street", "city" : "Some Sity", "postCode" : "123 4567", "country" : "Some Country" } }
ããã¥ã¡ã³ãã«å«ã¾ãã¦ããã¯ã©ã¹åãã³ã¬ã¯ã·ã§ã³åã®å¤æ´ã¯ã
@Entityã¢ããã¼ã·ã§ã³ã§è¨å®ã§ãã¾ãã
@Entityã¢ããã¼ã·ã§ã³
@Entityã¢ããã¼ã·ã§ã³ã¯ãã¯ã©ã¹ãã³ã¬ã¯ã·ã§ã³å ã«ããã¥ã¡ã³ãã¨ãã¦æ°¸ç¶åãããã¨ã宣è¨ãã¾ãã
ã¾ããã³ã¬ã¯ã·ã§ã³ã®ååã®å®ç¾©ãªã©ã®è¨å®ãè¡ãã¾ãã
Morphiaã¯ãã¯ã©ã¹åãã³ã¬ã¯ã·ã§ã³åã«è¨å®ãã¾ãã
Entity.java
package com.google.code.morphia.annotations; import java.lang.annotation.Annotation; import java.lang.annotation.Documented; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({java.lang.annotation.ElementType.TYPE}) public @interface Entity { public abstract String value(); public abstract CappedAt cap(); public abstract boolean noClassnameStored(); public abstract boolean slaveOk(); public abstract String concern(); }
ã³ã¬ã¯ã·ã§ã³åããhotelsãã«ããå ´å
@Entity("hotels") public class Hotel {
ããã¥ã¡ã³ãã«ã¯ã©ã¹åãå«ããªãå ´å
@Entity(noClassnameStored=true) public class Hotel {