by Eabin 23 Feb 11:51
Insert ID

Hi rob,

i'm using the 1.0.3a you sent me (many thanks for that fast response!), and was just confronted by the issue of wanting to retrieve the insertion id of a new object. obviously the field marked @Id is not updated automatically. Is there an easy way to get the new id?

The behaviour I really would like to see is that Ebean.save() returns a proxified copy of the new object.

keep it up,
-erwin

23 Feb 12:36
by imario

Which Database do you use?
e.g. I had this problem with Oracle 9 which does not support retrieving the "generated" key.
Avaje correctly creates the insert statement using the database sequence, but later on is not able to refetch it.

A workaround is to use Avajes Id generators.

Here it worked by:

* annoting the field with e.g.
@SequenceGenerator(name="SEQ_NAME", sequenceName="SEQ_NAME")
@GeneratedValue(strategy=GenerationType.AUTO, generator="SEQ_NAME")

* adding two lines of configuration to ebean.properties

ebean.ora10.identityGeneration=generator
ebean.ora10.idgen.seq_name.class=com.avaje.ebean.server.idgen.DbSequence


That way, Avaje first gets the next id from the database, updated the entity and then issues the insert.
So, you do have another database roundtrip per insert, but then it works.

Hope this helps!
Ciao,
Mario

23 Feb 12:48
by Eabin

thanks for the tip, i will try to use this as a workaround. i'm using hsqldb/mysql, both of which support fetching the generated id AFAIK.
anyway, native support for that would be nice ;)

23 Feb 13:06
by imario

Avaje supports generated ids already, its the database which might not support it.

As far as I can see from MySqlPlugin it should be able to get the generated id, though, for hsqldb Avaje has no support at all.

You need to carefully configure it in your ebean.properties.

I've hacked a HsqlDbPlugin together, but I am not sure if it is fully working.

I do not know how to configure it with ebean.properties only.

A rough outline is to

* set ebean.hsqldb-datasource-name.supportsGetGeneratedKeys=true

* ebean.hsqldb-datasource-name.namingconvention.sequence.nextvalprefix=(select next value for
* ebean.hsqldb-datasource-name.namingconvention.sequence.nextvalsuffix=from hsql_dual)

Notice: I am not sure if the configuration is correct.

You also need the dummy table hsql_dual with exactly one row - thats the workaround I found in internet to make the sequence work in hsqldb.

23 Feb 13:31
by Eabin

thanks.

it seems that hsqldb will get support for this feature with the upcoming 1.9/2.0 release. i always thought that this was already supported.

23 Feb 21:02
by Rob

In case you are not aware:

The author of H2, Thomas Mueller, is also the original developer of Hypersonic SQL.

... so you may want to look at H2 http://www.h2database.com (if that is an option).

23 Feb 22:47
by Eabin

i didn't know that, thanks for the tip. it even has a system tray icon under linux, that's new :)

now what would be the best way to copy data between h2 and mysql? i used a jdbc tool with hsqldb, but that was a bit of a pain to use. can you recommend anything?

24 Feb 05:48
by Rob

what would be the best way to copy data between h2 and mysql?

Ummm... not sure really. For me... I'd probably use Ebean with its MapBean feature... assuming the tables/columns are the same name etc.

This code will select all the rows from a sourceDb ... and insert them into a destDb (you'd need to extent it to merge rather than insert):


final EbeanServer destServer = Ebean.getServer("destDb");
EbeanServer sourceServer = Ebean.getServer("sourceDb");

SqlQuery sqlQuery = sourceServer.createSqlQuery();
sqlQuery.setQuery("select * from the_table");
sqlQuery.setBaseTable("the_table");

destServer.beginTransaction();
try {
sqlQuery.setListener(new SqlQueryListener() {
public void process(MapBean row){
// forces an insert
row.resetOldValues();
// insert into destDb
destServer.save(row);
}
});
destServer.commitTransaction();
} finally {
destServer.endTransaction();
}

24 Feb 09:51
by Eabin

sounds nice, but optimally the code would copy the schema too. i was playing around a bit with h2's run-script feature, trying to run a mysqldump file in all of the available compatibility modes - but no luck :(

24 Feb 15:44
by Eabin

i had to modify your code a bit to get it working. row.resetOldValues() is not sufficient (something about isReference() being true?), but when i copy the bean to a new MapBean() everything is going fine.

except for this little bug in ExeUpdateSql.java:parseUpdate (Line: 175)

when i try to execute "delete from table" pos[2] is 0, and sql.substring will crash. i work around this by executing "delete from table ;".

24 Feb 22:06
by Rob

Ok. logged Bug 81 for the ... ExeUpdateSql.java:parseUpdate (Line: 175)

Create a New Topic

Title:
Body:
 
Introduction User Guide (pdf) Install/Configure Public JavaDoc Whitepapers
General Database Specific Byte Code Deployment Annotations Features
Top Bugs Top Enhancements
woResponse