Spring data MongoDB 更新整个内嵌文档时报错???????
//实体类结构如下:
User类中包含了Home
public class User
{
private String id;
private String firstname;
private String lastname;
private int age;
private Home home;
public User()
{
};
public User(String id, String firstname, String lastname, int age, Home home)
{
super();
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.age = age;
this.home = home;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getLastname()
{
return lastname;
}
public void setLastname(String lastname)
{
this.lastname = lastname;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString()
{
return "User [id=" + id + ", firstname=" + firstname + ", lastname=" + lastname + ", age=" + age + "]";
}
public Home getHome()
{
return home;
}
public void setHome(Home home)
{
this.home = home;
}
}
//在MongoDB数据库中存储结构如下:
{
"_id" : "1001",
"home" : {
"homeAddress" : "深圳",
"homeName" : "中海日辉台"
},
"lastname" : "mook kim",
"age" : 30,
"firstname" : "yong"
}
//想对整个Home进行替换更新,但是用spring data update方法时报错:
Home home = new Home();
home.setHomeAddress("XXX");
home.setHomeName("XXX");
mongoOperation.updateFirst("userprofile", new Query(Criteria.where("firstname").is("yong")),
Update.update("home", home));
Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class com.mkyong.user.Home
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:205)
at org.bson.BSONEncoder.putMap(BSONEncoder.java:245)
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:177)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:121)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:67)
at com.mongodb.OutMessage.putObject(OutMessage.java:189)
at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:333)
at com.mongodb.DBCollection.update(DBCollection.java:145)
at com.mongodb.DBCollection.update(DBCollection.java:157)
at org.springframework.data.document.mongodb.MongoTemplate$12.doInCollection(MongoTemplate.java:821)
at org.springframework.data.document.mongodb.MongoTemplate$12.doInCollection(MongoTemplate.java:817)
at org.springframework.data.document.mongodb.MongoTemplate.execute(MongoTemplate.java:315)
at org.springframework.data.document.mongodb.MongoTemplate.updateFirst(MongoTemplate.java:817)
at com.mkyong.core.App.main(App.java:46)
请问这个是什么原因啊????????????不能序列化是什么意思? MongoDB Spring --------------------编程问答-------------------- 我也遇见了这个问题,请问您解决了吗?求教 --------------------编程问答-------------------- 类实现Serializable接口
补充:Java , Java EE