Search code examples
playorm

Having problems running a basic PlayORM example on OneToMany with Cursor


I've been playing with PlayORM recently, and found it quite interesting and useful. Hence, first of all, thanks a lot for providing this.

However, I did have some troubles running a very basic example on OneToMany relationship with potential Cursor support. Briefly, I'm having three simple classes: Email, User and Test as below.

@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, the scenario is simple and clear, and my Cassandra 1.2 environment is set up fine. Now, the problems:

  1. When I use List<Email> emails, foreign keys for e1 and e2 are stored as columns in the row of u1 - this is exactly the expected behaviour. However, when I find the u1 object, u1.getEmails() is always an empty list, without e1 and e2 in it, although they are actually there in the db. Why?

  2. When I use CursorToMany<Email> emailCursor instead, foreign keys for e1 and e2 never go into the row of u1 - this is abnormal, isn't it? Of course, in this case, when I find the u1 object, the Cursor does not have next().

BTW, I'm using PlayORM-1.7-snapshot, any tips and suggestions are very appreciated!

Lu


Solution

  • Thanks for the good words about PlayORM.
    Few things:
    1. How you are getting your u1 object? I mean which method you are using?
    2. Using Playorm command line tool, what result you are getting for the query
    "select * from User"
    OR
    select * from User where id = "u1"?

    3.There is a test case "testIndependentAddsAreCumulativeForCursor" in TestOneToMany.java. Is that running fine in your environment? If that is running fine, then please share your complete code.