1

I am trying to solve a problem of disconnected clients while using activejdbc. So the client app queues the inserts and subsequent updates and deletes when there is no internet. Then the batch of these come thru when they have connectivity.

To support this the client uses temporary primary keys which are replaced with the result of the the auto_increment key produced when the connectivity is restored and the batch applied. To do this I need to get the key generated when I do a save() or create(). Can activejdbc use https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#getGeneratedKeys%28%29 ?

oldDave
  • 395
  • 6
  • 25

1 Answers1

2

When you save a model, its ID is in fact becomes the auto-generated value returned by the database:

Person p = new Person(); 
p.set("first_name", "John", "last_name", "Doe");
p.saveIt(); 
System.out.println("ID of this model: " + p.getId()); 

However, if you try to set the ID of a model manually, then the framework assumes you know what you are doing and tries to use that ID in the insert.

Please, see: http://javalite.io/surrogate_primary_keys.

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Does this wortk the same way with foreign tables? I do exactly as you described above and i get a null in return for getId() – nyx00 Nov 12 '19 at 16:42
  • @nyx00 , feel free to open a ticket: https://github.com/javalite/javalite/issues - and we will look into it if you think this is a bug – ipolevoy Nov 13 '19 at 21:46