我有产品表,我需要存储相关产品。为此,我使用关联表来存储相关产品的ID。在简化模型中看起来像:
@DatabaseTable(tableName = Product.TABLE_NAME)
public class Product {
public static final String TABLE_NAME = "product";
public interface Fields extends BaseModel.Fields {
String MODEL = "model";
String SERIES = "series";
String IS_NEW = "is_new";
String IS_BESTSELLER = "is_bestseller";
String FICHE = "fiche";
String MANUAL = "manual";
String URL = "web_url";
String VIDEO_URL = "video_presentation";
String VIDEO_360_URL = "video_360_presentation";
}
@DatabaseField(columnName = Fields.ID, id = true)
private long id;
@ForeignCollectionField(columnName = Feature.TABLE_NAME)
private ForeignCollection<ProductFeatures> features;
@ForeignCollectionField(columnName = RelatedProducts.TABLE_NAME)
private ForeignCollection<RelatedProducts> related;
public Product() {
}
}
@DatabaseTable(tableName = RelatedProducts.TABLE_NAME)
public class RelatedProducts {
public static final String TABLE_NAME = "related_products";
public interface Fields extends BaseModel.Fields {
String PRODUCT_FK = "product_id";
String RELATED_FK = "related_product_id";
}
@DatabaseField(canBeNull = false, foreign = true, columnName = Fields.PRODUCT_FK
, foreignAutoRefresh = true, maxForeignAutoRefreshLevel = 2, columnDefinition =
"INTEGER NOT NULL REFERENCES " + Product.TABLE_NAME + "(" + Product.Fields.ID + ")")
Product product_;
@DatabaseField(canBeNull = false, foreign = true, columnName = Fields.RELATED_FK
, foreignAutoRefresh = true, maxForeignAutoRefreshLevel = 2, columnDefinition =
"INTEGER NOT NULL REFERENCES " + Product.TABLE_NAME + "(" + Product.Fields.ID + ")")
Product relatedProduct;
public RelatedProducts() {
}
但是当尝试添加/实例化DB时,这意味着创建整个逻辑我得到一个错误:
08-14 10:43:29.014 24133-24170/XXX.XXX.xxx E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
Process: XXX.XXX.xxx, PID: 24133
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalArgumentException: ORMLite does not know how to store class XXX.XXX.xxx.model.Product for field product. Use another class or a custom persister.
at com.j256.ormlite.field.FieldType.<init>(FieldType.java:189)
at com.j256.ormlite.table.DatabaseTableConfig.convertFieldConfigs(DatabaseTableConfig.java:236)
at com.j256.ormlite.table.DatabaseTableConfig.extractFieldTypes(DatabaseTableConfig.java:101)
at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:153)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:128)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:119)
at com.j256.ormlite.dao.BaseDaoImpl$5.<init>(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:72)
at com.j256.ormlite.field.FieldType.configDaoInformation(FieldType.java:297)
at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:201)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:128)
at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:119)
at com.j256.ormlite.dao.BaseDaoImpl$5.<init>(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:921)
at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:72)
at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:218)
at com.j256.ormlite.table.TableUtils.createTableIfNotExists(TableUtils.java:61)
at XXX.XXX.commons.util.db.OrmLiteTableUtils.createIfNotExists(OrmLiteTableUtils.java:35)
at XXX.XXX.infrastructure.db.DatabaseHelper.onCreate(DatabaseHelper.java:80)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.onCreate(OrmLiteSqliteOpenHelper.java:209)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.j256.ormlite.android.AndroidConnectionSource.getReadWriteConnection(AndroidConnectionSource.java:66)
at com.j256.ormlite.android.AndroidConnectionSource.getReadOnlyConnection(AndroidConnectionSource.java:54)
at com.j256.ormlite.dao.BaseDaoImpl.idExists(BaseDaoImpl.java:805)
at com.j256.ormlite.dao.BaseDaoImpl.createOrUpdate(BaseDaoImpl.java:335)
at com.j256.ormlite.dao.RuntimeExceptionDao.createOrUpdate(RuntimeExceptionDao.java:249)
at XXX.XXX.infrastructure.db.GenericDao.persist(GenericDao.java:44)
at XXX.XXX.xxx.server.DeviceRegistration.addUser(DeviceRegistration.java:54)
at XXX.XXX.xxx.server.DeviceRegistration.doInBackground(DeviceRegistration.java:36)
at XXX.XXX.xxx.server.DeviceRegistration.doInBackground(DeviceRegistration.java:20)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
有任何想法如何参考?
答案 0 :(得分:0)
IllegalArgumentException:ORMLite不知道如何为字段产品存储类XXX.XXX.xxx.model.Product。使用其他类或自定义持久性。
我唯一可以猜到的是这里有Product
个错误。 Product
字段指向不具有Product
注释的不同@DatabaseTable
类,因此ORMLite不知道如何处理它。
确保XXX.XXX.xxx.model.Product
是正确的包名。