1

真的很喜欢 objectify,尽管仍然在为在我的应用程序中构建数据的最佳方式而苦苦挣扎。我将很快推出一个新应用程序,并且不想被一个可能无法正常运行或执行速度非常慢的结构所困。

该应用程序将在 HRD 上,并将具有大量实体类型。出于说明目的,我将组成一些示例实体。假设该应用程序适用于快餐店。每个连锁店都是一个实体(例如麦当劳、温迪等)。每个特定的特许经营权或位置也将是一个实体。员工、订单、菜单、时间表等也将是实体。

我想最大的问题是如何设置这些实体之间的关系?我通过在每个实体中将数据存储 ID 存储为 long 来存储关系。例如,每个员工实体都会有一个长值,即他们工作所在位置的数据存储 ID,以及他们所属的链的成员。

使用这种结构,我可以使用以下语句查询来自特定餐厅的所有订单:

  Long restaurantId =restaurant.getId();
  Query<Order> q=ofy.query(Order.class).filter("location", resturantId);

只是好奇以这种方式使用数据存储/对象化是否有任何问题。任何输入都会很棒!我一直在小规模使用类似的东西,似乎工作正常。理想情况下,我想要最有效的结构,我意识到这可能需要一些测试。但是,一旦部署了可能的应用程序,可能很难更改...

@Entity
public class Chain {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;
  private String type;

  //getters & setters, etc
  }

@Entity
public class Location {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private Long chain;  //which resturant chain the location belongs to (mcdonalds, wendy's,     etc)

  private String address;
  private String owner;
  private String phoneNumber;

  //getters & setters, etc
  }

@Entity
public class Employee {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private Long location;  //which location the employee works for
  private Long chain;  //which resturant chain the location belongs to (mcdonalds, wendy's,     etc)

  private String name;
  private String position;

  //getters & setters, etc
  }

@Entity
public class Order {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private Long chain;  //which resturant chain the location belongs to (mcdonalds, wendy's,     etc)
  private Long location;
  private Long employee; //employee that took order

  private Order order;
  private String time;

   //getters & setters, etc
   }
4

2 回答 2

3

这是标准做法。前进!

Objectify 很棒——我们已经使用它大约 6 个月了,我们对它非常满意。

于 2011-10-28T23:39:29.560 回答
0

Key<Object>是类型安全的,Long不是。文档中不鼓励使用 Long。

参考:https ://github.com/objectify/objectify/wiki/Entities#relationships

我鼓励您通读整个页面,这值得花时间。我现在有一个Ref<Object>到处使用的类型安全结构。

于 2016-04-27T03:12:00.377 回答