Rauter
Rauter

Reputation: 521

How to use ORMLite foreign objects WITHOUT generated id?

I am learning how to use ORMLite with android. My problem is that I receive objects with an ID from the server and I think it would be good to use the same ID for my DB. This means I am not using generatedId = true and therefore cannot use foreignAutoGenerate = true.

public class Artwork {

    @DatabaseField(id = true, columnName = "id")
    String id;

    @DatabaseField
    String name;

    @DatabaseField
    String file;

    @DatabaseField(columnName = "user_id", foreign = true, foreignAutoCreate = true)
    User owner;
}

As you can see, Artwork references the user who owns it. Both already have IDs on the server side that I would like to use as IDs for my DB.

public class User {

    @DatabaseField(id = true, unique = true)
    String id;

    @DatabaseField
    String name;
}

And below is where the magic should happen...

Artwork artwork = new Artwork();
artwork.setName("name");
artwork.setFile("filepath");
artwork.setId("generated_by_server_0000");

User owner = new User();
owner.setId("generated_by_server_0001")
owner.setName("user");

artwork.setOwner(owner);

DatabaseHelper dbHelper = OpenHelperManager.getHelper(this, DatabaseHelper.class);

Dao<Artwork, String> artworkDao = dbHelper.getArtworkDao();
Dao<User, String> userDao = dbHelper.getUserDao();

userDao.create(owner);
artworkDao.create(artwork);

List<Artwork> artworksOnDb = artworkDao.queryForAll();

How can I easily persist those objects using ORMLite but setting the ID myself?

Upvotes: 2

Views: 779

Answers (1)

Gray
Gray

Reputation: 116878

My problem is that I receive objects with an ID from the server and I think it would be good to use the same ID for my DB. This means I am not using generatedId = true and therefore cannot use foreignAutoGenerate = true

Right. You don't not have to do generatedId = true with a foreign object but unfortunately you do need to do it with foreignAutoCreate = true because otherwise ORMLite wouldn't know if the User needs to be created or not. If you are using your own id, you'll need to use the UserDao and create the User directly and not rely on the auto mechanism.

To quote the docs for foreignAutoGenerate:

Set this to be true (default false) to have the foreign field automatically created using an internal DAO if its ID field is not set (null or 0). So when you call dao.create() on the parent object, any foreign field that has this set to true will possibly generate an additional create call via an internal DAO. By default you have to create the object using its DAO directly. By default you have to create the object using its DAO directly. This only works if generatedId is also set to true.

One thing that it is important to realize is that you have to insert the User before you insert the Artwork because the Artwork stores a user_id in its table.

User owner = new User();
owner.setId("generated_by_server_0001")
owner.setName("user");
...
// do this _before_ the create of Artwork
userDao.create(owner);

Artwork artwork = new Artwork();
artwork.setName("name");
...
artwork.setOwner(owner);
artworkDao.create(artwork);

Upvotes: 2

Related Questions