Core API (see Database, DB and Query).

Database

Provides the main API for fetching and persisting beans. We can obtain the "default database" via DB.getDefault() or just use methods on DB.

{@code
// EXAMPLE 1: Simple fetch.
//========================

// fetch order 10
Order order = DB.find(Order.class, 10);



// EXAMPLE 2: Fetch an Object with associations
//=============================================

// fetch Customer 7 including their billing and shipping addresses
Customer customer =
	DB.find(Customer.class)
		.setId(7)
		.fetch("billingAddress")
		.fetch("shippingAddress")
		.findOne();

Address billAddr = customer.getBillingAddress();
Address shipAddr = customer.getShippingAddress();




// EXAMPLE 3: Create and save an Order
//=====================================

// get a Customer reference so we don't hit the database
Customer customer = DB.getReference(Customer.class, 7);

// create a new Order object
Order newOrder = new Order();
newOrder.setStatus(Order.Status.NEW);
newOrder.setCustomer(customer);
...


DB.save(newOrder);



// EXAMPLE 4: Use another database
//=================================

// Get access to the Human Resources EbeanServer/Database
Database hrDatabase = DB.byName("HR");


Contact contact = hrDatabase.find(Contact.class, 42);

contact.setStatus(Contact.Status.INACTIVE);
...

// save the contact back to the HR database
hrDatabase.save(contact);
}