Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composite @PrimaryKey from two fields #6108

Closed
alexei-28 opened this issue Aug 15, 2018 · 8 comments
Closed

Composite @PrimaryKey from two fields #6108

alexei-28 opened this issue Aug 15, 2018 · 8 comments

Comments

@alexei-28
Copy link

alexei-28 commented Aug 15, 2018

Here my java pojo in my Android project:

public class Product extends RealmObject {
    @PrimaryKey
    private int id;
    private String name;
    private String description;
    private int order_id;
}

But the id of product is not unique. In table I can have many products with same id. The unique are combination of id and order_id. In table I can have only ONE product with same id AND order_id.

But when I try to add @PrimaryKey to order_id

public class Product extends RealmObject {
    @PrimaryKey
    private int id;
    private String name;
    private String description;
   @PrimaryKey
    private int order_id;
}

I get error on Android Studio:

e: error: A class cannot have more than one @PrimaryKey. Both "id" and "order_id" are annotated as @PrimaryKey.

So what I can do?

P.S. I update products by the next method:

  public static void updateProducts(final List<Product> incomingProductsList) {
        Realm realm = Realm.getDefaultInstance();
        try {
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    RealmList<Product> productsRealmList = new RealmList<>();
                    productsRealmList.addAll(incomingProductsList);
                   realm.copyToRealmOrUpdate(productsRealmList);
                }
            });
        } finally {
            realm.close();
        }
    }

@Zhuinden
Copy link
Contributor

Zhuinden commented Aug 15, 2018

Then the primary key field should be a String field that combines the two fields.

id_orderId

@Zhuinden
Copy link
Contributor

Duplicate of #1129

@alexei-28
Copy link
Author

Has another solution? It is difficult to change type of ID. In whole project I use ID exactly with integer type.

@Zhuinden
Copy link
Contributor

Zhuinden commented Aug 15, 2018

Shame that you are using int instead of long (although I think in Realm they are mapped to the same type), you could probably do some bit-shifting magic to merge two IDs together but keep it as a single long.

@alexei-28
Copy link
Author

I change to long

public class Product extends RealmObject {
    @PrimaryKey
    private long id;
    ...
}

@alexei-28
Copy link
Author

alexei-28 commented Aug 16, 2018

Here result POJO:

public class Product extends RealmObject {
    @PrimaryKey
    private String uniqueId;
    private long id;
    private String name;
    private String description;  
    private long order_id;

    public void createUniqueKey() {
        if (order_id > 0L) {
            this.uniqueId = this.id + "_" + this.order_id;
        } else {
            this.uniqueId = this.id + "";
        }
    }

And use:

public class ProductAdapterJsonDeserializer implements JsonDeserializer<Product> {

    @Override
    public Product deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Product product = GsonUtil.gson.fromJson(json, Product.class);
        product.createUniqueKey();
        return product;
    }
}

And now it's work fine. Is it good solution?

@Zhuinden
Copy link
Contributor

I think it's good solution 👍

@saran2020
Copy link

@Zhuinden Realm internally handles all int as long.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants