mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
134 lines
3.2 KiB
HTML
134 lines
3.2 KiB
HTML
<html>
|
|
<head>
|
|
<title>Ebean API</title>
|
|
</head>
|
|
<body BGCOLOR="#ffffff">
|
|
Ebean Object Relational Mapping (start at
|
|
<a href='com/avaje/ebean/EbeanServer.html'>EbeanServer</a> or <a href='com/avaje/ebean/Ebean.html'>Ebean</a>).
|
|
|
|
|
|
<h3>Ebean</h3>
|
|
<p>
|
|
Provides the main API for fetching and persisting beans with Ebean.
|
|
</p>
|
|
<p>
|
|
For a full description of the query language refer to <a href="com/avaje/ebean/Query.html">Query</a>.
|
|
</p>
|
|
<p>
|
|
|
|
</p>
|
|
<div id="overviewexamples">
|
|
<h3>
|
|
EXAMPLE 1: Simple fetch
|
|
</h3>
|
|
<pre>{@code
|
|
// fetch order 10
|
|
Order order = Ebean.find(Order.class, 10);
|
|
}</pre>
|
|
|
|
<h3>
|
|
EXAMPLE 2: Fetch an Object with associations
|
|
</h3>
|
|
<pre>{@code
|
|
// fetch Customer 7 including their billing and shipping addresses
|
|
Customer customer = Ebean.find(Customer.class)
|
|
.fetch("billingAddress");
|
|
.fetch("shippingAddress");
|
|
.setId(7)
|
|
.findOne();
|
|
|
|
|
|
Address billAddr = customer.getBillingAddress();
|
|
Address shipAddr = customer.getShippingAddress();
|
|
}</pre>
|
|
|
|
<h3>
|
|
EXAMPLE 3: Fetch a list of Objects with associations
|
|
</h3>
|
|
<pre>{@code
|
|
// Note: This example shows a "Partial Object".
|
|
// For the product objects associated with the
|
|
// order details only the product id and name is
|
|
// fetched (the product objects are partially populated).
|
|
|
|
// fetch orders for customer.id = 2
|
|
List<Order> orderList = Ebean.find(Order.class);
|
|
.fetch("customer")
|
|
.fetch("customer.shippingAddress")
|
|
.fetch("details")
|
|
.fetch("details.product","name")
|
|
.where().eq("customer.id",2)
|
|
.findList();
|
|
|
|
|
|
// Note: Only the product id and name is fetched for the
|
|
// product details. This is referred to as a
|
|
// "Partial Object" (one that is partially populated).
|
|
|
|
|
|
// code that traverses the object graph...
|
|
|
|
Order order = orderList.get(0);
|
|
Customer customer = order.getCustomer();
|
|
Address shipAddr = customer.getShippingAddress();
|
|
|
|
List<OrderDetail> details = order.getDetails();
|
|
OrderDetail detail = details.get(0);
|
|
Product product = detail.getProduct();
|
|
String productName = product.getName();
|
|
|
|
}</pre>
|
|
|
|
<h3>
|
|
EXAMPLE 4: Create and save an Order
|
|
</h3>
|
|
<pre>{@code
|
|
// get a Customer reference so we don't hit the database
|
|
Customer custRef = Ebean.getReference(Customer.class, 7);
|
|
|
|
// create a new Order object
|
|
Order newOrder = new Order();
|
|
newOrder.setStatus(Order.Status.NEW);
|
|
newOrder.setCustomer(custRef);
|
|
|
|
ArrayList orderLines = new ArrayList();
|
|
newOrder.setLines(orderLines);
|
|
...
|
|
|
|
// add a line to the order
|
|
Product prodRef = Ebean.getReference(Product.class, 41);
|
|
OrderLine line = new OrderLine();
|
|
line.setProduct(prodRef);
|
|
line.setQuantity(10);
|
|
orderLines.add(line);
|
|
...
|
|
|
|
// save the order and its lines in a single transaction
|
|
// NB: assumes CascadeType.PERSIST is set on the order lines association
|
|
Ebean.save(newOrder);
|
|
|
|
}</pre>
|
|
|
|
<h3>
|
|
EXAMPLE 5: Use another database
|
|
</h3>
|
|
<pre>{@code
|
|
// Get access to the Human Resources EbeanServer/Database
|
|
EbeanServer hrServer = Ebean.getServer("HR");
|
|
|
|
|
|
// fetch contact 3 from the HR database
|
|
Contact contact = hrServer.find(Contact.class, 3);
|
|
|
|
contact.setStatus(Contact.Status.INACTIVE);
|
|
...
|
|
|
|
// save the contact back to the HR database
|
|
hrServer.save(contact);
|
|
}</pre>
|
|
</div>
|
|
|
|
|
|
</body>
|
|
</html>
|