Save/Delete
example
Save will either insert or update the bean depending on its state.
Order order = new Order();
order.setOrderDate(new Date());
...
// insert the order
Ebean.save(order);
If the bean was fetched it will be updated...
Order order = Ebean.find(Order.class, 12);
order.setStatus("SHIPPED");
...
// update the order
Ebean.save(order);
example
Save and Delete will CASCADE based on the CascadeType
specificed on the associated @OneToMany, @OneToOne etc annotation.
By default save and delete will not cascade so you need to
specify a cascade type (such as the one on details below) for
save() or delete() to cascade.
...
// the Entity bean with its Mapping Annotations
...
@Entity
@Table(name="or_order")
public class Order {
...
// no cascading
@ManyToOne
Customer customer;
// save and delete cascaded
@OneToMany(cascade=CascadeType.ALL)
List<OrderDetail> details;
...
Note the @OneToMany(cascade=CascadeType.ALL) on
the details property. This means save() and delete() will
both casade from order to its order details.
...
// save the order ... will cascade also saving the order details
Ebean.save(order);
example
Delete is very similar to save. Just call Ebean.delete();
...
Order order = Ebean.find(Order.class, 12);
// delete the order
// ... this will cascade also deleting the order details
// ... with either CascadeType.ALL or CascadeType.REMOVE
Ebean.delete(order);