java - Morphia can't insert class into DB -


i want strore object mongodb morphia.

but got bunch of exceptions:

exception in thread "main" java.lang.illegalargumentexception: can't serialize class com.aerlingus.ta.models.b2b.faresearch.airsearchprefstype$cabinpref     @ org.bson.basicbsonencoder._putobjectfield(basicbsonencoder.java:270) 

here save():

public void create(model model) {     object keyvalue = get(model);     if(datastore.find(this.model).field(keyfield.id()).equal(keyvalue).get() == null){         datastore.save(model);     } else {         throw new runtimeexception(string.format("duplicate parameters '%s' : '%s'", keyfield.id(), keyvalue));     } } 

here airsearchprefstype class:

@embedded @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "") public static class cabinpref {      @embedded @compare     @xmlattribute(name = "preferlevel")     protected preferleveltype preferlevel;      @embedded @compare     @xmlattribute(name = "cabin")     protected cabintype cabin;      @transient     @xmlattribute(name = "cabinsubtype")     protected string cabinsubtype; 

and preferleveltype:

@embedded @xmltype(name = "preferleveltype") @xmlenum public enum preferleveltype {      @embedded     @xmlenumvalue("only")     only("only"),      @xmlenumvalue("unacceptable")     @embedded     unacceptable("unacceptable"),      @xmlenumvalue("preferred")     @embedded     preferred("preferred"),      @embedded     @xmlenumvalue("required")     required("required"),      @embedded     @xmlenumvalue("nopreference")     no_preference("nopreference");     private final string value; 

and cabintype:

@embedded @xmltype(name = "cabintype") @xmlenum public enum cabintype {      @xmlenumvalue("first")     first("first"),      @xmlenumvalue("business")     business("business"),      @xmlenumvalue("economy")     economy("economy");     private final string value; 

i couldn't understand wrong here. morphia wiorks static inner classes or enums.

how solve trouble?

the following code show exceptions yours:

package com.test.mongodb;  import java.io.ioexception; import java.io.serializable;  import org.junit.test;  import com.mongodb.basicdbobject; import com.mongodb.dbcollection; import com.mongodb.mongoclient;  public class testmongo {      static class temp implements serializable {          private static final long serialversionuid = 1l;         private string name;          public temp(string name) {             this.name = name;         }          public string getname() {             return name;         }     }      @test     public void test() {         try {             mongoclient mongoclient = new mongoclient();             dbcollection collection = mongoclient.getdb("test").getcollection("temp");              temp temp = new temp("current time: " + system.currenttimemillis());              collection.insert(new basicdbobject("javaobject", temp));         } catch (ioexception e) {             e.printstacktrace();         }     } } 

you can try way:

package com.test.mongodb;  import java.io.bytearrayinputstream; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.io.serializable;  import org.junit.test;  import com.mongodb.basicdbobject; import com.mongodb.dbcollection; import com.mongodb.dbcursor; import com.mongodb.dbobject; import com.mongodb.mongoclient;  public class testmongo {      static class temp implements serializable {          private static final long serialversionuid = 1l;         private string name;          public temp(string name) {             this.name = name;         }          public string getname() {             return name;         }     }      @test     public void test(){         try {             mongoclient mongoclient = new mongoclient();             dbcollection collection = mongoclient.getdb("test").getcollection("temp");              temp temp = new temp("currect time: " + system.currenttimemillis());              bytearrayoutputstream baos = new bytearrayoutputstream();             objectoutputstream oos = new objectoutputstream(baos);             oos.writeobject(temp);             collection.insert(new basicdbobject("javaobject", baos.tobytearray()));              readobject(collection);         } catch (ioexception e) {             e.printstacktrace();         }      }      /**      * read java object mongodb      * @param collection      */     public void readobject(dbcollection collection){         try {             dbcursor cursor = collection.find();             (dbobject dbobject : cursor) {                 bytearrayinputstream bais = new bytearrayinputstream((byte[]) dbobject.get("javaobject"));                 objectinputstream ois = new objectinputstream(bais);                 temp temp = (temp) ois.readobject();                 system.out.println(temp.getname());             }         } catch (exception e) {             e.printstacktrace();         }     } } 

this approach doesn't conform morphia exactly, mongo itself.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -