Question

Is it any way for me to get the next available ID for a table row (that would be automatically created when inserting a row in the table), so I would not be forced to insert that row at a given time, to get it?

To be more precise: I have an activity which contains a listview and each of those items are added using a second activity. When I finish adding the item details in the second activity, I pass that item through a parcelable object (I implemented Parcelable Interface to one of the holders class that DaoGenerator created). The id value of that object can not be null, to pass it with writeLong(id), and receive it with readLong() in my Parcelable methods, so I have to auto-generate the id value, by inserting the current item already in the database. What I would like to do is: generate those IDs (without inserting the item in the database), pass that item to first activity, and when user decides to save all those items from the list, I would add all of them to database in a single transaction.

some sample code I have atm:

public class Question implements Parcelable {

private Long id;
private String questionContent;

// KEEP FIELDS - put your custom fields here
// KEEP FIELDS END

public Question() {
}

public Question(Long id) {
    this.id = id;
}

public Question(Long id,String questionContent) {
    this.id = id;
    this.questionContent = questionContent;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}


// KEEP METHODS - put your custom methods here

// begin Parcelable implementation

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeLong(id);
    dest.writeString(questionContent);

}

public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
    public Question createFromParcel(Parcel in) {
        return new Question(in);
    }

    public Question[] newArray(int size) {
        return new Question[size];
    }
};

private Question(Parcel in) {

    id = in.readLong();
    questionContent = in.readString();
}

// end Parcelable implementation
// KEEP METHODS END

}

and this is how I create and send the item to list:

Question questionHolder = new Question(
                                    null,                                       etItemContent.getText().toString()                                      .trim(),);

                            Log.d(LOG_TAG, "question id = "
                                    + questionHolder.getId());

// inserting it here, would auto-generate the ID I required,
// but I would like to do that to all of the items in the first Activity (containing the list of all of the items)                                   
// questionDao.insert(questionHolder);

                            Log.d(LOG_TAG, "question id = "
                                    + questionHolder.getId());

                            // add item to intent
                            Bundle b = new Bundle();
                            b.putParcelable(IMPORTANCE_TAG, questionHolder);

                            Intent intent = new Intent();
                            intent.putExtras(b);

                            setResult(RESULT_OK, intent);
                            QuestionItemActivity.this.finish();
Was it helpful?

Solution

I would not suggest this as it create too much tight coupling. a couple options that comes to my mind:

  • If a field is nullable, I would suggest adding another flag to parcelable to denote if that field is null or not. so when writing

    if(id == null) {
        out.writeByte((byte)0);
    } else {
        out.writeByte((byte)1);
        out.writeLong(id);
    }
    

    and when reading

    boolean hasId = in.readByte() == 1;
    if(hasId) {
        id = in.readLong();
    }
    
  • Another option, since db ids start from 1, you can set id to 0 and handle this logically. so when you receive the object in your first activity, you can check the id and set to null if it is 0.

OTHER TIPS

Yes there's a mean to do that, if you're using an ORM, this would be easy as !@#%. All you have to do for example is:

  • Get the sequence name that generates the ID (there's always one even if you didn't create it manually).

  • Create an SQL query in your code for example :

    session.createSQLQuery( "SELECT nextval( 'mySequenceName' )");

  • Then execute the query to retrieve the unique ID.

I hope this will help.

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top