1

I was using activejdbc 1.4.9 and the following sample code was running just fine

Client client = new Client();
client.save(); 
Assert.assertNotNull(client.getId());

Since I upgraded to 1.4.12, client.getId() is always returning null when save is inserting a new record. i.e. id is not getting refreshed.

Did anyone notice this as well? Do I have to do anything different using this version to get the newly created id?

ccarvalho
  • 11
  • 1

1 Answers1

1

I cannot confirm this with the version 1.4.12. For instance, I wrote this example: https://github.com/javalite/simple-example/blob/new_id. Check out code in the Main.java. As you can see, the code is identical to yours, but on line 21, it prints out a real value of the new ID.

If you can put together a simple example that replicates your issue, I will take a look.

EDIT:

Now that you provided more info in comments below, the problem is with you setting the ID to empty string: "". Because the ID is not null anymore, the method save() uses update rather than insert. The update then uses the value of ID to update an "existing" record, and, as a result does not do anything. Messing with ID value is possible but not advised. Please see this for more information: http://javalite.io/surrogate_primary_keys

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • While preparing the example I think I noticed what may be causing the issue. My code sets the attributes before saving the model, such as: public setClient(String id, String name) { Client client = new Client(); client.setId(id); client.setName(name); client.save(); } When this is a new client, id parameter comes as empty ("") instead of null. Apparently in the previous version, activejdbc was understanding that either null or "" meant to insert a new record. And now it's only inserting when id is null. Can this be it? – ccarvalho Sep 26 '16 at 21:13
  • The same logic is still true. Please, take a look at how it decides to update or insert: https://github.com/javalite/activejdbc/blob/365a98e8feb704f65c78f5a7416a02a9592b9a02/activejdbc/src/main/java/org/javalite/activejdbc/Model.java#L2583-L2583 – ipolevoy Sep 26 '16 at 21:49