0

If i have for example a table Shows and i have a to-many relation to a table Actors
When inserting a Show which doesn't have id(its auto-generated), how can i insert Actors if i don't have id of the show to relate to?

Here is DaoGenerator code:

    Entity show = schema.addEntity("Show");
    show.setHasKeepSections(true);
    show.addIdProperty();
    show.addIntProperty("tvdb_id").notNull();
    show.addStringProperty("title");
    show.addIntProperty("year");
    show.addStringProperty("url");
    show.addLongProperty("first_aired");
    show.addStringProperty("country");
    show.addStringProperty("overview");
    show.addIntProperty("runtime");
    show.addStringProperty("status");
    show.addStringProperty("network");
    show.addStringProperty("air_day");
    show.addStringProperty("air_time");
    show.addStringProperty("certification");
    show.addStringProperty("imdb_id");
    show.addIntProperty("tvrage_id");
    show.addLongProperty("last_updated");
    show.addIntProperty("rating");
    show.addIntProperty("votes");
    show.addIntProperty("loved");
    show.addIntProperty("hated");

    Entity actor = schema.addEntity("Actor");
    actor.addIdProperty();
    actor.addStringProperty("name");
    actor.addStringProperty("character");
    actor.addStringProperty("image");

    Property showId = actor.addLongProperty("show_id").notNull().getProperty();
    ToMany showToActor= show.addToMany(actor, showId);
    showToActor.setName("actors");
pedja
  • 3,285
  • 5
  • 36
  • 48

2 Answers2

1

The ID is auto-generated when you insert the object.

So you have to insert the show before adding actors to it.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • OK, but doesn't that mean that i would have to do a query after insert so that i can get an id of the show? – pedja Jun 01 '14 at 08:47
  • Return type of `insert` method is long, could that be an id of the entry? I can't find any info in the documentation. – pedja Jun 01 '14 at 08:51
1

You can just call refresh() on that entity, the current copy would get updated and so its id.

Then you should take this showId and set it to each actor's show id.

show.refresh();
for (Actor actor : actors)
    actor.setShowId(show.getId());


actorDao.insertAllInTx(actors);
Nativ
  • 3,092
  • 6
  • 38
  • 69