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
- Am I missing something on code?
- 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?