Reputation: 3361
I'm trying to use ORMLite in java, and I've run into the following problem.
My code is this:
@DatabaseTable(tableName = "subscriptions")
public class Subscription {
@DatabaseField(id = true, index = true, uniqueCombo=true, foreign = true, foreignColumnName = "gcm_id")
private User user;
@DatabaseField(id = true, index = true, uniqueCombo=true, foreign = true, foreignColumnName = "id")
private Category category;
//other stuff in the class
}
Notice that both fields user and category are at the same time marked as id and foreign. I believe this is in compliance with my database:
These fields point to tables Users and Categories respectively. I think this is quite normal for a database, but please correct me if I'm wrong.
On the line
subscriptionDao = DaoManager.createDao(connectionSource, Subscription.class);
I am getting this error:
Exception in thread "main" java.lang.IllegalArgumentException: Id field user cannot also be a foreign object at com.j256.ormlite.field.FieldType.(FieldType.java:231) at com.j256.ormlite.field.FieldType.createFieldType(FieldType.java:937) at com.j256.ormlite.table.DatabaseTableConfig.extractFieldTypes(DatabaseTableConfig.java:208) at com.j256.ormlite.table.DatabaseTableConfig.fromClass(DatabaseTableConfig.java:146) at com.j256.ormlite.table.TableInfo.(TableInfo.java:53) at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:149) at com.j256.ormlite.dao.BaseDaoImpl.(BaseDaoImpl.java:126) at com.j256.ormlite.dao.BaseDaoImpl.(BaseDaoImpl.java:105) at com.j256.ormlite.dao.BaseDaoImpl$4.(BaseDaoImpl.java:895) at com.j256.ormlite.dao.BaseDaoImpl.createDao(BaseDaoImpl.java:895) at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:70)
Error should be pretty self explanatory, Id field user cannot also be a foreign object. However I couldn't find any info anywhere as to why this might be, or how to solve it. It also doesn't make sense to me, if my database design is correct.
To make even less sense, if I replace the lines
@DatabaseField(id = true, index = true, uniqueCombo=true, foreign = true, foreignColumnName = "gcm_id")
private User user;
with these
@DatabaseField(id = true, index = true, uniqueCombo=true)
private User user;
I get an expected "ORMLite does not know how to store class" error, but I do not get the same Id field _____ cannot also be a foreign object error for the category field. I only get this error for category field if I set id = false for my user field.
An easy patch would be to use a generated id for table subscriptions, but like I said, I am designing my database this way because I've been taught it is best to avoid generated ids everywhere, especially where the primary key of the table is obvious, easy to handle, works, etc (i.e. when you don't really gain anything from using a generated id).
Thank you all for your help.
Upvotes: 1
Views: 1577
Reputation: 5149
As far as I am aware, the issue is that you both have multiple id
fields and both of them are foreign.
According to the docs for id
:
Only one field can have this set in a class
A really easy fix would be using a table designed as follows:
@DatabaseTable(tableName = "subscriptions")
public class Subscription {
@DatabaseField(generatedId = true)
private int sub_id;
@DatabaseField(index = true, uniqueCombo=true, foreign = true, foreignColumnName = "gcm_id")
private User user;
@DatabaseField(index = true, uniqueCombo=true, foreign = true, foreignColumnName = "id")
private Category category;
//other stuff in the class
}
The generatedId
field auto increments the id
when these objects are inserted into the database.
Upvotes: 1