android - 如何在 SugarORM 中设置主键

标签 android primary-key sugarorm

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

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

这可能吗? 谢谢。

最佳答案

目前无法指定主键。但是从 sugar 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 中设置主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32932808/

相关文章:

android - 将 XML WebView 转换为 @Composable

constraints - 在 CockroaachDB 中应用主键约束 - 多个主键错误

java - SugarORM 中的持久化对象

java - Sugar ORM 库数据未正确检索

android - 使用 SugarORM 和 GSON 解析字符串 id

android - 在android平台上没有办法自动播放视频吗?

android - 如何在 Android 上测试自动续订订阅

android - 使用 Novoda 的 ImageLoader 显示图像下载进度

c# - 在使用 int 对象 ID 时,我应该使用可为 null 的 int 吗?

sql - 重复主键sql