Last updated Mar 27, 2024

Getting entities by PK

When using Active Objects, getting entities given their primary keys is very simple. Of course we will be using the EntityManager. The actual method is net.java.ao.EntityManager#get(Class<T>, K...):

1
2
public Post getPost(int postId)
{
    return entityManager.get(Post.class, postId);
}

Note that the get method take a vararg argument so it is possible to get several entities at once by passing several primary keys to the method. It will then return an array of entities.

This code is a sample extract from the BlogService implementation of the DogFood blog

Rate this page: