Ayohaych
Ayohaych

Reputation: 5189

ORMLite id is always null (does not have an id field)

Can't for the life of me figure this out. So in my request object I have

@DatabaseField(id = true, columnName = "ID", canBeNull = false, generatedId = true)
private int entityId;

Whenever I do my insert method like this:

public void insert(T object) throws SQLException {
    DatabaseConnection connection = objectDao.startThreadConnection();
    Savepoint savepoint = null;
    try {
        savepoint = connection.setSavePoint(null);
        objectDao.createOrUpdate(object);
    } finally {
        connection.commit(savepoint);
        objectDao.endThreadConnection(connection);
    }
}

I always get a error like this

Class class UnsyncTripRequest does not have an id field

I'm not sure why I get this as from what I understand the id should be auto created? I see in OrmLites BaseDaoImpl class there is an extractId method that has this line:

FieldType idField = tableInfo.getIdField();

Which is always null. Anybody have any insight into how I could fix this issue. Thanks

Upvotes: 0

Views: 771

Answers (3)

Bipin
Bipin

Reputation: 352

The Dao you are working on to create and update or delete, should be synchronised. When you use

com.j256.ormlite.dao.BaseDaoImpl.createOrUpdate()

ormlite extractId to see if that can be updated, then it goes and updates that particular row.

if there is a parallel thread deletes that row which has the id.

Ormlite throws java.sql.SQLException saying "does not have an id field"

Dao is access to table operation if that is synchronised, you should be good.

synchronized (objectDao){
                 objectDao.createOrUpdate(object);
            }

Upvotes: 0

Gray
Gray

Reputation: 116888

FieldType idField = tableInfo.getIdField(); ... Which is always null.

The problem seems to be your entity, not anything to do with the save-point. Somehow the tableInfo that is being generated by the DAO doesn't match your entity. If the entityId field is in your class then I suspect that your ormlite_config.txt file hasn't been updated.

Upvotes: 1

SISLAM
SISLAM

Reputation: 50

You have both 'id=true' and 'generatedId=true' attributes set. Can you please remove 'id=true' and see what happens?

Upvotes: 0

Related Questions