public abstract class Model extends Object
By having your entity beans extend this it provides a 'Active Record' style programming model for Ebean users.
Note that there is a ebean-mocker project that enables you to use Mockito or similar tools to still mock out the underlying 'default EbeanServer' for testing purposes.
You may choose not use this Model mapped superclass if you don't like the 'Active Record' style or if you believe it 'pollutes' your entity beans.
You can use Dependency Injection like Guice or Spring to construct and wire a EbeanServer instance and have that same instance used with this Model and Finder. The way that works is that when the DI container creates the EbeanServer instance it can be registered with the Ebean singleton. In this way the EbeanServer instance can be injected as per normal Guice / Spring dependency injection and that same instance also used to support the Model and Finder active record style.
If you choose to use the Model mapped superclass you will probably also chose to additionally add
a Finder
as a public static field to complete the active record pattern and provide a
relatively nice clean way to write queries.
// Typically there is a common base model that has some
// common properties like the ones below
@MappedSuperclass
public class BaseModel extends Model {
@Id Long id;
@Version Long version;
@WhenCreated Timestamp whenCreated;
@WhenUpdated Timestamp whenUpdated;
...
// Extend the mappedSuperclass
@Entity @Table(name="o_account")
public class Customer extends BaseModel {
String name;
...
}
// Active record style ... save(), delete() etc
Customer customer = new Customer();
customer.setName("AC234");
// save() method inherited from Model
customer.save();
Constructor and Description |
---|
Model() |
Modifier and Type | Method and Description |
---|---|
static Database |
db()
Return the underlying 'default' Database.
|
static Database |
db(String server)
Return a named Database that is typically different to the default database.
|
boolean |
delete()
Delete this bean.
|
boolean |
delete(String server)
Perform a delete using this entity against the specified server.
|
boolean |
deletePermanent()
Delete a bean permanently without soft delete.
|
void |
flush()
Flush any batched changes to the database.
|
void |
insert()
Insert this entity.
|
void |
insert(String server)
Perform an insert using this entity against the specified server.
|
void |
markAsDirty()
Marks the entity bean as dirty.
|
void |
markPropertyUnset(String propertyName)
Mark the property as unset or 'not loaded'.
|
void |
refresh()
Refreshes this entity from the database.
|
void |
save()
Insert or update this entity depending on its state.
|
void |
update()
Update this entity.
|
void |
update(String server)
Perform an update using this entity against the specified server.
|
public Model()
public static Database db()
This provides full access to the API such as explicit transaction demarcation etc.
Example:
try (Transaction transaction = Customer.db().beginTransaction()) {
// turn off cascade persist for this transaction
transaction.setPersistCascade(false);
// extra control over jdbc batching for this transaction
transaction.setBatchGetGeneratedKeys(false);
transaction.setBatchMode(true);
transaction.setBatchSize(20);
Customer customer = new Customer();
customer.setName("Roberto");
customer.save();
Customer otherCustomer = new Customer();
otherCustomer.setName("Franko");
otherCustomer.save();
transaction.commit();
}
public static Database db(String server)
server
- The name of the Database. If this is null then the default Database is returned.public void markAsDirty()
This is used so that when a bean that is otherwise unmodified is updated the version property is updated.
An unmodified bean that is saved or updated is normally skipped and this marks the bean as dirty so that it is not skipped.
Customer customer = Customer.find.byId(id);
// mark the bean as dirty so that a save() or update() will
// increment the version property
customer.markAsDirty();
customer.save();
Database.markAsDirty(Object)
public void markPropertyUnset(String propertyName)
This would be used to specify a property that we did not wish to include in a stateless update.
// populate an entity bean from JSON or whatever
User user = ...;
// mark the email property as 'unset' so that it is not
// included in a 'stateless update'
user.markPropertyUnset("email");
user.update();
propertyName
- the name of the property on the bean to be marked as 'unset'public void save()
Ebean will detect if this is a new bean or a previously fetched bean and perform either an insert or an update based on that.
Database.save(Object)
public void flush()
When using JDBC batch flushing occurs automatically at commit() time or when the batch size is reached. This provides the ability to manually flush the batch.
public void update()
Database.update(Object)
public void insert()
Database.insert(Object)
public boolean delete()
This will return true if the bean was deleted successfully or JDBC batch is being used.
If there is no current transaction one will be created and committed for you automatically.
If the Bean does not have a version property (or loaded version property) and the bean does not exist then this returns false indicating that nothing was deleted. Note that, if JDBC batch mode is used then this always returns true.
Database.delete(Object)
public boolean deletePermanent()
This is used when the bean contains a @SoftDelete
property and we
want to perform a hard/permanent delete.
Database.deletePermanent(Object)
public void update(String server)
public void insert(String server)
public boolean delete(String server)
public void refresh()
Database.refresh(Object)
Copyright © 2019. All rights reserved.