Model
We can have our entity beans extend io.ebean.Model
. They then have
save()
and delete()
methods.
Customer extends Model ...
package org.example.domain;
import jakarata.persistence.Id;
import jakarata.persistence.Entity;
@Entity
public class Customer extends Model {
@Id
long id;
String name;
// getters and setters
}
package org.example.domain
import jakarata.persistence.Entity
import jakarata.persistence.Id
@Entity
class Customer : Model() {
@Id
var id: Long = 0
var name: String? = null
}
Now we can just save()
and delete()
customer.
Note that it would be nice if we have a constructor that takes name. We will do that shortly and that means we can make name a non-nullable property.
Model.save()
Customer customer = new Customer();
customer.setName("Hello world");
customer.save();
customer.delete();
val customer = Customer()
customer.name = "Hello entity bean"
customer.save()
customer.delete();
Note that save()
and delete()
use the default database
.
We obtain the default database via DB.getDefault().