境界:如何构建一对多的关系 [英] Realm : How to structure one to many relationship

查看:141
本文介绍了境界:如何构建一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找到许多ORM的在Android中,迄今为止我所用,并会适合我的应用程序至今都ActiveAndroid和SugarOrm,但我需要的是目前还不支持(不支持,但可以通过创建修复SQL脚本)截至目前(一到多的关系)。林看着境界ORM Android的一个非常有前途的。

I have been looking over to many ORM's in android, by far what i have used and would fit to my app so far are ActiveAndroid and SugarOrm, but what i need is not currently supported(not supported but can be fix by creating sql scripts) as of now (one-to-many relationship). Im looking at Realm ORM for android a very promising one.

这是可能的境界?

// this is just a sample of what i need to do.,
// parent
class Message{
  long id;
  List<Meta> messages;
}

// child
class Meta{
  long senderId;
  String message;
  Date date;
  int status;
}

// I have already know how to do this on ActiveAndroid but seems a bit hard
// to update records or fetch single data.

请注意:我一直有问题,最近手动创建时,我的SQL脚本,它很费时间编码所有这些消费时ORM的都在那里,而且它很讨厌当事情需要改变我必须重组大多数受影响的列等。

Note: I have been having problems lately when manually creating my SQL scripts, and its very time consuming coding all of those when ORM's are there, And its very annoying when something needs to change I have to restructure most of the affected columns and etc.

我希望我问过我的问题清楚,我希望有一个更好的办法了这一点。

I hope I had asked my question clearly and I hope there is a better way for this.

感谢

推荐答案

是的,这是境界非常可能的。境界是对象存储,从而为他们它存储你的对象。在一个正常的对象模型一到多的关系正在使用的列表定义,同样具有该领域有一个名为RealmList可以使用特殊的列表。

Yes, that is very much possible with Realm. Realm is an object store, so it stores your objects as they are. In a normal object model one-to-many relationships are defined using lists, the same with Realm which has a special list called RealmList you can use.

在领域你有模式应该定义如下:

In Realm the model you have should be defined as follow:

class Message extends RealmObject {
  private long id;
  private RealmList<Meta> messages;

  // Getters and setters
}

class Meta extends RealmObject {
  private long senderId;
  private String message;
  private Date date;
  private int status;

  // Getters and setters
}

这篇关于境界:如何构建一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆