Category Archives: apache-cayenne

Apache cayenne not creating object relations

The code below correctly adds a 'Project' object which contains one child 'Div' object to a mysql database. However, the final line creates a null pointer exception, as although the database is persisted correctly, the relation has not been added to the java object.

    ServerRuntime cayenneRuntime = new ServerRuntime("cayenne-BlueTwine.xml");
    ObjectContext context = cayenneRuntime.getContext();

    Project project = context.newObject(Project.class); // Add new project to context

    // set some variables on project object

    Div rootDiv = context.newObject(Div.class);

    // set some variables on rootDiv object

    rootDiv.setProject(project);

    context.commitChanges();

    System.out.println(rootDiv.getProject().getId()); // Returns null pointer

When I last worked on this project a year ago, the above code would execute correctly, so I know that this is possible. Could upgrading to apache cayenne 3.1 have affected anything? Since upgrading I have synced my classes with the database schema, regenerated my classes, and rebuilt my database schema.

Any suggestions on why this relation has not been added to the java object would be greatly appreciated, as I'm stuck for where to look!

Apache Cayenne Get Collection Expression

Using Apache Cayenne I am trying to figure out how to avoid doing iterative calls to the DB when I have a Collection of attribute values.

Say we have a persistent object Person with an attribute name of type String. If I have a List containing names of the Person I would like to generate an expression that allows a single DB call rather than iterating over the list and getting each Person in turn.

This doesn't actually exist but I would like something like this:

List<String> names = ...;
ExpressionFactory.orLikeExp(Person.NAME_PROPERTY, names);