Get Related ParseUser From a ParseRelation for Android - java

I have 2 classes
User
StockProduct
StockProduct has a relation column sold_by for User.
I am trying to retrieve sold_by for the corresponding StockProduct but, it's returning 0 objects for the following code.
/**
* GET SELLER FOR CURRENT PRODUCT
* current_stock_object: ParseObject for StockProduct
*/
ParseRelation getSellerRelation = current_stock_object.getRelation("sold_by");
ParseQuery getSeller = getSellerRelation.getQuery();
getSeller.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> users, ParseException e) {
if (e!=null){
e.printStackTrace();
}
Log.d("SIZE", Integer.toString(users.size()));
}
});
Output:
I successfully retrieve the relation via Parse DataBrowser.
SIDE NOTE
/**
* GET PRICE FOR CURRENT PRODUCT
*/
ParseRelation<ParseObject> getPriceRelation = current_stock_object.getRelation("prices");
ParseQuery getPrice = getPriceRelation.getQuery();
getPrice.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject>prices, ParseException e) {
Log.d("PRICE SIZE", Integer.toString(prices.size()));
System.out.println(prices.get(0).get("costPrice"));
}
});
Works perfectly fine, leaving me with the thought that, getting ParseUser requires a different approach.
Any help would be appreciated.

I would try:
ParseRelation<ParseUser> getSellerRelation = current_stock_object.getRelation("sold_by");
getSellerRelation.getQuery().findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> users, ParseException e) {
if (e!=null){
e.printStackTrace();
}
Log.d("SIZE", Integer.toString(users.size()));
}
});
All I added was an explicit type to the ParseRelation (in this case ParseUser). The documentation uses exactly this syntax, so I'm not sure why this wouldn't work for fetching the Relation.. maybe your "current_stock_object" needs to be fetched, or the "current_stock_object" is not the one you are looking at in the Parse console. Use the debugger and check the fields of your "current_stock_object" and ensure the objectId matches the one you are looking at in the console. Again, the object could be stale may need a fetch
Side note: (unrelated possibly)
Be sure to only use ParseRelation for a many-to-many relationship, otherwise just store the ParseObjects directly as a field. In this case, you have a StockProduct with a relation to _User table. If it makes sense in your application that a StockProduct can have multiple sellers, stick with the ParseRelation. If it was actually intended that a StockProduct may only have one unique seller, switch to not using a ParseRelation

Related

What is the difference between insert () and createObject()?

I have a setChatsList() method and it has a huge code:
public void setChatsList(final ChatsModel chatsModel) {
Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(#NonNull Realm realm) {
ChatsModel realmChats = realm.createObject(ChatsModel.class);
Response realmResponse = realm.createObject(Response.class);
Item realmItem = realm.createObject(Item.class);
Message realmMessage = realm.createObject(Message.class);
Attachment realmAttachment = realm.createObject(Attachment.class);
Video realmVideo = realm.createObject(Video.class);
Response response = chatsModel.getResponse();
RealmList<Item> items = new RealmList<>();
Integer itemCount = response.getCount();
RealmList<Item> itemList = response.getItems();
if (itemList != null) {
for (Item item : itemList) {
Message message = item.getMessage();
realmMessage.setId(message.getId());
realmMessage.setDate(message.getDate());
realmMessage.setOut(message.getOut());
realmMessage.setUserId(message.getUserId());
realmMessage.setReadState(message.getReadState());
realmMessage.setTitle(message.getTitle());
realmMessage.setBody(message.getBody());
realmMessage.setRandomId(message.getRandomId());
RealmList<Attachment> attachments = message.getAttachments();
RealmList<Attachment> attachmentList = new RealmList<>();
if (attachments != null) {
for (Attachment attachment : attachments) {
String type = attachment.getType();
Video video = attachment.getVideo();
realmVideo.setAccessKey(video.getAccessKey());
realmVideo.setCanAdd(video.getCanAdd());
realmVideo.setCanEdit(video.getCanEdit());
realmVideo.setComments(video.getComments());
realmVideo.setDate(video.getDate());
realmVideo.setDescription(video.getDescription());
realmVideo.setDuration(video.getDuration());
realmVideo.setId(video.getId());
realmVideo.setOwnerId(video.getOwnerId());
realmVideo.setPhoto130(video.getPhoto130());
realmVideo.setPhoto320(video.getPhoto320());
realmVideo.setPhoto640(video.getPhoto640());
realmVideo.setPlatform(video.getPlatform());
realmVideo.setTitle(video.getTitle());
realmVideo.setViews(video.getViews());
realmAttachment.setType(type);
realmAttachment.setVideo(realmVideo);
attachmentList.add(realmAttachment);
}
realmMessage.setAttachments(attachmentList);
}
realmResponse.getItems().add(item);
}
}
realmResponse.setCount(itemCount);
realmChats.setResponse(realmResponse);
}
});
}
Works correctly!
Just read in the official documentation about the method insert(), also for storage in the database. I rewrote the setChatsList() method thus:
public void setChatsList(final ChatsModel chatsModel) {
Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(#NonNull Realm realm) {
realm.insert(chatsModel);
}
});
}
And to my surprise, it worked fine too and the code is less!
But I'm sure that not everything is so smooth, I think that somewhere there is a catch.
Question: What is the difference between insert() and createObject()?
insert()
saves an unmanaged object into the Realm (managed object is no-op), without creating a managed proxy object as return value.
createObject()
creates a managed object in Realm, and returns a proxy to this managed object.
copyToRealm()
saves an unmanaged object into the Realm, with returning a proxy to the created managed object.
The key difference between insert() and copyToRealm() is whether a proxy is returned or not; which means that inserting many items is much more efficient by re-using a single unmanaged object and calling insert() on it with the right parameters.
However, you generally need createObject() if you want to set up relations between objects.
P.S. insert/copyToRealm/createObject(clazz) have a counterpart insertOrUpdate, copyToRealmOrUpdate and createObject(clazz, primaryKeyValue) for objects with primary keys.
Assuming you have primary key as integer
0 is the default value for int fields, so if you have a RealmObject with 0 as the value then, it means that realm.createObject(YourRealmClass.class) will fail with the error mentioned below.
RealmPrimaryKeyConstraintException: Value already exists:
as it will create an object with the default value.
What is the better way to create RealmObjects?
copyToRealmOrUpdate() or insert().
I will recommend you that use copyToRealmOrUpdate() because. it is better to use as it first checks if record exists or not . If record exists then it will update if record does not exits it will insert new record .

Parse for Android can't pin List of subclass to cache

As indicated by http://blog.parse.com/2013/05/30/parse-on-android-just-got-classier/ Parse now supports subclassing of ParseObject. My issue arrises when I try to "pin" my retrieved List of Post objects from my query to the cache.
Post is a subclass of ParseObject
ParseQuery<Post> query = ParseQuery.getQuery(Post.class);
query.orderByDescending("updatedAt");
// Gets all posts from Parse.
query.findInBackground(new FindCallback<Post>() {
#Override
public void done(final List<Post> posts, ParseException e) {
if (e != null) {
// There was an error or the network wasn't available.
return;
}
// Nothing to update.
if (posts.size() == 0) {
return;
}
// Add the latest Posts from this query to the local cache.
Post.pinAllInBackground(POST_CACHE_LABEL, posts);
updateMapFromCache();
}
});
ParseObject.pinAllInBackground() only accepts List<ParseObject> not List<?> which is now creating an issue for me. Does anyone have a solution to this besides iterating through List<Post> and pinning them individually?
Thanks for your help!
This is not a answer, but I didn't have enough repo to comment.
I am having this same problem, once a object of subclass of PraseObject is stored in local data store.
I am able to retrieve it while the application is running, once application is restarted, what I get from a query similar to above is a list of ParseObject's and not a list of my custom object.
For now what I did is, created a custom parser to recreate a object of my custom implementation. But this defeats the purpose of what is indicated in "http://blog.parse.com/2013/05/30/parse-on-android-just-got-classier/".

Implementing news feed on GAE - Should I use Prospective Search?

I have an issue i'm struggling with for some time now. Im trying to implement a news feed feature in my app using GAE cloud endpoints and java. The common concept is of followers and followees, where an action of a followee can be seen by his followers. A new follower should also see his followees past actions, not only from the time he started following.
I made a few tries with the following components. Each try worked great but was lacking something:
On each user action i added a 'log' entity into the datastore with the user id included. When a user was displaying his news feed i just queried for all those entities by their user ids according to the user's followees list. Everything was fine until i realized that a 'IN' query cannot be cursored. So this option was gone.
On this try, which is also the current state of the application, im using the Search API. Upon every user action im not storing a 'log' entity into the datastore anymore but a document into a search index. Complex queries can be cursored here and the world is smiling again. But... im not too sure that, billing wise, this is a smart descision. It seems that the costs of searching/adding/deleting documents along side the documented daily limitations is making the whole thing a bit too sketchy.
The next try should be Prospective Search API. From what i'm reading in the documents it seems the right component to pick for that purpose. Unfortunately, the documentation is really poor and give very little examples. Also the billing information is unclear.
So im asking for the advice of the stackoverflow community. Can you please advise me about this matter ? and if Prospective Search is the right option to choose, can you please provide some clear sample java code that uses cloud endpoints?
EDIT : Just to emphasize the main design requirement here - The news feed feature need to have the ability to fetch sorted followees actions using a cursor (in order avoid querying the whole batch).
Use a pull-aggregate-per-follower model: periodically (or on demand) query all followees actions once and then cache them inside a dedicated per-follower entity. Remember the time of last query, so next time you just query from that point on (assuming actions can not be added/changed to the past times).
This will give you the following features (and limitations):
If query is on-demand, than you will not need to query for users that are inactive.
Since the query is "new-only" (looks for new actions only), it would cost you nothing if it returned zero results.
You will only query each followee actions per follower once. After that all recent actions would be cached inside one entity and loaded into memory with one get. This should be a substantial cost and time saving.
You could sort/filter actions in memory any way you wish.
Limitations:
Entities have a 1MB limit, so there is a max no of actions that you can cache in one entity. So you will either need to limit caching of recent actions per user or spread out action caching over multiple entities.
You will need to use IN query over followees (max 30) and also use parallel threads to achieve decent performance. This could easily hit 3-5 seconds when querying over 1000-2000 followees. Also, you could easily hit RPC limit (aka max concurrent API calls) per instance when serving multiple users at the same time.
I hope I understand the question correctly - you want to implement a news feed into your application and allow users to follow each other. The new followers need to be able to see the users actions. I am sure there are multiple other ways of solving this problem, but I will attempt to help you out by providing a solution that makes use of JAVA JDO to access the datastore.
I would first design the entity relationships in JDO as follows:
1 User to many actions.
1 User to many followers (User).
1 User to many following (User).
Here are simple JDO classes:
User Class:
#PersistenceCapable(identityType=IdentityType.APPLICATION)
public class User {
#PrimaryKey
#Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private String userId; // Google unique user ID, could also store user email.
#Persistent
private Set<Key> actions;
#Persistent
private Set<Key> followers;
#Persistent
private List<Key> following;
public User(Key key, String userId) {
this.key = key;
this.userId = userId;
this.actions = new HashSet<Key>();
this.followers = new HashSet<Key>();
this.following = new HashSet<Key>();
}
public Key getKey() {
return this.key;
}
public void addAction(Key actionKey) {
this.actions.add(actionKey);
}
public void addActions(Set<Key> actionKeys) {
this.actions.addAll(actionKeys);
}
public Set<Key> getActions() {
return this.actions;
}
public void addFollower(Key followerKey) {
this.followers.add(followerKey);
}
public void addFollowers(Set<Key> followerKeys) {
this.followers.addAll(followerKeys);
}
public Set<Key> getFollowers() {
return this.followers;
}
public void addFollowing(Key followingKey) {
this.following.add(followingKey);
}
public void addAllFollowing(Set<Key> followingKeys) {
this.following.addAll(followingKeys);
}
public Set<Key> getFollowing() {
return this.following;
}
}
Action Class:
#PersistenceCapable(identityType=IdentityType.APPLICATION)
public class Action {
#PrimaryKey
#Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
Date date;
#Persistent
private String title;
public Action(Key key, String title) {
this.key = key;
this.title = title;
this.date = new Date(); // date of creation (now).
}
public Key getKey() {
return this.key;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
}
The Action class makes use of a Date property, you can refer to the documentation for applicable data types in the datastore. When an action is created, a Date object is allocated and initialized so that it represents the time at which it was allocated, measured to the nearest millisecond.
In my example above I linked the entities by their Keys, you could instead link them by their classes as follows:
List<Action> actions;
The relationship in my example is one of an unowned one-to-many relationship, perhaps it should be owned one-to-many. More information here for your to take a look and perhaps decide which would be best for your solution.
Once the relationships have been defined, you can create your endpoint classes around the JDO model classes. This will create basic api methods. You might want to change the endpoint class methods to suit your needs, for example change the way an action is created. A basic example would be to create the key from the actions title as follows (ActionEnpoint.java):
...
#ApiMethod(name = "insertAction")
public Action insertAction( #Named("title") String title ) {
PersistenceManager pm = getPersistenceManager();
Key key = KeyFactory.createKey(Action.class.getSimpleName(), title);
Action action = null;
try {
action = new Action(key, title);
pm.makePersistent(action);
} finally {
pm.close();
}
return action;
}
...
If you want to, you can add a method to your UserEndpoint class to query the datastore and return all actions belonging to that user and per date using the datastore query objects.
You need to add a method to your UserEndpoint class that allows you to add an action to that user, here is a simple example:
...
#ApiMethod(name = "addActionToUser")
public Achiever addActionToUser(
#Named("userId") String userId,
#Named("actionTitle") String actionTitle) {
PersistenceManager pm = getPersistenceManager();
Key userKey = KeyFactory.createKey(User.class.getSimpleName(), userId);
Key actionKey = KeyFactory.createKey(Action.class.getSimpleName(), actionTitle);
User user = null;
try {
user = (User) pm.getObjectById(User.class, userKey);
user.addAction(actionKey);
pm.makePersistent(user);
} catch (Exception e) {
}
return user;
}
...
Once all of the above is complete you can easily get the list of actions per user by calling the getUser method in your UserEndpoint class, which returns a User object. You can then call [ReturnedUserObject].getActions(). A new follower can now view all of the "followees" actions by just calling the api method to get that "followees" object and get his/her actions. You can then just sort the actions by date or however you envision it.
I hope I understood your question correctly, I was unsure about the first component you mentioned, but it seemed as though you got your relationships mixed up. I hope this solution points you in the right direction at least :).
If you need any additional help or clarification, or my answer was completely off point to what you were looking for then please let me know.
Kind regards,
Miki

Make MySQL automatically refresh

I am inserting record in database(MySQL) based on user input. Immediate the data is inserted i want to retrieve the record i just inserted and display it in textfield.
But to my surprise nothing is retrieved. Is there some kind of way that will refresh my database immediately i insert data in db. I am using java!
public class Member {
public Member(){
final String nameMember = inputName.getText();
//Button listener action
newAccount.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
try {
//this method insert data into db
subscribe();
//this retrieves the data inserted by subscribe()
createdAccountDetails(nameMember);
} catch (SQLException ex) {
Logger.getLogger(Member.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}
You have to commit the data into db then only you can fetch it into results. But, it would be a bad practice to use insert and select as two different calls.
Either you can save the values in session and show them(if no exception occurred during insertion) or you can use a pl/sql function or procedure for the same.

many-to-many JPA mapping inserting but not fething the child collections

i've hit a block once again with hibernate.I've posted numerous times on different aspects of the user and contact management that i've been building.
The sad thing is that i didn't really have the time to play with it and understand it better before actually starting working with it. Sorry but English is not my native language, i rather speak french. And again i've started coding in java in an autodidact way.i'm doing all of this by reading books and haven't gone to school for it. with time constraints it's hard to read a book from beginning to the end.
I'm not sure i should put every of my codes dealing with an issue here and from what i've learned from other forum is to post just the necessary and being concise.
So in my User model i have UserAccount class, Profile that holds details like name, preferences etc , AccountSession and Phone.
my contact management model have Contact and Group.
UserAccount has one-to-one association with Profile, one-to-many with AccountSession,contact and group, all bidirectional.the one-to-many association with phone is unidirectional because contact also has and unidirectional with Phone.
Contact has a bidirectional many-o-many with group and one-to-many with phone that i said earlier.
Group also has a many-to-many bedirectional with contact.
here are the mappings
// UserAccount
......
#OneToOne(targetEntity=UserProfileImpl.class,cascade={CascadeType.ALL})
#org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
#JoinColumn(name="USER_PROFILE_ID")
private UserProfile profile;
#OneToMany(targetEntity=ContactImpl.class, cascade={CascadeType.ALL}, mappedBy="userAccount")
#org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Contact> contacts = new HashSet<Contact>();
#OneToMany(targetEntity=GroupImpl.class, cascade={CascadeType.ALL}, mappedBy="userAccount")
#org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Group> groups = new HashSet<Group>();
.......
//Group
#ManyToOne(targetEntity=UserAccountImpl.class)
#JoinColumn(name="USER_ACCOUNT_ID",nullable=false)
private UserAccount userAccount;
#ManyToMany(targetEntity=ContactImpl.class,cascade={CascadeType.PERSIST, CascadeType.MERGE})
#JoinTable(name="GROUP_CONTACT_MAP", joinColumns={#JoinColumn(name="GROUP_ID")},
inverseJoinColumns={#JoinColumn(name="CONTACT_ID")})
private Set<Contact> contacts = new HashSet<Contact>();
//Contact
....
#ManyToOne(targetEntity=UserAccountImpl.class)
#JoinColumn(name="USER_ACCOUNT_ID",nullable=false)
private UserAccount userAccount;
#ManyToMany(targetEntity=GroupImpl.class, mappedBy="contacts")
private Set<Group> groups=new HashSet<Group>();
....
// helper methods from group
public void addContact(Contact contact) {
try{
this.getContacts().add(contact);
contact.getGroups().add(this);
}catch(Exception e) {
}
}
//helper method from group
public void removeContact(Contact contact) {
contact.getGroups().remove(contact);
this.getContacts().remove(contact);
}
//helper method from contact
public void addGroup(Group group) {
try{
this.getGroups().add(group);
group.getContacts().add(this);
} catch(Exception e) {
e.printStackTrace();
}
}
//Helper method from group
public void removeGroup(Group group){
try{
group.getContacts().remove(this);
this.getGroups().remove(group);
} catch(Exception e) {
e.printStackTrace();
}
}
//UserAccount setter from Contact.All the children with many-to-one have the same
/**
* #param userAccount the userAccount to set
*/
public void setUserAccount(UserAccount userAccount) {
this.userAccount = userAccount;
}
I'ld like to pull the UserAccount by its email field which is an unique field in the UserAccount table.
In the UserAccountDAO the method i call to get the UserAccount is getUserAccountByEmail here below.So i expect this method to load all the children collections of the UserAccount namely its Contact collection, group collection.I want it in such a way that when UserAccount is loaded with Contacts collection each of the contact object has its reference with its belonging groups collection if any etc and vice versa.
public UserAccount getUserAccountByEmail(String email) {
// try {
logger.info("inside getUserAccountByEmail");
logger.debug(email);
Session session = (Session) this.getDBSession().getSession();
UserAccount user = (UserAccount) session.createCriteria(this.getPersistentClass())
.setFetchMode("contacts", FetchMode.SELECT) //recently added
.setFetchMode("groups", FetchMode.SELECT) // recently added
.add(Restrictions.eq("email", email))
.uniqueResult();
logger.debug(user);
return user;
// } catch(NonUniqueResultException ne) {
// logger.debug("Exception Occured: getUserAccountByEmail returns more than one result ", ne);
// return null;
// } catch(HibernateException he){
// logger.debug("Exception Occured: Persistence or JDBC exception in method getUserAccountByEmail ",he);
// return null;
// }catch(Exception e) {
// logger.debug("Exception Occured: Exception in method getUserAccountByEmail", e);
// return null;
// }
Since there has to be an UserAccount before any contact and groups, in my unit test when testing the saving of a contact object for which there must be an existing group i do this in order
a create userAccount object ua.
b create group object g1;
c create contact object c1;
d ua.addGroup(g1);
e c1.setUserAccount(ua);
f c1.addGroup(g1);
g uaDao.save(ua); // which saves the group because of the cascade
h cDao.save(c1);
Most of the time i use the session.get() from hibernate to pull c1 by its it id generated by hibernate and do all the assertions which works actually.
but in Integration test when i call getUserAccountByEmail with and without the setFetchMode and it returns the right object but then all the children collections are empty. i've tried the JOIN and the SELECT.the query string changes but then the result set is still the same. So this arises some questions :
1. What should i do to fix this?
2. the helper method works fine but it's on the parent side(i do it in the test).What i've been wondering about is that doing c1.setUserAccount(ua); is enough to create a strong relationship between UserAccount and contact.most of the time there will not be cases where i save the userAccount with contact but yet the helper method that set the association in both side and which is in UserAccount will not been called before i save the contact for a particular userAccount.So i'm little confused about that and suspecting that setting of the association is part of the why something is not working properly.and then calling session.get(UserAccount.class, ua.getID()) i think goes what i want and i'ld like getUserAccountByEmail to do the same.
3. ChssPly76 thinks the mapping has to be rewrite.So i'm willing to let you guide me through this.I really need to know the proper way to do this because we can't lean everything from a good book.So i you think i should change the mapping just show me how.and probable i'm doing things the wrong way without even been aware of that so don't forget i'm still learning java itself.THanks for the advise and remarks and thanks for reading this
I agree with you that it seems likely that the associations between your parent objects and their child collections are not getting persisted properly. I always like to start out by looking at what is in the database to figure out what's going on. After you run your test what do you see in the actual database?
It seems likely that one of two things is happening (using UserAccount as an example):
The items in the child collection are not getting saved to the database at all, in which case you'll be able to see in the database that there are no records associated with your UserAccount. This could be caused by saving the UserAccount object before you've added the child object to the UserAccount's collection.
The items in the child collection are getting saved to the database, but without the needed association to the parent object, in which case you'll see rows for your child items but the join column (ie 'userAccount' will be null). This could be caused by not setting the userAccount() property on the child object.
These are the two scenarios that I've run into where I've seen the problem you describe. Start by taking a look at what goes into your database and see if that leads you farther.

Categories