Pergunta

I am trying to store a class to MySql database. The class is declared as follows:

@DatabaseTable(tableName = "categories")
public class CategoryItem  {

    @DatabaseField(generatedId = true)
    protected long mUniqueId;

    @DatabaseField
    private String mCategoryDisplayName;

    @DatabaseField
    private int mItemsCount;    // How many items are from this category

    @DatabaseField(foreign = true, foreignAutoRefresh = true, maxForeignAutoRefreshLevel = 5)
    private CategoryItem mParent;

    protected CategoryItem() {}

When trying to store this class to MySql database (in openshift), I receive the following exception:

 java.sql.SQLException: generated-id key was not set by the update call
    com.j256.ormlite.stmt.mapped.MappedCreate.insert(MappedCreate.java:115)
    com.j256.ormlite.stmt.StatementExecutor.create(StatementExecutor.java:438)
    com.j256.ormlite.dao.BaseDaoImpl.create(BaseDaoImpl.java:308)
    com.kanooli.common.itemslist.ORMLite.ORMLiteItemsAdapter.addCategory(ORMLiteItemsAdapter.java:51)
    com.kanooli.common.itemslist.Update.updateDataBase(Updater.java:85)
    kanooliserver.mysql.DbUpdater.doGet(DbUpdater.java:65)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

When I looked in the source code, this is where this print comes:

if (key == null) {
    // may never happen but let's be careful out there
    throw new SQLException("generated-id key was not set by the update call");
}

Is this a bug in ORMLite?

Foi útil?

Solução

java.sql.SQLException: generated-id key was not set by the update call

This is strange and should not happen. I don't believe this is a bug in ORMLite. I believe that this indicates that you have a mismatch between the schema that created the database table and the entity that is being inserted into the table. Please verify that the schema that created the MySQL table actually has the id field defined something like:

`id` INTEGER AUTO_INCREMENT

If you created the table without AUTO_INCREMENT and then tried to insert an entity into the table that thought it was generatedId = true then that would result in the exception that you have seen.

I've added some checks for this specific issue, some tests to look for it, and better messaging. See these check-ins:

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top