Model

Play 1.5.x

Model.action – Queries

Query query = JPA.em().createQuery("jpql_query");
Access the persistence manager

Post post = Post.findById(id);
List posts = Post.findAll();
Finder methods

post.save();
Save the object to the persistent store

boolean post.validateAndSave();
true if object validates and saved, see validation annotations

List posts = Post.all().from(50).fetch(100);
Read records 50 to 100, if any

Post.find("select p from Post p, Comment c where c.post
= p and c.subject like ?", "%hop%");
Parametrized lookup using a join

long userCount = Post.count("author=?", connectedUser);
long postCount = Post.count();
Counting records

JPAPlugin.startTx(boolean readonly);
JPAPlugin.closeTx(boolean rollback);
Custom transaction control methods

JPA.setRollbackOnly();
Forces a transaction rollback

Model – Basics

@Entity(name="sql_tbl") public class Post extends Model
Specifies that the class is persistent

@Embedded
Defines this field as being embedded

@EmbeddedId
Defines this field as being (part of) the identity for the class, and being embedded into this class

@Embeddable
Specifies that the class is persistent embedded in another persistent class

@MappedSuperclass
Specifies that this class contains persistent information to be mapped in a subclass

Model – Generators

@GeneratedValue(strategy = [NONE, TABLE, SEQUENCE,
IDENTITY, AUTO])
Used to generate auto indexes

@SequenceGenerator
Defines a generator of values using sequences in the datastore for use with persistent entities

@TableGenerator
Defines a generator of sequences using a table in the datastore for use with persistent entities

Model – Relational mapping

@Table(name="sql_tbl", catalog="", schema="")
Defines the table where this class will be stored

@Id
Defines this field as being (part of) the identity for the class

@Version
Defines this field as storing the version for the class

@Basic
Defines this field as being persistent, can be omitted

@Transient
Defines this field as being transient (not persisted)

@Lob(fetch=[LAZY, EAGER], type=[BLOB,CLOB])
Defines this field as being stored as a large object

@UniqueConstraint(primary=false, String columns[])
Used to define secondary indexes

@Temporal(DATE,TIME,TIMESTAMP)
Should only be used on java.util.Date and Calendar fields

@Enumerated(ORDINAL, STRING)
Defines this field as storing an enumerated class

@Column(name="sql_column_name")
Defines a table column name that is different to the field name

Model – Callbacks

Convenient ways to implement database-independent triggers.

@PrePersist
Defines this method as being a callback for pre-persist events

@PostPersist
Defines this method as being a callback for post-persist events

@PreRemove
Defines this method as being a callback for pre-remove events

@PostRemove
Defines this method as being a callback for post-remove events

@PreUpdate
Defines this method as being a callback for pre-update events

@PostLoad
Defines this method as being a callback for post-load events

Model – Relations

@OneToOne(entity, fetch=[LAZY,EAGER], nullable=true)
Defines this field as being a 1-1 relation with another persistent entity

@OneToMany(mappedBy="remote_attribute")
Defines this field as being a 1-N relation with other persistent entities

@ManyToMany(cascade=[ALL, PERSIST, MERGE,
REMOVE, REFRESH, DETACH])
Defines this field as being a M-N relation with other persistent entities

@ManyToOne
Defines this field as being a N-1 relation with another persistent entity

@JoinColumn(name = "id_connector")
Defines a column for joining to either a join table or foreign key relation.

@JoinTable(name = "nm_table", joinColumns =
{ @JoinColumn(name = "id_coupon", nullable = false) },
inverseJoinColumns = { @JoinColumn(name =
"id_campaign", nullable = false) })
Used to map ManyToMany relationships

Model – JPA Queries

@NamedQuery(name="q1", "jpql_query");
Defines a named JPQL query to use in the persistence unit

@NamedNativeQuery(name="q2","sql_query")
Defines a native SQL query for use in the persistence unit

@SqlResultSetMapping
Used to map a native SQL query result to the object model

This is only a subset of the JPA2 annotations, Hibernate also has its own non standard set.