领域 IllegalStateException:模式验证失败
Realm IllegalStateException: Schema validation failed
我正在尝试在我的项目中使用 Realm 数据库
这是我的代码片段
在申请中java代码
public class CoreApplication extends Application {
private static CoreApplication sInstance;
public static final String F_PREFERENCE = "K_PREFERENCES";
public static Realm realm;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
LocaleManager.setLocale(this);
realm.init(this);
realm = Realm.getDefaultInstance();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name("mydb.realm")
.schemaVersion(2)
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
@Override
public void onTerminate() {
Realm.getDefaultInstance().close();
super.onTerminate();
}
}
RealmObject java class
public class CashierTable extends RealmObject implements Serializable {
@Index
@PrimaryKey
private long id;
private String name = "";
private String password = "";
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
当我尝试调用此函数时,App started.I 出现异常
public static CashierTable getSingleCashierTable() {
CoreApplication.realm = Realm.getDefaultInstance();
AtomicReference<CashierTable> cashierTable=new AtomicReference<>();
CoreApplication.realm .executeTransaction(realm -> {
cashierTable.set(realm.where(CashierTable.class).findFirst());
});
return cashierTable.get();
}
这是我的日志结果
java.lang.RuntimeException: Unable to create application com.unipay.posApp.CoreApplication: java.lang.IllegalStateException: Schema validation failed due to the following errors:
- Type 'CashierTable' appears more than once in the schema.
我已经结清了现金,但仍然有同样的问题。
谁能解释一下我的代码有什么问题?
你不能有两个具有相同 class 名称的 RealmObject class 两次,除非你使用 Realm 5.0+ 并使用 @RealmClass(name="...
为它们指定不同的 table 名称至少你的 classes.
之一
package io.package.first;
public class Dog extends RealmObject {
}
package io.package.second;
@RealmClass(name="SecondDog")
public class Dog extends RealmObject {
}
我正在尝试在我的项目中使用 Realm 数据库
这是我的代码片段
在申请中java代码
public class CoreApplication extends Application {
private static CoreApplication sInstance;
public static final String F_PREFERENCE = "K_PREFERENCES";
public static Realm realm;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
LocaleManager.setLocale(this);
realm.init(this);
realm = Realm.getDefaultInstance();
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.name("mydb.realm")
.schemaVersion(2)
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
@Override
public void onTerminate() {
Realm.getDefaultInstance().close();
super.onTerminate();
}
}
RealmObject java class
public class CashierTable extends RealmObject implements Serializable {
@Index
@PrimaryKey
private long id;
private String name = "";
private String password = "";
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
当我尝试调用此函数时,App started.I 出现异常
public static CashierTable getSingleCashierTable() {
CoreApplication.realm = Realm.getDefaultInstance();
AtomicReference<CashierTable> cashierTable=new AtomicReference<>();
CoreApplication.realm .executeTransaction(realm -> {
cashierTable.set(realm.where(CashierTable.class).findFirst());
});
return cashierTable.get();
}
这是我的日志结果
java.lang.RuntimeException: Unable to create application com.unipay.posApp.CoreApplication: java.lang.IllegalStateException: Schema validation failed due to the following errors:
- Type 'CashierTable' appears more than once in the schema.
我已经结清了现金,但仍然有同样的问题。
谁能解释一下我的代码有什么问题?
你不能有两个具有相同 class 名称的 RealmObject class 两次,除非你使用 Realm 5.0+ 并使用 @RealmClass(name="...
为它们指定不同的 table 名称至少你的 classes.
package io.package.first;
public class Dog extends RealmObject {
}
package io.package.second;
@RealmClass(name="SecondDog")
public class Dog extends RealmObject {
}