Objectbox-java: Fail to persist the Relation object

Created on 8 Jun 2017  路  13Comments  路  Source: objectbox/objectbox-java

Hi team at ObjectBox,

It is a fantastic product and I find that persisting single table/box is fast and easy. But it seems the relation persistence, though I try to stick to the tutorial, a bit complicated. I fail to persist the Relation Object. So for example, I persists a Post Object, with User Object embedded. I can get back my Post, but the user is not persisted. Here is the two entity, Post and User, for your reproduction.

package com.mm.main.app.schema;

import com.google.gson.annotations.Expose;
import com.mm.main.app.activity.storefront.im.model.MsgType;
import com.mm.main.app.manager.WebSocketManager;
import com.mm.main.app.utils.StringUtil;
import io.objectbox.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import io.objectbox.annotation.apihint.Internal;
import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.relation.ToOne;
import io.objectbox.relation.ToMany;

@Entity
public class Post implements Serializable, Sharable {
private static final long serialVersionUID = 7526472295622776149L; // unique id
@Id
long id;

private long myMerchantId;

@Expose
private int PostId = 0;
@Expose
private Integer MerchantId = 0;

private long UserId;

private long UserSourceId;

@Expose
private Integer IsMerchantIdentity = 0;
@Expose
private String PostText = "";
@Expose
private String PostImage = "";
@Expose
private Integer StatusId = 2;
@Expose
private Date LastModified = new Date();
@Expose
private Date LastCreated = new Date();
@Expose
private String UserKey = "";

public String getUserKey() {
    return UserKey;
}

public void setUserKey(String userKey) {
    UserKey = userKey;
}

@Expose
@Relation
User User = new User();
@Expose
private boolean IsShowTag = true;
@Expose
@Relation(idProperty = "myMerchantId")
Merchant Merchant = new Merchant();
@Expose
@Backlink
List<Brand> BrandList = new ArrayList<>();
@Expose
@Backlink
List<Merchant> MerchantList = new ArrayList<>();
@Expose
@Backlink
List<User> UserList = new ArrayList<>();
@Expose
@Backlink
List<Comment> PostCommentList = new ArrayList<>();
@Expose
@Backlink
List<Sku> SkuList = new ArrayList<>();
@Expose
private Boolean isExpand = false;
@Expose
@Backlink
List<User> LikeList = new ArrayList<>();
@Expose
private Boolean isLocal = false;
@Expose
private String CorrelationKey = StringUtil.getUUID();
@Expose
private String GroupKey = StringUtil.getUUID();
@Expose
private int totalCommentPage = 0;
@Expose
private int LikeCount = 0;
@Expose
private int CommentCount = 0;
@Expose
@Relation
User UserSource;

public boolean isNewsfeed;
public boolean isCuratorfeed;
public boolean isProfilefeed;

private String userKeyReferrer;

private String impressionKey;

@Keep
public com.mm.main.app.schema.User getUserSource() {
    return UserSource;
}

@Keep
public void setUserSource(com.mm.main.app.schema.User userSource) {
    UserSource = userSource;
}

public void setTotalCommentPage(Integer totalCommentPage) {
    this.totalCommentPage = totalCommentPage;
}


public int getLikeCount() {
    return LikeCount;
}

public void setLikeCount(int likeCount) {
    LikeCount = likeCount;
}

public void incrementLikeCount() {
    LikeCount++;
}

public void decrementLikeCount() {
    if (LikeCount > 0) {
        LikeCount--;
    }
}

public int getCommentCount() {
    return CommentCount;
}

public void setCommentCount(int commentCount) {
    CommentCount = commentCount;
}

public void incrementCommentCount() {
    CommentCount++;
}

public void decrementCommentCount() {
    CommentCount--;
}

public String getGroupKey() {
    return GroupKey;
}

public void setGroupKey(String groupKey) {
    GroupKey = groupKey;
}

public Boolean getLocal() {
    return isLocal;
}

public void setLocal(Boolean local) {
    isLocal = local;
}

public boolean isShowTag() {
    return IsShowTag;
}

public void setShowTag(boolean showTag) {
    IsShowTag = showTag;
}

private boolean isLiked = false;


/** Used to resolve relations */
@Internal
@Generated(1307364262)
transient BoxStore __boxStore;


@Internal
@Generated(1117743716)
transient ToOne<User> UserToOne = new ToOne<>(this, Post_.User);


@Internal
@Generated(2020640711)
transient ToOne<Merchant> MerchantToOne = new ToOne<>(this, Post_.Merchant);


@Internal
@Generated(1708529250)
transient ToOne<User> UserSourceToOne = new ToOne<>(this, Post_.UserSource);

public Integer getStatusId() {
    return StatusId;
}

public Post(Integer postId, String userKey, Integer merchantId, Integer isMerchantIdentity, String postText, String postImage, Integer statusId, Date lastModified, Date lastCreated, com.mm.main.app.schema.User user, boolean isShowTag, com.mm.main.app.schema.Merchant merchant, List<Brand> brandList, List<com.mm.main.app.schema.Merchant> merchantList, List<com.mm.main.app.schema.User> userList, List<Comment> postCommentList, List<Sku> skuList, boolean isExpand, List<com.mm.main.app.schema.User> likeList, Boolean isLocal, String correlationKey, String groupKey, Integer totalCommentPage, Integer likeCount, Integer commentCount, boolean isLiked) {
    PostId = postId;
    MerchantId = merchantId;
    IsMerchantIdentity = isMerchantIdentity;
    PostText = postText;
    PostImage = postImage;
    StatusId = statusId;
    LastModified = lastModified;
    LastCreated = lastCreated;
    User = user;
    IsShowTag = isShowTag;
    Merchant = merchant;
    BrandList = brandList;
    MerchantList = merchantList;
    UserList = userList;
    PostCommentList = postCommentList;
    SkuList = skuList;
    this.isExpand = isExpand;
    LikeList = likeList;
    this.isLocal = isLocal;
    CorrelationKey = correlationKey;
    GroupKey = groupKey;
    this.totalCommentPage = totalCommentPage;
    LikeCount = likeCount;
    CommentCount = commentCount;
    this.isLiked = isLiked;
}

public String getCorrelationKey() {
    return CorrelationKey;
}

public void setCorrelationKey(String correlationKey) {
    CorrelationKey = correlationKey;
}


public void merge(Post post) {
    PostId = post.getPostId();
    MerchantId = post.getMerchantId();
    PostText = post.getPostText();
    PostImage = post.getPostImage();
    StatusId = post.getStatusId();
    LastModified = post.getLastModified();
    LastCreated = post.getLastCreated();
    UserKey = post.getUserKey();
    User = post.getUser();
    IsShowTag = post.isShowTag();
    Merchant = post.getMerchant();
    BrandList = post.getBrandList();
    MerchantList = post.getMerchantList();
    UserList = post.getUserList();

    PostCommentList.addAll(post.getPostCommentList());
    Collections.sort(PostCommentList, new Comparator<Comment>() {
        @Override
        public int compare(Comment lhs, Comment rhs) {
            return rhs.getLastModified().compareTo(lhs.getLastModified());
        }
    });
    Map<String, Comment> commentLinkedHashMap = new LinkedHashMap<>();
    for (Comment comment : PostCommentList) {
        commentLinkedHashMap.put(comment.getCorrelationKey(), comment);
    }
    PostCommentList.clear();
    PostCommentList.addAll(commentLinkedHashMap.values());

    LikeList.addAll(post.getLikeList());
    Map<String, User> userLinkedHashMap = new LinkedHashMap<>();
    for (User user : LikeList) {
        userLinkedHashMap.put(user.getUserKey(), user);
    }
    LikeList.clear();
    LikeList.addAll(userLinkedHashMap.values());
    SkuList = post.getSkuList();
    this.isExpand = post.isExpand();
    this.isLocal = post.getLocal();
    CorrelationKey = post.getCorrelationKey();
    GroupKey = post.getGroupKey();
    this.totalCommentPage = post.getTotalCommentPage();
    IsMerchantIdentity = post.getIsMerchantIdentity();
    if (LikeCount == 0 && post.getLikeCount() > 0) {
        LikeCount = post.getLikeCount();
    }
    if (CommentCount == 0 && post.getCommentCount() > 0) {
        CommentCount = post.getCommentCount();
    }
}

public void setPostId(Integer postId) {
    PostId = postId;
}

public void setMerchantId(Integer merchantId) {
    MerchantId = merchantId;
}

public void setPostImage(String postImage) {
    PostImage = postImage;
}

public void setStatusId(Integer statusId) {
    StatusId = statusId;
}

public void setLastModified(Date lastModified) {
    LastModified = lastModified;
}

public void setLastCreated(Date lastCreated) {
    LastCreated = lastCreated;
}

@Keep
public void setUser(com.mm.main.app.schema.User user) {
    User = user;
}

@Keep
public void setMerchant(com.mm.main.app.schema.Merchant merchant) {
    Merchant = merchant;
}

@SuppressWarnings("unused")
public void setBrandList(List<Brand> brandList) {
    BrandList = brandList;
}

public void setMerchantList(List<com.mm.main.app.schema.Merchant> merchantList) {
    MerchantList = merchantList;
}

public void setUserList(List<com.mm.main.app.schema.User> userList) {
    UserList = userList;
}

public boolean isExpand() {
    return isExpand;
}

public void setExpand(boolean expand) {
    isExpand = expand;
}

public void setSkuList(List<Sku> skuList) {
    SkuList = skuList;
}

public Post() {
    IsShowTag = true;
}


public List<Comment> getPostCommentList() {
    return PostCommentList;
}

public void setPostCommentList(List<Comment> postCommentList) {
    PostCommentList = postCommentList;
}

public int getPostId() {
    return PostId;
}

public int getMerchantId() {
    return MerchantId;
}

public String getPostText() {
    return PostText;
}

public void setPostText(String postText) {
    this.PostText = postText;
}

public String getPostImage() {
    return PostImage;
}

public Date getLastModified() {
    return LastModified;
}

public Date getLastCreated() {
    return LastCreated;
}

@Keep
public User getUser() {
    if (User != null) {
        return User;
    } else {
        return new User();
    }
}

@Keep
public Merchant getMerchant() {
    return Merchant;
}

public List<Brand> getBrandList() {
    return BrandList;
}

public List<Merchant> getMerchantList() {
    return MerchantList;
}

public List<User> getUserList() {
    return UserList;
}

public List<Sku> getSkuList() {
    return SkuList;
}

public List<User> getLikeList() {
    return LikeList;
}

public void setLikeList(List<User> likeList) {
    LikeList = likeList;
}

public boolean isLiked() {
    return isLiked;
}

public void setLiked(boolean liked) {
    isLiked = liked;
}

public int getTotalCommentPage() {
    return totalCommentPage;
}

public void setTotalCommentPage(int totalCommentPage) {
    this.totalCommentPage = totalCommentPage;
}

public Integer getIsMerchantIdentity() {
    return IsMerchantIdentity;
}

public void setIsMerchantIdentity(Integer isMerchantIdentity) {
    IsMerchantIdentity = isMerchantIdentity;
}

@Override
public MsgType getMsgType(boolean isOutgoing) {
    if (isOutgoing) {
        return MsgType.OUTGOING_POST;
    } else {
        return MsgType.INCOMING_POST;
    }
}

@Override
public String getShareKey() {
    return Integer.toString(getPostId());
}

@Override
public WebSocketManager.MessageDataType getMessageDataType() {
    return WebSocketManager.MessageDataType.Post;
}

public String getUserKeyReferrer() {
    if (userKeyReferrer == null && getUser() != null) {
        return getUser().getUserKey();
    }
    return userKeyReferrer;
}

public void setUserKeyReferrer(String userKeyReferrer) {
    this.userKeyReferrer = userKeyReferrer;
}

public Post(Post post) {
    if (post!= null) {
        PostId = post.getPostId();
        MerchantId = post.getMerchantId();
        IsMerchantIdentity = post.getIsMerchantIdentity();
        PostText = post.getPostText();
        PostImage = post.getPostImage();
        StatusId = post.getStatusId();
        LastModified = post.getLastModified();
        LastCreated = post.getLastCreated();
        UserKey = post.getUserKey();
        User = new User(post.getUser());
        IsShowTag = post.isShowTag();
        if (post.getMerchant()!=null) {
            Merchant = new Merchant(post.getMerchant());
        }
        if (post.getBrandList() != null) {
            BrandList = new ArrayList<>(post.getBrandList());
        }
        if (post.getMerchantList() != null) {
            MerchantList = new ArrayList<>(post.getMerchantList());
        }
        if (post.getUserList() != null) {
            UserList = new ArrayList<>(post.getUserList());
        }
        if (post.getPostCommentList() != null) {
            PostCommentList = new ArrayList<>(post.getPostCommentList());
        }
        if (post.getSkuList() != null) {
            SkuList = new ArrayList<>(post.getSkuList());
        }
        this.isExpand = post.isExpand();
        if (post.getLikeList() != null) {
            LikeList = new ArrayList<>(post.getLikeList());
        }
        this.isLocal = post.getLocal();
        CorrelationKey = post.getCorrelationKey();
        GroupKey = post.getGroupKey();
        this.totalCommentPage = post.getTotalCommentPage();
        LikeCount = post.getLikeCount();
        CommentCount = post.getCommentCount();
        if (post.getUserSource() != null) {
            UserSource = new User(post.getUserSource());
        }
        this.userKeyReferrer = post.getUserKeyReferrer();
        this.isLiked = post.isLiked();
    }
}

@Generated(1221235648)
@Internal
/** This constructor was generated by ObjectBox and may change any time. */
public Post(long id, long myMerchantId, int PostId, Integer MerchantId, long UserId, long UserSourceId, Integer IsMerchantIdentity, String PostText, String PostImage, Integer StatusId, Date LastModified, Date LastCreated, String UserKey, boolean IsShowTag, Boolean isExpand, Boolean isLocal, String CorrelationKey, String GroupKey, int totalCommentPage, int LikeCount, int CommentCount, boolean isNewsfeed, boolean isCuratorfeed, boolean isProfilefeed, String userKeyReferrer, String impressionKey, boolean isLiked) {
    this.id = id;
    this.myMerchantId = myMerchantId;
    this.PostId = PostId;
    this.MerchantId = MerchantId;
    this.UserId = UserId;
    this.UserSourceId = UserSourceId;
    this.IsMerchantIdentity = IsMerchantIdentity;
    this.PostText = PostText;
    this.PostImage = PostImage;
    this.StatusId = StatusId;
    this.LastModified = LastModified;
    this.LastCreated = LastCreated;
    this.UserKey = UserKey;
    this.IsShowTag = IsShowTag;
    this.isExpand = isExpand;
    this.isLocal = isLocal;
    this.CorrelationKey = CorrelationKey;
    this.GroupKey = GroupKey;
    this.totalCommentPage = totalCommentPage;
    this.LikeCount = LikeCount;
    this.CommentCount = CommentCount;
    this.isNewsfeed = isNewsfeed;
    this.isCuratorfeed = isCuratorfeed;
    this.isProfilefeed = isProfilefeed;
    this.userKeyReferrer = userKeyReferrer;
    this.impressionKey = impressionKey;
    this.isLiked = isLiked;
}

public void setPostId(int postId) {
    PostId = postId;
}

public String getImpressionKey() {
    return impressionKey;
}

public void setImpressionKey(String impressionKey) {
    this.impressionKey = impressionKey;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public boolean getIsShowTag() {
    return IsShowTag;
}

public void setIsShowTag(boolean IsShowTag) {
    this.IsShowTag = IsShowTag;
}

public Boolean getIsLocal() {
    return isLocal;
}

public void setIsLocal(Boolean isLocal) {
    this.isLocal = isLocal;
}

public boolean getIsLiked() {
    return isLiked;
}

public void setIsLiked(boolean isLiked) {
    this.isLiked = isLiked;
}

public void setMerchantId(int MerchantId) {
    this.MerchantId = MerchantId;
}

public long getUserId() {
    return UserId;
}

public void setUserId(long UserId) {
    this.UserId = UserId;
}

public long getUserSourceId() {
    return UserSourceId;
}

public void setUserSourceId(long UserSourceId) {
    this.UserSourceId = UserSourceId;
}

public long getMyMerchantId() {
    return myMerchantId;
}

public void setMyMerchantId(long myMerchantId) {
    this.myMerchantId = myMerchantId;
}

public Boolean getIsExpand() {
    return isExpand;
}

public void setIsExpand(Boolean isExpand) {
    this.isExpand = isExpand;
}

}
package com.mm.main.app.schema;

import android.text.TextUtils;
import com.google.gson.annotations.Expose;
import com.mm.main.app.global.MmGlobal;
import com.mm.main.app.listitem.NewsFeedFeaturedItem;
import com.mm.main.app.manager.CacheManager;
import com.mm.main.app.schema.converter.NewsFeedFeaturedItemConverter;
import com.mm.main.app.schema.request.PostLike;
import com.mm.main.app.utils.StringUtil;
import io.objectbox.annotation.*;

import java.io.Serializable;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import io.objectbox.annotation.apihint.Internal;
import io.objectbox.BoxStore;
import io.objectbox.relation.ToOne;
import io.objectbox.relation.ToMany;

/**

  • User class for both Retrofit server exchange schema, and ORM model for Active Android
    */

@Entity
public class User implements Serializable {
public static final String GUEST_USER_PREFIX = "GUEST-";

private static final long serialVersionUID = 7526472295622776147L;  // unique id
@Id
long id;
private long myMerchantId;

@Expose
Integer FollowerCount = 0;
@Expose
Integer FriendCount = 0;
@Expose
Integer FollowingUserCount = 0;
@Expose
Integer FollowingMerchantCount = 0;
@Expose
Integer FollowingBrandCount = 0;
@Expose
Integer FollowingCuratorCount = 0;
@Expose
Integer WishlistCount = 0;
@Expose
Integer UserTypeId = 0;
@Expose
Integer MerchantId = 0;
@Expose
Integer InventoryLocationId = 0;
@Expose
String FirstName = "";
@Expose
String LastName = "";
@Expose
String MiddleName = "";
@Expose
String DisplayName = "";
@Expose
String Email = "";
@Expose
String MobileCode = "";
@Expose
String MobileNumber = "";
@Expose
String ProfileImage = "";
@Expose
String CoverImage = "";
@Expose
String ProfileAlternateImage = "";
@Expose
String CoverAlternateImage = "";
@Expose
Integer IsCurator = 0;
@Expose
Integer StatusId = 0;
@Expose
String StatusNameInvariant = "";
@Expose
String TimeZoneId = "";
@Expose
Integer LanguageId = 0;
@Expose
java.util.Date LastLogin = new Date();
@Expose
java.util.Date LastModified = new Date();
@Expose
java.util.Date LastCreated = new Date();
@Expose
java.util.Date DateOfBirth = new Date();
@Expose
Integer Age = 0;
@Expose
@Transient
Integer[] UserSecurityGroupArray = new Integer[0];
@Expose
@Transient
Integer[] UserInventoryLocationArray = new Integer[0];
@Expose
@Transient
java.util.Map<Integer, Integer[]> UserMerchantSecurityGroupArray = new ConcurrentHashMap<>();

@Transient
java.util.List<UserMerchantSecurityGroup> UserMerchantSecurityGroupArrayIM;

@Expose
String Password = "";
@Expose
String PasswordOld = "";
@Expose
@Relation(idProperty = "myMerchantId")
Merchant Merchant = new Merchant();
@Expose
@Backlink
java.util.List<Merchant> Merchants = new ArrayList<Merchant>();
@Expose
String Gender = "";
@Expose
Integer GeoCountryId = 0;
@Expose
Integer GeoProvinceId = 0;
@Expose
Integer GeoCityId = 0;
@Expose
String UserDescription = "";
@Expose
Integer PostLikeId = 0;
@Expose
Integer IsFollowing = 0;
@Expose
Integer WeChatId = 0;
@Expose
@Backlink
java.util.List<UserSocialAccount> UserSocialAccounts = new ArrayList<>();
@Expose
Integer IsMm = 0;
int ShippedOrderCount;
@Expose
private String UserKey = "";
@Expose
private String UserName = "";
@Expose
private Integer Count = 0;
@Expose
private int IsMerchant;
@Expose
private Integer IsPass;
private long myPostId;

public String getCuratorImage() {
    String key;
    if (getProfileAlternateImage() != null && !getProfileAlternateImage().equals("")) {
        key = getProfileAlternateImage();
    } else {
        key = getProfileImage();
    }
    return key;
}

public boolean isGuest() {
    return isGuest;
}

public void setGuest(boolean guest) {
    isGuest = guest;
}

@Expose
private boolean isGuest = false;

@Expose
private Double PaymentTotal;
@Expose
private Integer LoyaltyStatusId;

private String impressionKey;
private String ReferralInviteCode;
private int UserReferralCount;
private int PostCount;
@Convert(converter = NewsFeedFeaturedItemConverter.class, dbType = Integer.class)
private NewsFeedFeaturedItem.ITEM_TYPE featureItemType = NewsFeedFeaturedItem.ITEM_TYPE.TYPE_NORMAL;
private String CorrelationKey = StringUtil.getUUID();
private Integer PositionX;
private Integer PositionY;
@Relation(idProperty = "myPostId")
Post post;
/**
 * Used to resolve relations
 */
@Internal
@Generated(1307364262)
transient BoxStore __boxStore;

@Internal
@Generated(1388679202)
transient ToOne<Merchant> MerchantToOne = new ToOne<>(this, User_.Merchant);

@Internal
@Generated(111882897)
transient ToOne<Post> postToOne = new ToOne<>(this, User_.post);

public User() {
}

public User(String userKey) {
    UserKey = userKey;
}

public User(PostLike postLike) {
    User user = new User();
    if (postLike != null) {
        user.setUserKey(postLike.getUserKey());
        user.setProfileImage(postLike.getProfileImage());
        user.setDisplayName(postLike.getDisplayName());
        user.setIsCurator(postLike.getIsCurator());
    }
}

public User(ProductLikeItem productLike) {
    User user = new User();
    if (productLike != null) {
        user.setUserKey(productLike.getUserKey());
        user.setProfileImage(productLike.getProfileImage());
        user.setDisplayName(productLike.getDisplayName());
        user.setIsCurator(productLike.getIsCurator());
    }
}

public User(User user) {

    if (user != null) {
        FollowerCount = user.getFollowerCount();
        FriendCount = user.getFriendCount();
        FollowingUserCount = user.getFollowingUserCount();
        FollowingMerchantCount = user.getFollowingMerchantCount();
        FollowingBrandCount = user.getFollowingBrandCount();
        FollowingCuratorCount = user.getFollowingCuratorCount();
        WishlistCount = user.getWishlistCount();
        UserTypeId = user.getUserTypeId();
        MerchantId = user.getMerchantId();
        InventoryLocationId = user.getInventoryLocationId();
        FirstName = user.getFirstName();
        LastName = user.getLastName();
        MiddleName = user.getMiddleName();
        DisplayName = user.getDisplayName();
        Email = user.getEmail();
        MobileCode = user.getMobileCode();
        MobileNumber = user.getMobileNumber();
        ProfileImage = user.getProfileImage();
        CoverImage = user.getCoverImage();
        ProfileAlternateImage = user.getProfileAlternateImage();
        CoverAlternateImage = user.getCoverAlternateImage();
        IsCurator = user.getIsCurator();
        StatusId = user.getStatusId();
        StatusNameInvariant = user.getStatusNameInvariant();
        TimeZoneId = user.getTimeZoneId();
        LanguageId = user.getLanguageId();
        LastLogin = user.getLastLogin();
        LastModified = user.getLastModified();
        LastCreated = user.getLastCreated();
        DateOfBirth = user.getDateOfBirth();
        Age = user.getAge();
        if (user.getUserSecurityGroupArray() != null) {
            UserSecurityGroupArray = user.getUserSecurityGroupArray().clone();
        }
        if (user.getUserMerchantSecurityGroupArray() != null) {
            UserInventoryLocationArray = user.getUserInventoryLocationArray().clone();
        }
        if (user.getUserMerchantSecurityGroupArray() != null) {
            UserMerchantSecurityGroupArray = new ConcurrentHashMap<>(user.getUserMerchantSecurityGroupArray());
        }
        if (user.getUserMerchantSecurityGroupArrayIM() != null) {
            UserMerchantSecurityGroupArrayIM = new ArrayList<>(user.getUserMerchantSecurityGroupArrayIM());
        }
        Password = user.getPassword();
        PasswordOld = user.getPasswordOld();
        if (user.getMerchant() != null) {
            Merchant = new Merchant(user.getMerchant());
        }
        if (user.getMerchantList() != null) {
            Merchants = new ArrayList<>(user.getMerchants());
        }
        UserKey = user.getUserKey();
        UserName = user.getUserName();
        Count = user.getCount();
        Gender = user.getGender();
        GeoCountryId = user.getGeoCountryId();
        GeoProvinceId = user.getGeoProvinceId();
        GeoCityId = user.getGeoCityId();
        UserDescription = user.getUserDescription();
        PostLikeId = user.getPostLikeId();
        IsFollowing = user.getIsFollowing();
        WeChatId = user.getWeChatId();
        if (user.getUserSocialAccounts() != null) {
            UserSocialAccounts = new ArrayList<>(user.getUserSocialAccounts());
        }
        IsMm = user.getIsMm();
        IsMerchant = user.getIsMerchant();
        this.impressionKey = user.getImpressionKey();
        ReferralInviteCode = user.getReferralInviteCode();
        UserReferralCount = user.getUserReferralCount();
        PostCount = user.getPostCount();
        ShippedOrderCount = user.getShippedOrderCount();
        this.featureItemType = user.getFeatureItemType();
        CorrelationKey = user.getCorrelationKey();
        PositionX = user.getPositionX();
        PositionY = user.getPositionY();
        isGuest = user.isGuest();
    }
}

@Generated(1970772203)
@Internal
/** This constructor was generated by ObjectBox and may change any time. */
public User(long id, long myMerchantId, Integer FollowerCount, Integer FriendCount, Integer FollowingUserCount,
        Integer FollowingMerchantCount, Integer FollowingBrandCount, Integer FollowingCuratorCount, Integer WishlistCount,
        Integer UserTypeId, Integer MerchantId, Integer InventoryLocationId, String FirstName, String LastName, String MiddleName,
        String DisplayName, String Email, String MobileCode, String MobileNumber, String ProfileImage, String CoverImage,
        String ProfileAlternateImage, String CoverAlternateImage, Integer IsCurator, Integer StatusId, String StatusNameInvariant,
        String TimeZoneId, Integer LanguageId, java.util.Date LastLogin, java.util.Date LastModified, java.util.Date LastCreated,
        java.util.Date DateOfBirth, Integer Age, String Password, String PasswordOld, String Gender, Integer GeoCountryId,
        Integer GeoProvinceId, Integer GeoCityId, String UserDescription, Integer PostLikeId, Integer IsFollowing, Integer WeChatId,
        Integer IsMm, int ShippedOrderCount, String UserKey, String UserName, Integer Count, int IsMerchant, Integer IsPass, long myPostId,
        boolean isGuest, Double PaymentTotal, Integer LoyaltyStatusId, String impressionKey, String ReferralInviteCode,
        int UserReferralCount, int PostCount, NewsFeedFeaturedItem.ITEM_TYPE featureItemType, String CorrelationKey, Integer PositionX,
        Integer PositionY) {
    this.id = id;
    this.myMerchantId = myMerchantId;
    this.FollowerCount = FollowerCount;
    this.FriendCount = FriendCount;
    this.FollowingUserCount = FollowingUserCount;
    this.FollowingMerchantCount = FollowingMerchantCount;
    this.FollowingBrandCount = FollowingBrandCount;
    this.FollowingCuratorCount = FollowingCuratorCount;
    this.WishlistCount = WishlistCount;
    this.UserTypeId = UserTypeId;
    this.MerchantId = MerchantId;
    this.InventoryLocationId = InventoryLocationId;
    this.FirstName = FirstName;
    this.LastName = LastName;
    this.MiddleName = MiddleName;
    this.DisplayName = DisplayName;
    this.Email = Email;
    this.MobileCode = MobileCode;
    this.MobileNumber = MobileNumber;
    this.ProfileImage = ProfileImage;
    this.CoverImage = CoverImage;
    this.ProfileAlternateImage = ProfileAlternateImage;
    this.CoverAlternateImage = CoverAlternateImage;
    this.IsCurator = IsCurator;
    this.StatusId = StatusId;
    this.StatusNameInvariant = StatusNameInvariant;
    this.TimeZoneId = TimeZoneId;
    this.LanguageId = LanguageId;
    this.LastLogin = LastLogin;
    this.LastModified = LastModified;
    this.LastCreated = LastCreated;
    this.DateOfBirth = DateOfBirth;
    this.Age = Age;
    this.Password = Password;
    this.PasswordOld = PasswordOld;
    this.Gender = Gender;
    this.GeoCountryId = GeoCountryId;
    this.GeoProvinceId = GeoProvinceId;
    this.GeoCityId = GeoCityId;
    this.UserDescription = UserDescription;
    this.PostLikeId = PostLikeId;
    this.IsFollowing = IsFollowing;
    this.WeChatId = WeChatId;
    this.IsMm = IsMm;
    this.ShippedOrderCount = ShippedOrderCount;
    this.UserKey = UserKey;
    this.UserName = UserName;
    this.Count = Count;
    this.IsMerchant = IsMerchant;
    this.IsPass = IsPass;
    this.myPostId = myPostId;
    this.isGuest = isGuest;
    this.PaymentTotal = PaymentTotal;
    this.LoyaltyStatusId = LoyaltyStatusId;
    this.impressionKey = impressionKey;
    this.ReferralInviteCode = ReferralInviteCode;
    this.UserReferralCount = UserReferralCount;
    this.PostCount = PostCount;
    this.featureItemType = featureItemType;
    this.CorrelationKey = CorrelationKey;
    this.PositionX = PositionX;
    this.PositionY = PositionY;
}

public static void sortListByDisplayName(java.util.List<User> users) {
    Collections.sort(users, new Comparator<User>() {
        public int compare(User user1, User user2) {
            String name1 = user1.getDisplayName().toLowerCase();
            String name2 = user2.getDisplayName().toLowerCase();

            if (name1 == null && name2 == null) {
                return 0;
            }

            if (name1 == null) {
                return -1;
            }

            if (name2 == null) {
                return 1;
            }

            return name1.compareTo(name2);
        }
    });
}

public List<com.mm.main.app.schema.Merchant> getMerchants() {
    return Merchants;
}

public void setMerchants(java.util.List<com.mm.main.app.schema.Merchant> merchants) {
    Merchants = merchants;
}

public NewsFeedFeaturedItem.ITEM_TYPE getFeatureItemType() {
    return featureItemType;
}

public void setFeatureItemType(NewsFeedFeaturedItem.ITEM_TYPE featureItemType) {
    this.featureItemType = featureItemType;
}

public String getUserDescription() {
    return UserDescription;
}

public void setUserDescription(String userDescription) {
    UserDescription = userDescription;
}

public String getCorrelationKey() {
    return CorrelationKey;
}

public void setCorrelationKey(String correlationKey) {
    CorrelationKey = correlationKey;
}

public Integer getPositionX() {
    return PositionX;
}

public void setPositionX(Integer positionX) {
    PositionX = positionX;
}

public Integer getPositionY() {
    return PositionY;
}

public void setPositionY(Integer positionY) {
    PositionY = positionY;
}

public Integer getWishlistCount() {
    return WishlistCount;
}

public void setWishlistCount(Integer wishlistCount) {
    WishlistCount = wishlistCount;
}

public Integer getFollowingCuratorCount() {
    return FollowingCuratorCount;
}

public void setFollowingCuratorCount(Integer followingCuratorCount) {
    FollowingCuratorCount = followingCuratorCount;
}

public Integer getUserTypeId() {
    return UserTypeId;
}

public void setUserTypeId(Integer userTypeId) {
    UserTypeId = userTypeId;
}

public List<com.mm.main.app.schema.Merchant> getMerchantList() {
    return Merchants;
}

public void setMerchantList(java.util.List<com.mm.main.app.schema.Merchant> merchantList) {
    Merchants = merchantList;
}

public Integer getMerchantId() {
    return MerchantId;
}

public void setMerchantId(Integer merchantId) {
    MerchantId = merchantId;
}

public Integer getInventoryLocationId() {
    return InventoryLocationId;
}

public void setInventoryLocationId(Integer inventoryLocationId) {
    InventoryLocationId = inventoryLocationId;
}

public String getFirstName() {
    return FirstName;
}

public void setFirstName(String firstName) {
    FirstName = firstName;
}

public String getLastName() {
    return LastName;
}

public void setLastName(String lastName) {
    LastName = lastName;
}

public String getMiddleName() {
    return MiddleName;
}

public void setMiddleName(String middleName) {
    MiddleName = middleName;
}

public String getDisplayName() {
    if (UserKey.equals(MmGlobal.getInstance().getUserKey())) {
        return DisplayName;
    }
    String alias = CacheManager.getInstance().getUserAlias(UserKey);
    return !TextUtils.isEmpty(alias) ? alias : DisplayName;
}

public void setDisplayName(String displayName) {
    DisplayName = displayName;
}

public boolean hasAlias() {
    String alias = CacheManager.getInstance().getUserAlias(UserKey);
    return !TextUtils.isEmpty(alias) && !alias.equals(DisplayName);
}

public String getOrgDisplayName() {
    return DisplayName;
}

public String getEmail() {
    return Email;
}

public void setEmail(String email) {
    Email = email;
}

public String getMobileCode() {
    return MobileCode;
}

public void setMobileCode(String mobileCode) {
    MobileCode = mobileCode;
}

public String getMobileNumber() {
    return MobileNumber;
}

public void setMobileNumber(String mobileNumber) {
    MobileNumber = mobileNumber;
}

public String getProfileImage() {
    return ProfileImage;
}

public void setProfileImage(String profileImage) {
    ProfileImage = profileImage;
}

public String getCoverImage() {
    return CoverImage;
}

public void setCoverImage(String coverImage) {
    CoverImage = coverImage;
}

public String getProfileAlternateImage() {
    return ProfileAlternateImage;
}

public void setProfileAlternateImage(String profileAlternateImage) {
    ProfileAlternateImage = profileAlternateImage;
}

public void setProfileAlternateImagee(String profileAlternateImage) {
    ProfileAlternateImage = profileAlternateImage;
}

public String getCoverAlternateImage() {
    return CoverAlternateImage;
}

public void setCoverAlternateImage(String coverAlternateImage) {
    CoverAlternateImage = coverAlternateImage;
}

public Integer getIsCurator() {
    return IsCurator;
}

public void setIsCurator(Integer isCurator) {
    IsCurator = isCurator;
}

public boolean isCurator() {
    return IsCurator != null && IsCurator.intValue() > 0;
}

public Integer getStatusId() {
    return StatusId;
}

public void setStatusId(Integer statusId) {
    StatusId = statusId;
}

public String getStatusNameInvariant() {
    return StatusNameInvariant;
}

public void setStatusNameInvariant(String statusNameInvariant) {
    StatusNameInvariant = statusNameInvariant;
}

public String getTimeZoneId() {
    return TimeZoneId;
}

public void setTimeZoneId(String timeZoneId) {
    TimeZoneId = timeZoneId;
}

public Integer getLanguageId() {
    return LanguageId;
}

public void setLanguageId(Integer languageId) {
    LanguageId = languageId;
}

public Date getLastLogin() {
    return LastLogin;
}

public void setLastLogin(java.util.Date lastLogin) {
    LastLogin = lastLogin;
}

public Date getLastModified() {
    return LastModified;
}

public void setLastModified(java.util.Date lastModified) {
    LastModified = lastModified;
}

public Integer[] getUserSecurityGroupArray() {
    return UserSecurityGroupArray;
}

public void setUserSecurityGroupArray(Integer[] userSecurityGroupArray) {
    UserSecurityGroupArray = userSecurityGroupArray;
}

public Map<Integer, Integer[]> getUserMerchantSecurityGroupArray() {
    return UserMerchantSecurityGroupArray;
}

public void setUserMerchantSecurityGroupArray(java.util.Map<Integer, Integer[]> userMerchantSecurityGroupArray) {
    UserMerchantSecurityGroupArray = userMerchantSecurityGroupArray;
}

public Integer[] getUserInventoryLocationArray() {
    return UserInventoryLocationArray;
}

public void setUserInventoryLocationArray(Integer[] userInventoryLocationArray) {
    UserInventoryLocationArray = userInventoryLocationArray;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    Password = password;
}

public String getPasswordOld() {
    return PasswordOld;
}

public void setPasswordOld(String passwordOld) {
    PasswordOld = passwordOld;
}

@Keep
public com.mm.main.app.schema.Merchant getMerchant() {
    return Merchant;
}

@Keep
public void setMerchant(com.mm.main.app.schema.Merchant merchant) {
    Merchant = merchant;
}

public String getUserKey() {
    return UserKey;
}

public void setUserKey(String userKey) {
    UserKey = userKey;
}

public String getUserName() {
    return UserName;
}

public void setUserName(String userName) {
    UserName = userName;
}

public Integer getCount() {
    return Count;
}

public void setCount(Integer count) {
    Count = count;
}

public Integer getFollowerCount() {
    return FollowerCount;
}

public void setFollowerCount(Integer followerCount) {
    FollowerCount = followerCount;
}

public Integer getFriendCount() {
    return FriendCount;
}

public void setFriendCount(Integer friendCount) {
    FriendCount = friendCount;
}

public Integer getFollowingUserCount() {
    return FollowingUserCount;
}

public void setFollowingUserCount(Integer followingUserCount) {
    FollowingUserCount = followingUserCount;
}

public Integer getFollowingMerchantCount() {
    return FollowingMerchantCount;
}

public void setFollowingMerchantCount(Integer followingMerchantCount) {
    FollowingMerchantCount = followingMerchantCount;
}

public Integer getFollowingBrandCount() {
    return FollowingBrandCount;
}

public void setFollowingBrandCount(Integer followingBrandCount) {
    FollowingBrandCount = followingBrandCount;
}

public String getGender() {
    return Gender;
}

public void setGender(String gender) {
    Gender = gender;
}

public Integer getGeoCountryId() {
    return GeoCountryId;
}

public void setGeoCountryId(Integer geoCountryId) {
    GeoCountryId = geoCountryId;
}

public Integer getGeoProvinceId() {
    return GeoProvinceId;
}

public void setGeoProvinceId(Integer geoProvinceId) {
    GeoProvinceId = geoProvinceId;
}

public Integer getGeoCityId() {
    return GeoCityId;
}

public void setGeoCityId(Integer geoCityId) {
    GeoCityId = geoCityId;
}

public Date getDateOfBirth() {
    return DateOfBirth;
}

public void setDateOfBirth(java.util.Date dateOfBirth) {
    DateOfBirth = dateOfBirth;
}

public Integer getPostLikeId() {
    return PostLikeId;
}

public void setPostLikeId(Integer postLikeId) {
    PostLikeId = postLikeId;
}

public Integer getIsFollowing() {
    return IsFollowing;
}

public void setIsFollowing(Integer isFollowing) {
    IsFollowing = isFollowing;
}

public Integer getWeChatId() {
    return WeChatId;
}

public void setWeChatId(Integer weChatId) {
    WeChatId = weChatId;
}

public List<UserSocialAccount> getUserSocialAccounts() {
    return UserSocialAccounts;
}

public void setUserSocialAccounts(java.util.List<UserSocialAccount> userSocialAccounts) {
    UserSocialAccounts = userSocialAccounts;
}

public Integer getIsMm() {
    return IsMm;
}

public void setIsMm(Integer isMm) {
    IsMm = isMm;
}

public boolean isMM() {
    return IsMm != null && IsMm.intValue() == 1;
}

public String getReferralInviteCode() {
    return ReferralInviteCode;
}

public void setReferralInviteCode(String referralInviteCode) {
    ReferralInviteCode = referralInviteCode;
}

public int getUserReferralCount() {
    return UserReferralCount;
}

public void setUserReferralCount(int userReferralCount) {
    UserReferralCount = userReferralCount;
}

public int getPostCount() {
    return PostCount;
}

public void setPostCount(int postCount) {
    PostCount = postCount;
}

public int getShippedOrderCount() {
    return ShippedOrderCount;
}

public void setShippedOrderCount(int shippedOrderCount) {
    ShippedOrderCount = shippedOrderCount;
}

public Integer getAge() {
    return Age;
}

public void setAge(Integer age) {
    Age = age;
}

public Date getLastCreated() {
    return LastCreated;
}

public void setLastCreated(java.util.Date lastCreated) {
    LastCreated = lastCreated;
}

public int getIsMerchant() {
    return IsMerchant;
}

public void setIsMerchant(int isMerchant) {
    IsMerchant = isMerchant;
}

public String getImpressionKey() {
    return impressionKey;
}

public void setImpressionKey(String impressionKey) {
    this.impressionKey = impressionKey;
}

public List<UserMerchantSecurityGroup> getUserMerchantSecurityGroupArrayIM() {
    if (UserMerchantSecurityGroupArrayIM == null && UserMerchantSecurityGroupArray != null && UserMerchantSecurityGroupArray.size() != 0) {
        UserMerchantSecurityGroupArrayIM = new ArrayList<>();
        for (Map.Entry<Integer, Integer[]> entry : UserMerchantSecurityGroupArray.entrySet()) {
            Integer key = entry.getKey();
            Integer[] value = entry.getValue();
            UserMerchantSecurityGroup merchantSecurityGroup = new UserMerchantSecurityGroup(key, value);
            UserMerchantSecurityGroupArrayIM.add(merchantSecurityGroup);
        }
    }
    return UserMerchantSecurityGroupArrayIM;
}

public void setUserMerchantSecurityGroupArrayIM(java.util.List<UserMerchantSecurityGroup> userMerchantSecurityGroupArrayIM) {
    UserMerchantSecurityGroupArrayIM = userMerchantSecurityGroupArrayIM;
}

public Integer getIsPass() {
    return IsPass;
}

public void setIsPass(Integer isPass) {
    IsPass = isPass;
}

public Double getPaymentTotal() {
    return PaymentTotal;
}

public void setPaymentTotal(Double paymentTotal) {
    PaymentTotal = paymentTotal;
}

public Integer getLoyaltyStatusId() {
    return LoyaltyStatusId;
}

public void setLoyaltyStatusId(Integer loyaltyStatusId) {
    LoyaltyStatusId = loyaltyStatusId;
}

public List<Merchant> customerServiceMerchantList() {
    List<Merchant> filteredMerchants = new ArrayList<>();
    List<Integer> merchantIds = new ArrayList<>();

    if (MmGlobal.getInstance().isMMAgent()) {
        filteredMerchants.add(0, Merchant.MM());
    }

    getUserMerchantSecurityGroupArrayIM();
    if (UserMerchantSecurityGroupArrayIM != null) {
        for (UserMerchantSecurityGroup userMerchantSecurityGroup : UserMerchantSecurityGroupArrayIM) {
            Integer[] roles = userMerchantSecurityGroup.getRoles();
            for (Integer roleId : roles) {
                if (roleId.intValue() == MmGlobal.MMCSId.intValue() ||
                        roleId.intValue() == MmGlobal.MerchantCSId.intValue()) {
                    int merchantId = userMerchantSecurityGroup.getMerchantId();
                    if (merchantId != 0) {
                        merchantIds.add(merchantId);
                    }
                    break;
                }
            }
        }

        for (int id : merchantIds) {
            for (Merchant merchant : Merchants) {
                if (merchant.getMerchantId() == id) {
                    filteredMerchants.add(merchant);
                    break;
                }
            }
        }
    }

    return filteredMerchants;
}

public static User getGuestUser() {
    User user = new User();
    user.isGuest = true;
    user.UserKey = GUEST_USER_PREFIX + StringUtil.getUUID();
    return user;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public boolean getIsGuest() {
    return isGuest;
}

public void setIsGuest(boolean isGuest) {
    this.isGuest = isGuest;
}

public long getMyMerchantId() {
    return myMerchantId;
}

public void setMyMerchantId(long myMerchantId) {
    this.myMerchantId = myMerchantId;
}

/** To-one relationship, resolved on first access. */
@Generated(1861903482)
public Post getPost() {
    post = postToOne.getTarget(this.myPostId);
    return post;
}

/** Set the to-one relation including its ID property. */
@Generated(805708127)
public void setPost(Post post) {
    postToOne.setTarget(post);
    this.post = post;
}

public long getMyPostId() {
    return myPostId;
}

public void setMyPostId(long myPostId) {
    this.myPostId = myPostId;
}

}

All 13 comments

First, thank you for your intensive testing and feedback!

We agree that documentation has to be improved for relations.

There is one pitfall for to-many relations: use a single ToMany object and do not overwrite it.

Don't do this:

List BrandList = new ArrayList<>();

Leave it to this and let ObjectBox generate the assignment of a ToMany object:

List BrandList;

See also https://github.com/greenrobot/ObjectBox/issues/103#issuecomment-305456590

Thanks for your prompt feedback!
However, not only back link cannot be found, also the @Relation object cannot be found too.
For example, the User field is null in ObjectBox despite it actually link to a user in memory.

For to-one, use the ToOne to update/set the relation ship. I did not see how you do this, but as a rule of thumb: change tracking is done by ToOne/ToMany and in order to persist, changes must be tracked.

Can you give some examples or more details in your documentation? So for example, my object Post is being updated from Retrofit directly from API, including the nested user. Then I would like persist this to the Objectbox, I would therefore put(post). However, I find the user not being saved, at least, when I do postbox.getAll(), all my posts have user field as null. So you mention using ToOne to update/set/retrieve, this is a bit abstract to me. Would prefer to have example or tutorial or documentation so that I can better understand and reproduce.

What you need to do: go through object graph and "wrap" all direct object relations into the ToOne/ToMany objects. See also #104.

There are some updates to our object relation docs regarding this topic.

@andrewkkchan Is it OK to close the issue? Let us know if I missed something. Thanks!

Closing this now. Please open another issue, if I missed something. Thanks!

Hi team,

That's great library. And the support for Kotlin language, It's perfect. But I have a similar bug, that describes to here. These are my entities:

@Entity data class AuthorizedUser( @Id @Expose(serialize = false, deserialize = false) var id: Long = 0, ....... ){ lateinit var user: ToOne<User> }

@Entity data class User @JvmOverloads constructor( @Id(assignable = true) var id: Long = 0, ....... )

The user's id is assigned by the server. But after the app saves the AuthorizedUser entity and the app needs get the user entity, this is not found. But the target id (user.targetId) is correct. I am not sure, but after the save the AuthorizedUser entity, the app needs to save the User entity too. But Is it necessary? It's a good idea that the box store saves the relations after saving the root entity.

@eugenio1590 how did you resolve your problem? I have same one now

@the-rs using my previous example, it would be like this

authorizedUserBox.put(authorizedUser) userBox.put(authorizedUser.user.target)

@eugenio1590 thanks, had to box.put all my ToMany relations to make it work

@the-rs Yes, it is to be expected. My suggestion is that the box store saves the relations after saving the root entity.

CC: @greenrobot-team

This is a limitation when using self-assigned IDs (@Id(assignable = true)).

See the docs about self-assigned IDs. If you do, ObjectBox assumes you will take care of storing related entities.

Only if the ID of a related entity is 0 ObjectBox will store a related entity and assign it an ID, otherwise it will just update the relation.
-ut

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chiara-jm picture chiara-jm  路  17Comments

Gaket picture Gaket  路  79Comments

greenrobot picture greenrobot  路  17Comments

GerritDeMeulder picture GerritDeMeulder  路  20Comments

DJafari picture DJafari  路  29Comments