在 Morphia 中,我如何更新 ArrayList 中的一个嵌入对象

发布于 2025-01-03 23:51:09 字数 1625 浏览 0 评论 0原文

使用 Mongodb 与 Morphia 确实很陌生,并且看到了许多如何执行此操作的高级答案。

如果可能的话,我想做得简单,并且我有一个名为 @Embedded 的对象
fileObjects 包含 Files 对象。

我无法更新文件内的字段。

我只想更新一个字段,例如 String fileHash

@Entity
public class BatchData {

    @Id private ObjectId id;
    @Embedded
    public ArrayList<Files> fileObjects = new ArrayList<Files>();
}

更新.. 阅读 Morphia Updating 上的 wiki 并没有“真正”只说明如何执行此操作当数组包含这样的 Integer 时:

  @Embedded
   List<Integer> roomNumbers = new ArrayList<Integer>();

这是我到目前为止所尝试的:

mongo.createUpdateOperations(BatchData.class).add("fileObjects", Files, false);

此代码插入的 Files 已经在 mongo 中。 false 不会检测到这一点并将其插入到数组的末尾。我可以向 Files 添加一个唯一 ID,以便检测到正在插入的 Files 存在于数组中,然后更新它吗?

@Embedded 
public class Files
{ 
    public Files() {

    }

    public int position;
    public String fileName = "";
    public String fileHash = "";

    public Files(int pos, String fileName, String fileHash) {
        this.position = pos;
        this.fileName = fileName;
        this.fileHash = fileHash;
    }
} 

阅读其他答案,例如 morphia-mongodb-accessing 但是他已经有了
@Entity BlogEntry(参见链接)位于 mongo 外部的 POJO 中。也许我也必须这样做?
拉出来,改一下,再保存回来?

Really new to Using Mongodb with Morphia and see many advanced answers how to do this.

I want to do it simple if it's possible and I have this @Embedded Object called
fileObjects that contains Files Objects.

I Cannot update the fields inside the Files.

I want to update only one field f.ex the String fileHash.

@Entity
public class BatchData {

    @Id private ObjectId id;
    @Embedded
    public ArrayList<Files> fileObjects = new ArrayList<Files>();
}

UPDATE..
Reading the wiki at Morphia Updating dont "really" say how to do this only when the array contains Integer like this:

  @Embedded
   List<Integer> roomNumbers = new ArrayList<Integer>();

Here is what i try so far:

mongo.createUpdateOperations(BatchData.class).add("fileObjects", Files, false);

The Files that this code insert is already in the mongo. The false don't detect that and insert it at the end of the array. Can i add an unique-id to the Files so it detect that the Files am inserting exist in the array, and then just update it?

@Embedded 
public class Files
{ 
    public Files() {

    }

    public int position;
    public String fileName = "";
    public String fileHash = "";

    public Files(int pos, String fileName, String fileHash) {
        this.position = pos;
        this.fileName = fileName;
        this.fileHash = fileHash;
    }
} 

Reading other answers like morphia-mongodb-accessing but he already have the
@Entity BlogEntry(see link) in a POJO outside of mongo. Maybe i have to do the same?
Pull it out, change it and save it back?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁把谁当真 2025-01-10 23:51:09

为了让任何人高兴而回答我自己的问题。

我想我不确定是否解决了它。
fileObjects 有许多文件时,它看起来正在进行测试。
正确的 fileHash 确实已更新。

UpdateOperations<BatchData>updateOperations=mongo.createUpdateOperations
             (BatchData.class)
            .disableValidation().set("fileObjects.$.fileHash",hash).enableVali..;

mongo.update(mongo.createQuery(BatchData.class)
            .filter("uuid",theBatch.uuid)
            .filter("fileObjects.fileName","theFileName"),updateOperations);

Answering my own question for anyone's delight.

I think i solved it not sure.
It looks like it's working im testing when the fileObjects have many Files.
The right fileHash is updated indeed.

UpdateOperations<BatchData>updateOperations=mongo.createUpdateOperations
             (BatchData.class)
            .disableValidation().set("fileObjects.$.fileHash",hash).enableVali..;

mongo.update(mongo.createQuery(BatchData.class)
            .filter("uuid",theBatch.uuid)
            .filter("fileObjects.fileName","theFileName"),updateOperations);
清音悠歌 2025-01-10 23:51:09

就我而言,我能够使用removeAll方法:

UpdateOperations<MyType> ops = ds.createUpdateOperations(
            MyType.class).removeAll("myEmbeddedList", "thevalue");
ds.update(ds.find(MyType.class).get(), ops);

这假设MyType具有字段List myEmbeddedList;并且有一个值“thevalue”要从列表中删除。

In my case I was able to use the removeAll method:

UpdateOperations<MyType> ops = ds.createUpdateOperations(
            MyType.class).removeAll("myEmbeddedList", "thevalue");
ds.update(ds.find(MyType.class).get(), ops);

This assume that MyType has the field List myEmbeddedList; and that there is a vlue "thevalue" to be removed from the list.

三生殊途 2025-01-10 23:51:09

我刚刚(是的,现在)以这种方式解决了这个问题:
我通过查询获取根类

List<Applications> fileList = mDatastore.createQuery(File.class).field("_id").equal(new ObjectId(ID)).asList();

然后,我在这个根类中实现了 CRUD 操作(我将发布我的根类):

@Entity
public class Applications extends BaseEntity{


    private String name;    
    private String owner;   
    private String version;

    @Embedded
    List<Resources> resources = new ArrayList<>();

    @Embedded
    List<Activities> activities = new ArrayList<>();

    @Embedded
    List<Components> components = new ArrayList<>();

    public Applications() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getVersion() {
        return version;
    }

    public Applications setVersion(String version) {
        this.version = version;
        return this;
    }

    public List<Resources> getResources() {
        return resources;
    }

    public Applications setResources(List<Resources> resources) {
        this.resources = resources;
        return this;
    }

    public Resources getResourcesByName(String name) {
        for(Resources resource : this.resources){
            if (resource.getName().equals(name))
                return resource;
        }
        return null;
    }

    public boolean containsResourceByName(String name) {
        for(Resources resource : this.resources){
            if (resource.getName().equals(name))
                return true;
        }
        return false;
    }



    public Applications addResource(Resources newResource) {
        if (!containsResourceByName(newResource.getName())) {
          this.resources.add(newResource);
        }
        return this;
      }

    public Applications removeResource(Resources newResource) {
        if (containsResourceByName(newResource.getName())) {
          this.resources.remove(getResourcesByName(newResource.getName()));
        }
        return this;
    }

    public Applications removeResourceByName(String name) {
        if (containsResourceByName(name)) {
          this.resources.remove(getResourcesByName(name));
        }
        return this;
    }

    public List<Activities> getActivities() {
        return activities;
    }

    public Applications setActivities(List<Activities> activities) {
        this.activities = activities;
        return this;
    }

    public Activities getActivityByName(String name) {
        for(Activities activity : this.activities){
            if (activity.getName().equals(name))
                return activity;
        }
        return null;
    }

    public boolean containsActivityByName(String name) {
        for(Activities activity : this.activities){
            if (activity.getName().equals(name))
                return true;
        }
        return false;
    }

    public Applications addActivity(Activities newActivity) {
        if (!containsActivityByName(newActivity.getName())) {
          this.activities.add(newActivity);
        }
        return this;
      }

    public Applications removeActivity(Activities newActivity) {
        if (containsActivityByName(newActivity.getName())) {
          this.activities.remove(getActivityByName(newActivity.getName()));
        }
        return this;
    }

    public Applications removeActivityByName(String name) {
        if (containsActivityByName(name)) {
          this.activities.remove(getActivityByName(name));
        }
        return this;
    }

    public List<Components> getComponents() {
        return components;
    }

    public Applications setComponents(List<Components> components) {
        this.components = components;
        return this;
    }

    public Components getComponentByName(String name) {
        for(Components component : this.components){
            if (component.getName().equals(name))
                return component;
        }
        return null;
    }

    public boolean containsComponentByName(String name) {
        for(Components component : this.components){
            if (component.getName().equals(name))
                return true;
        }
        return false;
    }

    public Applications addComponent(Components newComponent) {
        if (!containsComponentByName(newComponent.getName())) {
          this.components.add(newComponent);
        }
        return this;
      }

    public Applications removeComponent(Components newComponent) {
        if (containsComponentByName(newComponent.getName())) {
          this.components.remove(getComponentByName(newComponent.getName()));
        }
        return this;
    }

    public Applications removeComponentByName(String name) {
        if (containsComponentByName(name)) {
          this.components.remove(getComponentByName(name));
        }
        return this;
    }

    public Applications updateComponentByName(String name, String newName) {
        if (containsComponentByName(name)) {
          getComponentByName(name).setName(newName);
        }
        return this;
    }


    @Override
    public String toString() {

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_ID, id);
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_NAME, name);
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_OWNER, owner);
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_VERSION, version);
            jsonObject.put(DatabaseModel.ActivitiesModel.DOCUMENT_NAME, activities);
            jsonObject.put(DatabaseModel.ResourcesModel.DOCUMENT_NAME, resources);
            jsonObject.put(DatabaseModel.ComponentsModel.DOCUMENT_NAME, components);
            return jsonObject.toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "Something went wrong";
        }

    }

一次,我修改了我的应用程序数据,我只是将其保存到主程序中的 mongodb 中:

mDatastore.save(mApplications);

希望这可以帮助你,再见!

I just (yeah, right now) solved this problem, in this way:
I get the root class with a query

List<Applications> fileList = mDatastore.createQuery(File.class).field("_id").equal(new ObjectId(ID)).asList();

Then, I have implemented the CRUD operations inside this root class (I'll post my root class):

@Entity
public class Applications extends BaseEntity{


    private String name;    
    private String owner;   
    private String version;

    @Embedded
    List<Resources> resources = new ArrayList<>();

    @Embedded
    List<Activities> activities = new ArrayList<>();

    @Embedded
    List<Components> components = new ArrayList<>();

    public Applications() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getVersion() {
        return version;
    }

    public Applications setVersion(String version) {
        this.version = version;
        return this;
    }

    public List<Resources> getResources() {
        return resources;
    }

    public Applications setResources(List<Resources> resources) {
        this.resources = resources;
        return this;
    }

    public Resources getResourcesByName(String name) {
        for(Resources resource : this.resources){
            if (resource.getName().equals(name))
                return resource;
        }
        return null;
    }

    public boolean containsResourceByName(String name) {
        for(Resources resource : this.resources){
            if (resource.getName().equals(name))
                return true;
        }
        return false;
    }



    public Applications addResource(Resources newResource) {
        if (!containsResourceByName(newResource.getName())) {
          this.resources.add(newResource);
        }
        return this;
      }

    public Applications removeResource(Resources newResource) {
        if (containsResourceByName(newResource.getName())) {
          this.resources.remove(getResourcesByName(newResource.getName()));
        }
        return this;
    }

    public Applications removeResourceByName(String name) {
        if (containsResourceByName(name)) {
          this.resources.remove(getResourcesByName(name));
        }
        return this;
    }

    public List<Activities> getActivities() {
        return activities;
    }

    public Applications setActivities(List<Activities> activities) {
        this.activities = activities;
        return this;
    }

    public Activities getActivityByName(String name) {
        for(Activities activity : this.activities){
            if (activity.getName().equals(name))
                return activity;
        }
        return null;
    }

    public boolean containsActivityByName(String name) {
        for(Activities activity : this.activities){
            if (activity.getName().equals(name))
                return true;
        }
        return false;
    }

    public Applications addActivity(Activities newActivity) {
        if (!containsActivityByName(newActivity.getName())) {
          this.activities.add(newActivity);
        }
        return this;
      }

    public Applications removeActivity(Activities newActivity) {
        if (containsActivityByName(newActivity.getName())) {
          this.activities.remove(getActivityByName(newActivity.getName()));
        }
        return this;
    }

    public Applications removeActivityByName(String name) {
        if (containsActivityByName(name)) {
          this.activities.remove(getActivityByName(name));
        }
        return this;
    }

    public List<Components> getComponents() {
        return components;
    }

    public Applications setComponents(List<Components> components) {
        this.components = components;
        return this;
    }

    public Components getComponentByName(String name) {
        for(Components component : this.components){
            if (component.getName().equals(name))
                return component;
        }
        return null;
    }

    public boolean containsComponentByName(String name) {
        for(Components component : this.components){
            if (component.getName().equals(name))
                return true;
        }
        return false;
    }

    public Applications addComponent(Components newComponent) {
        if (!containsComponentByName(newComponent.getName())) {
          this.components.add(newComponent);
        }
        return this;
      }

    public Applications removeComponent(Components newComponent) {
        if (containsComponentByName(newComponent.getName())) {
          this.components.remove(getComponentByName(newComponent.getName()));
        }
        return this;
    }

    public Applications removeComponentByName(String name) {
        if (containsComponentByName(name)) {
          this.components.remove(getComponentByName(name));
        }
        return this;
    }

    public Applications updateComponentByName(String name, String newName) {
        if (containsComponentByName(name)) {
          getComponentByName(name).setName(newName);
        }
        return this;
    }


    @Override
    public String toString() {

        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_ID, id);
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_NAME, name);
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_OWNER, owner);
            jsonObject.put(DatabaseModel.ApplicationsModel.FIELD_VERSION, version);
            jsonObject.put(DatabaseModel.ActivitiesModel.DOCUMENT_NAME, activities);
            jsonObject.put(DatabaseModel.ResourcesModel.DOCUMENT_NAME, resources);
            jsonObject.put(DatabaseModel.ComponentsModel.DOCUMENT_NAME, components);
            return jsonObject.toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "Something went wrong";
        }

    }

Once, I modified my Application data I just save it to mongodb in the main program with:

mDatastore.save(mApplications);

Hope this can help you, bye!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文