Java Reference
In-Depth Information
This will create an Ebean server connected to the default data source, managing all entities found in
the
models
package.
Now it's time to transform your
Book
class to a valid Ebean entity. You can do this by making the
Book
class extend the
play.db.ebean.Model
superclass to have access to Play's built-in Ebean helper, as
illustrated in Listing 8-15.
Listing 8-15. Transforming the Book Class to a Valid Ebean Entity
1. package models;
2.
3. import java.util.*;
4. import play.db.ebean.*;
5. import play.data.validation.Constraints.*;
6.
7. import javax.persistence.*;
8.
9. @Entity
10. public class Book extends Model {
11. @Id
12. public Long id;
13. @Required
14. public String label;
15.
16. public static Finder<Long,Book> find = new Finder(
17. Long.class, Book.class
18. );
19.
20.
21. public static List<Book> all() {
22. return find.all();
23. }
24.
25. public static void create(Book book) {
26. book.save();
27. }
28. public static void update(Long id, Book book) {
29. book.update(id);
30. }
31.
32. public static void delete(Long id) {
33. find.ref(id).delete();
34. }
35.
36.
37. }
Line 13
: Line 13 adds the persistence annotation.
Lines 16 to 18
: These lines create a finder helper called
find
to initiate queries.
Lines 21 to 34
: These lines implement the CRUD operations. For instance, when you
call the
create()
action, Ebean translates the
save()
method call into one or more
SQL
INSERT
statements that store a new record in a database table using SQL.