简体   繁体   English

Morphia无法将类插入数据库

[英]Morphia can't insert class into DB

I want to strore Object to MongoDB with Morphia. 我想和Morphia一起使用对象到MongoDB。

But I got bunch of exceptions: 但是我有很多例外:

Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class com.aerlingus.ta.models.b2b.faresearch.AirSearchPrefsType$CabinPref
    at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:270)

Here is this save() : 这是这个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 is AirSearchPrefsType class : 这是AirSearchPrefsType类

@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 : 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 : 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 what is wrong here. 我不明白这里出了什么问题。 Does Morphia wiorks with static inner classes or with enums. Morphia是否与静态内部类或枚举一起工作?

How to solve this trouble? 如何解决这个麻烦?

The following code will show exceptions like 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 this 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 the java object from mongodb
     * @param collection
     */
    public void readObject(DBCollection collection){
        try {
            DBCursor cursor = collection.find();
            for (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 to Morphia exactly, but to Mongo itself. 这种方法并不完全符合Morphia,而是Mongo本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM