android – 如何在SugarORM中设置主键

我是第一次使用SugarORM,我有一个简单的问题.
如何将一个实体设置为主键?例如,我需要这样的东西:

public class Student{
    @PrimaryKey
    private String studentId;
    private String name;
}

可能吗?
谢谢.

最佳答案
目前无法指定主键.但从糖1.4开始,可以选择使用@Unique属性(https://github.com/satyan/sugar#sugarrecord):

public class Book extends SugarRecord {
    @Unique
    String isbn;
    String title;
    String edition;
    /*...*/
    public Book(String isbn, String title, String edition) {
        this.isbn = isbn;
        this.title = title;
        this.edition = edition;
    }
}

它不是主键,但在许多情况下它做类似的工作(https://github.com/satyan/sugar#update-entity-based-on-unique-values):

Book book = new Book("isbn123", "Title here", "2nd edition")
book.save();

// Update book with isbn123
Book sameBook = new Book("isbn123", "New Title", "5th edition")
sameBook.update();

book.getId() == sameBook.getId();

转载注明原文:android – 如何在SugarORM中设置主键 - 代码日志