gpt4 book ai didi

java - Morphia 无法将类插入数据库

转载 作者:行者123 更新时间:2023-12-01 12:30:14 25 4
gpt4 key购买 nike

我想使用 Morphia 将对象存储到 MongoDB。

但是我遇到了很多异常(exception):

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)

这是这个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));
}
}

这是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;

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;

客舱类型:

@Embedded
@XmlType(name = "CabinType")
@XmlEnum
public enum CabinType {

@XmlEnumValue("First")
FIRST("First"),

@XmlEnumValue("Business")
BUSINESS("Business"),

@XmlEnumValue("Economy")
ECONOMY("Economy");
private final String value;

我不明白这里出了什么问题。Morphia 是否与静态内部类或枚举一起工作。

如何解决这个问题?

最佳答案

以下代码将显示像您一样的异常:

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();
}
}
}

您可以尝试以下方法:

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();
}
}
}

这种方法并不完全符合 Morphia,而是符合 Mongo 本身。

关于java - Morphia 无法将类插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25977149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com