0
votes

I got two Entities :

public class UserAccount extends BaseEntity {
    @Expose
    @Property("username")
    @Indexed(value = IndexDirection.ASC, name = "userNameIndex", unique = true)
    private String userName = new String();

    @Expose
    @Property("password")
    private String password = new String();
}

and

public class UserProfile extends BaseEntity {
    @Expose
    @Property("first name")
    @Indexed(value = IndexDirection.ASC, name = "firstNameIndex")
    private String firstName = new String();

    @Expose
    @Property("middle name")
    // @Indexed(value = IndexDirection.ASC, name = "middleNameIndex")
    private String middleName = new String();

    @Expose
    @Property("last name")
    @Indexed(value = IndexDirection.ASC, name = "lastNameIndex")
    private String lastName = new String();

    @Expose
    @Reference(/* idOnly = true, */value = "user id" /* , lazy = true */)
    private UserAccount userAccount = new UserAccount();
}

I am trying to search all Users based on searched UserName (contains! but unique) and UsedProfileObjectId (@Id). I am trying to constraint check while saving userdetails if the provided username is Unique or not? (I need to check it in both new add and update cases). So I tried to code this:

public List<UserProfile> findAllUsersWithSameUserName(ObjectId id,
            String userName) {
        Datastore ds = getDatastore();
        Query<UserProfile> profileQuery = ds.createQuery(UserProfile.class);
        Query<UserAccount> accountQuery = ds.createQuery(UserAccount.class);
        accountQuery.criteria("username").containsIgnoreCase(userName);
        container.add(profileQuery.criteria("user id").in(
                accountQuery.asKeyList()));

        return profileQuery.asList();
    }

But this code is showing

java.lang.NullPointerException

  1. Am I missing something on code?
  2. Also I try to enforce

@Reference(value = "user id", lazy = true) private UserAccount userAccount = new UserAccount();

But, I got no clue how to rebind those Object with NO Data?

I tried to do this:

        user = userProfileDAO.findUserByProfileID(id);
        user.getUserAccount();
        user.getUserAccount().getUserName();
        user.getUserAccount().getPassword();

        // Gson gson = new Gson();
        // .setDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").create();
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd")
                .excludeFieldsWithoutExposeAnnotation().setPrettyPrinting()
                .create();
        response = gson.toJson(user, UserProfile.class);

Still the reponse got EMPTY UserAccount Data:

"userAccount": {
    "userName": "",
    "password": "",
    "isDeleted": false,
    "isActive": false,
    "addedOn": "2015-08-06"
  },

Even though on inspection I can get actual data like this: user.getUserAccount().getUserName() Could not find any good example of using lazy=true in Morphia too? How can I enforce this lazy effect on my code?

1
Without seeing the stacktrace it's going to be hard to tell... I'm not entirely sure what your second question is about. What do you mean by "enforce this lazy effect on my code?" - evanchooly
Have you tried without a space in the property name? That's definitely an unusual choice - xeraa
@evanchooly can you suggest me how can I use Lazy on "Reference" Object and bind it with its parent Object while fetching? - Milson
@xeraa can you suggest me how can I use Lazy on "Reference" Object and bind it with its parent Object while fetching? - Milson
The annotations docs explain it a bit. - evanchooly

1 Answers

1
votes

This is a way how @Reference Object Query need to be done:

public UserProfile findAllUsersWithSameUserName(ObjectId id, String userName) {
        Datastore ds = getDatastore();

        Query<UserProfile> profileQuery = ds.createQuery(UserProfile.class);
        Query<UserAccount> accountQuery = ds.createQuery(UserAccount.class);
        accountQuery.criteria("username").hasThisOne(userName.toString());
        profileQuery.and(profileQuery.criteria("_id").notEqual(id),
                profileQuery.criteria("user id").in(accountQuery.asKeyList()));
        return profileQuery.get();
    }