我是第一次使用 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/