Google App Engine Objectify 5.1.21 - 多对多关系查询
Google App Engine Objectify 5.1.21 - Many-to-many relationship queries
我是 Objectify 编码的新手,我需要你的帮助来解决多对多关系查询的问题。
我有 2 个名为用户和安装的实体:
@Entity
public class User {
@Id private String login;
private String password;
@Index private String name;
@Index private String firstName;
@Index private byte right;
}
@Entity
public class Installation {
@Id private long id;
@Index private long clientId;
@Index private String name;
private String address1;
private String address2;
@Index private String postCode;
private String city;
@Index private String countryCode;
@Index private int status;
}
一个用户可以访问多个安装,一个安装可以有多个用户:多对多关系。我想映射这些实体,但我不想在加载用户时加载系统安装。因此,我创建了一个用于映射用户和安装的实体:
@Entity
public class InstallUser {
@Id private Long id;
@Index Key<User> userKey;
@Index Key<Installation> instalKey;
public void setLink(User user, Installation instal){
//initialize
this.userKey = Key.create(User.class, user.getLogin());
this.instalKey = Key.create(Installation.class, instal.getId());
}
}
你能帮我把查询写到:
- 加载由其登录名 (@id) 标识的用户的所有安装;
- 加载有权访问由其 ID (@id) 标识的安装的所有用户;
- 加载由其登录名 (@id) 标识并按其权限过滤(例如权限=2)的用户的所有安装。
预先感谢您的帮助。
虽然数据存储不为您执行联接,但您仍然可以执行联接 - 您是查询计划者。做 RDBMS 会做的事情。例如,要执行 #1 ("load all installations of a user identified by his login"),您将:
- 为该用户创建
Key<User>
- 查找与该用户关联的所有
InstallUser
s
- 使用所有相关的
Key<Installation>
来批量加载安装。
在 InstallUser
中使用 Ref<?>
s 而不是 Key<?>
s 并使用 @Load
注释自动获取 Installation
.
还有很多其他方法可以构建这些数据。根据关系的数量,您可以在 User
或 Installation
中使用列表 属性 而不是单独的关系实体。但你走在正确的道路上。
我是 Objectify 编码的新手,我需要你的帮助来解决多对多关系查询的问题。
我有 2 个名为用户和安装的实体:
@Entity
public class User {
@Id private String login;
private String password;
@Index private String name;
@Index private String firstName;
@Index private byte right;
}
@Entity
public class Installation {
@Id private long id;
@Index private long clientId;
@Index private String name;
private String address1;
private String address2;
@Index private String postCode;
private String city;
@Index private String countryCode;
@Index private int status;
}
一个用户可以访问多个安装,一个安装可以有多个用户:多对多关系。我想映射这些实体,但我不想在加载用户时加载系统安装。因此,我创建了一个用于映射用户和安装的实体:
@Entity
public class InstallUser {
@Id private Long id;
@Index Key<User> userKey;
@Index Key<Installation> instalKey;
public void setLink(User user, Installation instal){
//initialize
this.userKey = Key.create(User.class, user.getLogin());
this.instalKey = Key.create(Installation.class, instal.getId());
}
}
你能帮我把查询写到:
- 加载由其登录名 (@id) 标识的用户的所有安装;
- 加载有权访问由其 ID (@id) 标识的安装的所有用户;
- 加载由其登录名 (@id) 标识并按其权限过滤(例如权限=2)的用户的所有安装。
预先感谢您的帮助。
虽然数据存储不为您执行联接,但您仍然可以执行联接 - 您是查询计划者。做 RDBMS 会做的事情。例如,要执行 #1 ("load all installations of a user identified by his login"),您将:
- 为该用户创建
Key<User>
- 查找与该用户关联的所有
InstallUser
s - 使用所有相关的
Key<Installation>
来批量加载安装。
在 InstallUser
中使用 Ref<?>
s 而不是 Key<?>
s 并使用 @Load
注释自动获取 Installation
.
还有很多其他方法可以构建这些数据。根据关系的数量,您可以在 User
或 Installation
中使用列表 属性 而不是单独的关系实体。但你走在正确的道路上。