在 Java 中,如何将领域对象转换为 class 我希望写入数据库的对象?
In Java, How do I cast a Realm Object into the class I wish to write to the Database?
我目前正在处理一个项目,我的数据库中有大量表(总共大约 60 个)。我正在努力创建数据库助手 class,它将充当写入者/ reader 到/从数据库。我的写入方法示例如下:
public static void executeInsertInto(final Context context, final UserData passedObject){
//Generate a realm object from the context
Realm realm = Realm.getInstance(context);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
//Begin a transaction, create a new item from the passed object, and commit.
realm.beginTransaction();
UserData itemToWrite = realm.copyToRealm(passedObject);
realm.commitTransaction();
}
});
//Close the realm object to prevent leaks
if (realm != null) {
realm.close();
}
}
从代码中可以看出,我手动创建了一个UserData类型的对象;问题是,我要么必须重复 60 次(对于多种类型的对象/表格),要么想出更好的方法。这引出了我的问题,有没有一种方法可以使用传递的对象类型来创建对象来确定 class?像下面的代码这样的东西不想工作,因为我的 Java 代码做了一些根本性的错误:
public static void executeInsertInto(final Context context, final RealmObject passedObject){
//Generate a realm object from the context
Realm realm = Realm.getInstance(context);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
//Begin a transaction, create a new item from the passed object, and commit.
realm.beginTransaction();
//The below line is flawed in that you cannot create an object that way,
//But hopefully it illustrates what I am trying to accomplish
passedObject.getClass() itemToWrite = (passedObject.getClass()) realm.copyToRealm(passedObject);
realm.commitTransaction();
}
});
//Close the realm object to prevent leaks
if (realm != null) {
realm.close();
}
}
有人有什么想法吗?我将不胜感激!
编辑:
为了进一步澄清,假设我有两个 classes(Class Car 和 Class Bike), 两者都延伸到Class Transportation, 如果我通过class transportation作为参数,我怎么能确定要传递的类型,然后一旦传递,我如何使用该信息创建新对象?
-锡尔
All Realm 类 扩展了 RealmObject 因此您可以使用泛型来完成它:
public static <T extends RealmObject> void executeInsertInto(final Context context, final T passedObject){
Realm realm = Realm.getInstance(context);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
T itemToWrite = realm.copyToRealm(passedObject);
}
});
realm.close();
}
我目前正在处理一个项目,我的数据库中有大量表(总共大约 60 个)。我正在努力创建数据库助手 class,它将充当写入者/ reader 到/从数据库。我的写入方法示例如下:
public static void executeInsertInto(final Context context, final UserData passedObject){
//Generate a realm object from the context
Realm realm = Realm.getInstance(context);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
//Begin a transaction, create a new item from the passed object, and commit.
realm.beginTransaction();
UserData itemToWrite = realm.copyToRealm(passedObject);
realm.commitTransaction();
}
});
//Close the realm object to prevent leaks
if (realm != null) {
realm.close();
}
}
从代码中可以看出,我手动创建了一个UserData类型的对象;问题是,我要么必须重复 60 次(对于多种类型的对象/表格),要么想出更好的方法。这引出了我的问题,有没有一种方法可以使用传递的对象类型来创建对象来确定 class?像下面的代码这样的东西不想工作,因为我的 Java 代码做了一些根本性的错误:
public static void executeInsertInto(final Context context, final RealmObject passedObject){
//Generate a realm object from the context
Realm realm = Realm.getInstance(context);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
//Begin a transaction, create a new item from the passed object, and commit.
realm.beginTransaction();
//The below line is flawed in that you cannot create an object that way,
//But hopefully it illustrates what I am trying to accomplish
passedObject.getClass() itemToWrite = (passedObject.getClass()) realm.copyToRealm(passedObject);
realm.commitTransaction();
}
});
//Close the realm object to prevent leaks
if (realm != null) {
realm.close();
}
}
有人有什么想法吗?我将不胜感激!
编辑: 为了进一步澄清,假设我有两个 classes(Class Car 和 Class Bike), 两者都延伸到Class Transportation, 如果我通过class transportation作为参数,我怎么能确定要传递的类型,然后一旦传递,我如何使用该信息创建新对象?
-锡尔
All Realm 类 扩展了 RealmObject 因此您可以使用泛型来完成它:
public static <T extends RealmObject> void executeInsertInto(final Context context, final T passedObject){
Realm realm = Realm.getInstance(context);
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
T itemToWrite = realm.copyToRealm(passedObject);
}
});
realm.close();
}