1 4 Annotating Persistence
1 4 Annotating Persistence
Dynamic proxy may need a paired interface and implementation (Web Services) JavaBeans may need a way to override getter/setter conventions (BeanInfo) Serialization may not need to serialize everything (transient) We may need to indicate that a member is being retired (@deprecated)
JPA is annotation-enabled
http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html
Entity / Locking
@Entity @Version
Identity
@Id @IdClass @EmbeddedId @GeneratedValue @SequenceGenerator @TableGenerator
Direct Mappings
@Basic @Enumerated @Temporal @Lob @Transient
Relationship Mappings
@OneToOne @OneToMany @ManyToOne @ManyToMany @MapKey @OrderBy
Queries
@NamedQuery @NamedQueries @NamedNativeQuery @NamedNativeQueries @QueryHint @ColumnResult @EntityResult @FieldResult @SqlResultSetMapping @SqlResultSetMappings
Composition
@Embeddable @Embedded @AttributeOverride @AttributeOverrides @AssociationOverride @AssociationOverrides
Inheritance
@Inheritance @DiscriminatorColumn @DiscriminatorValue @MappedSuperclass @AssociationOverride @AssociationOverrides @AttributeOverride @AttributeOverrides
Entity Manager
@PersistenceUnit @PersistenceUnits @PersistenceContext @PersistenceContexts @PersistenceProperty
Entity
@Entity public class User implements Serializable {
Entity
@Entity(name=USER) public class UserImpl extends UuidEntity implements User, Serializable {
u FROM USER)
Entity
@Entity public class User implements Serializable { @Id // field access private String id; public String getId() { return id; } public void setId(String value) { id = value; } }
Entity
@Entity public class User implements Serializable { private String id; @Id // property access public String getId() { return id; } public void setId(String value) { id = value; } }
Table
@Entity(name="USER") @Table(name="APP_USER") public class UserImpl extends UuidEntity implements Serializable, User {
name, catalog, schema, uniqueConstants Works well with Entity to isolate per database settings
Column
// ... public class User implements Serializable { @Column(length = 64) private String from_address; public String getFromAddress() {return from_address;} // ... }
name, unique, nullable, insertable, updatable, columnDefinition, table, length, precision, scale
Id
public class Message { @Id private Long id; }
Use @Column to specify a different name Primitive or wrapper type, String, Date (sql or util)
GeneratedValue
public class Message { @Id @GeneratedValue private Long id; // ... } public class Message { @Id @GeneratedValue(GenerationType.SEQUENCE) private Long id; // ... }
Version
public class UuidEntity implements Serializable { @Version private Timestamp last_update; // ... }
Basic
// ... @Basic(fetch=FetchType.LAZY) private List<Subscription> subscriptions; // ...
Default annotation for unannotated fields or properties fetch, optional FetchType = (EAGER, LAZY)
Basic
// ... @Basic(fetch=FetchType.LAZY) private List<Subscription> subscriptions; // ...
Java primitive types, wrappers of the primitive types, String, BigInteger, BigDecimal, util.Date, Calendar, sql.Date, Time, Timestamp, byte[], Byte[], char[], Character[], enums, and any other type that implements Serializable.
Transient
@Entity public class User { @Transient private String password1 = null; // ... } @Entity public class User implements Serializable { private transient String password1 = null; // ... }
OneToOne
@Entity @Table(name = "APP_MESSAGE") public class Message implements Serializable { @Id @GeneratedValue private Long id; @OneToOne(cascade = CascadeType.ALL) private Message message; // ... }
OneToMany
// ... public class User implements Serializable { @OneToMany(cascade = CascadeType.ALL) private List<Subscription> subscriptions; // ... public class UserImpl extends UuidEntity implements @OneToMany(cascade = CascadeType.ALL, mappedBy=user, targetEntity=SubscriptionImpl.class) private List<Subscription> subscriptions;
OneToMany
@Entity(name="USER") @Table(name="APP_USER") public class UserImpl extends UuidEntity implements @OneToMany(cascade = CascadeType.ALL, mappedBy=user, targetEntity=SubscriptionImpl.class) private List<Subscription> subscriptions;
(Can't change the One, because the the One side is usually a primary key!)
Important because application must keep relationships synchronized with any changes
OneToMany
@Entity(name="USER") @Table(name="APP_USER") public class UserImpl extends UuidEntity implements @OneToMany(cascade = CascadeType.ALL, mappedBy=user, targetEntity=SubscriptionImpl.class) private List<Subscription> subscriptions;
ManyToOne
// ... public class SubscriptionImpl extends UuidEntity @JoinColumn(nullable = false) @ManyToOne(targetEntity = UserImpl.class) private User user;
NamedQuery
@Entity @NamedQuery(name = "User.COUNT", query = "SELECT COUNT(*) FROM USER") public class User implements Serializable {
NamedQuery
@Entity @NamedQuery( name="findAllEmployees", query="SELECT * FROM EMPLOYEE WHERE MGR=1" hints={ @QueryHint(name=TopLinkQueryHints.REFRESH, value=HintValues.TRUE) } )
NamedQueries
@NamedQueries( { @NamedQuery(name = User.FIND_ALL, query = UserImpl.FIND_ALL_QUERY), @NamedQuery(name = User.FIND_BY_NAME, query = UserImpl.FIND_BY_NAME_QUERY), @NamedQuery(name = User.COUNT, query = UserImpl.COUNT_QUERY) }) public class UserImpl extends UuidEntity implements
Resources
Toplink JPA Annotation Reference
http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html
/docs/jpa-docs/toplink-jpa-annotations.pdf
http://jcp.org/en/jsr/detail?id=220 /docs/jpa-docs/ejb3_0-fr-spec-persistence.pdf
http://java.sun.com/developer/technicalArticles/J2EE/jpa/ /docs/jpa-docs/j2ee-jpa.pdf