我最近一直在玩 PlayORM,发现它非常有趣和有用。因此,首先,非常感谢提供这个。
但是,我在运行一个非常基本的关于 OneToMany 关系的示例时确实遇到了一些麻烦,并且可能支持 Cursor。简而言之,我有三个简单的类:电子邮件、用户和测试,如下所示。
@NoSqlEntity
public class Email {
@NoSqlId
private String id;
//getters and setters
... ...
}
@NoSqlEntity
@NoSqlQueries({ ... })
public class User {
@NoSqlId
private String id;
@NoSqlIndexed
private String name;
@NoSqlIndexed
private int age;
@NoSqlOneToMany
private List<Email> emails = new ArrayList<Email>();
//@NoSqlOneToMany
//private CursorToMany<Email> emailCursor = new CursorToManyImpl<Email>();
//getters and setters
... ...
public void addEmail(Email e) {
emails.add(e);
//emailCursor.addElement(e);
}
}
public class Test {
private static NoSqlEntityManager mgr;
public static void main(String[] args) {
Map properties = new HashMap();
properties.put(Bootstrap.AUTO_CREATE_KEY, "create");
String clusterName = ...
String seeds = ...
String keyspace = ...
Bootstrap.createAndAddBestCassandraConfiguration(properties, clusterName, keyspace, seeds);
NoSqlEntityManagerFactory factory = Bootstrap.create(DbTypeEnum.CASSANDRA, properties, null, null);
mgr = factory.createEntityManager();
Email e1 = new Email();
Email e2 = new Email();
mgr.put(e1);
mgr.put(e2);
User u1 = new User();
u1.setName...
u1.setAge...
u1.addEmail(e1);
u1.addEmail(e2);
mgr.put(u1);
mgr.flush();
... ...
}
OK,场景简单明了,我的 Cassandra 1.2 环境搭建好了。现在,问题:
当我使用
List<Email> emails
时,e1 和 e2 的外键存储为 u1 行中的列 - 这正是预期的行为。但是,当我找到 u1 对象时,u1.getEmails()
它始终是一个空列表,其中没有 e1 和 e2,尽管它们实际上在 db 中。为什么?当我
CursorToMany<Email> emailCursor
改为使用时,e1 和 e2 的外键永远不会进入 u1 的行 - 这是不正常的,不是吗?当然,在这种情况下,当我找到 u1 对象时,Cursor 没有 next()。
顺便说一句,我正在使用 PlayORM-1.7-snapshot,非常感谢任何提示和建议!
鲁