[ebean] Setting default values only for new entities

874 views
Skip to first unread message

Tibor

unread,
May 21, 2010, 1:09:04 PM5/21/10
to Ebean ORM
Hi!

I have an entity class and I would need to generate a new UUID for new
entities but I don't want to create it in the constructor since it
would be a waste of time to generate new UUID's for already existing
entities for which the UUID value will be overwritten with the stored
value at loading.

What is the best strategy for these kinds of tasks? Is it possible to
annotate a function so that it will be a callback method before an
entity gets saved (like the PrePersist annotation in JPA), so that it
can be checked if it's still null and if so, set it to a UUID?

Sorry if it's a common thing (it seems so), I just didn't know where
to find the answer.

Thanks,
Tibor

edge

unread,
May 21, 2010, 1:46:07 PM5/21/10
to Ebean ORM
You can implement the interface
com.avaje.ebean.event.BeanPersistController - there is a preInsert
method so you can do something like

public boolean preInsert(BeanPersistRequest<?> request) {
final MyObject myObject = (MyObject) request.getBean();
myObject.setUUID(generateUUID());
}

I'm not sure if we support the JPA type lifecycle annotations e.g.
@PreInsert - I remember there was a debate about it e.g.
http://www.avaje.org/topic-4.html but Rob may have implemented since
then. Personally I think the BeanController is much cleaner e.g. if
you're using IoC then you can set up all the stuff in your bean
controller and not clutter up your domain objects.

BTW to use your BeanController you add it to list
EbeanConfig.persistController

Eddie

Rob Bygrave

unread,
May 21, 2010, 7:30:10 PM5/21/10
to eb...@googlegroups.com
Ebean detects that your Id is a UUID... and automatically assigns a UUID generator for you... and will set the UUID value on insert when the id is null.

So I'd say the best strategy is to do nothing and let Ebean assign the UUID value automatically (when the value is null).


Cheers, Rob.

Rob Bygrave

unread,
May 21, 2010, 9:21:17 PM5/21/10
to eb...@googlegroups.com
So for example:

@Entity
@Table(...)
class Customer {

  @Id
  UUID id;
  ...
}


Ebean knows that the @Id type is UUID and automatically assigns a UUID Generator to this bean type.  When a new Customer is saved and it's id property is null then Ebean will get a new UUID and set it.


Now in terms of what I think is best practice with Ebean I personally would generally not use the JPA @Generator annotations etc with the idea that I'd want to make the application as portable as possible across different DB's.

Most DB's either support DB Identity or DB Sequences. DB2 and H2 support both but even then there will be a 'default' or 'preferred' mechanism (Identity or Sequence and you should be able to specify which mechanism that is).

So depending on your DatabasePlatform Ebean will do the 'right thing' or in the case of DB2 or H2 the 'default/preferred' thing. Now in terms of DB sequences the sensible thing to do is use a naming convention based on the table/primary key column.  So that means that I would only use the JPA Generator annotations when I needed to explicitly use a sequence that did not match the naming convention (and even in that case you'd have to question whether that is a good thing to do longer term - you could also use NamingConvention to handle this case if you like).

Summary:
--------------
Personally I don't use any of the JPA Generator annotations and use the DatabasePlatform and NamingConvention to do the right thing. 


Cheers, Rob.
Reply all
Reply to author
Forward
0 new messages