JPA Annotation Reference

Table 1-1 JPA Annotations by Category

CategoryDescriptionAnnotations

Entity

By default, TopLink JPA assumes that a Java class is non-persistent and not eligible for JPA services unless it is decorated with this annotation.

Use this annotation to designate a plain old Java object (POJO) class as an entity so that you can use it with JPA services.

You must designate a class as a JPA entity (either using this annotation or the orm.xml file) before you can use the class with JPA services.

@Entity


Database Schema Attributes

By default, TopLink JPA assumes that an entity's name corresponds to a database table of the same name and that an entity's data member names correspond to database columns with the same names.

Use these annotations to override this default behavior and fine-tune the relationship between your object model and data model.

@Table

@SecondaryTable

@SecondaryTables

@Column

@JoinColumn

@JoinColumns

@PrimaryKeyJoinColumn

@PrimaryKeyJoinColumns

@JoinTable

@UniqueConstraint


Identity

By default, TopLink JPA assumes that each entity must have at least one field or property that serves as a primary key.

Use these annotations to specify one of the following:

  • one @Id

  • multiple @Id and an @IdClass

  • one @EmbeddedId

You can also use these annotations to fine-tune how your database maintains the identity of your entities.

@Id

@IdClass

@EmbeddedId

@GeneratedValue

@SequenceGenerator

@TableGenerator


Direct Mappings

By default, TopLink JPA automatically configures a Basic mapping for most Java primitive types, wrappers of the primitive types, and enums.

Use these annotations to fine-tune how your database implements these mappings.

@Basic

@Enumerated

@Temporal

@Lob

@Transient


Relationship Mappings

TopLink JPA requires that you map relationships explicitly, but TopLink allows some defaulting.

Use these annotations to specify the type and characteristics of entity relationships to fine-tune how your database implements these relationships.

@OneToOne

@ManyToOne

@OneToMany

@ManyToMany

@MapKey

@OrderBy


Composition

Some objects cannot exist on their own, but can only be embedded within owning entities.

Use these annotations to specify objects that are embedded and to override how they are mapped in the owning entity's table.

@Embeddable

@Embedded

@AttributeOverride

@AttributeOverrides

@AssociationOverride

@AssociationOverrides


Inheritance

By default, TopLink JPA assumes that all persistent fields are defined by a single entity class.

Use these annotations if your entity class inherits some or all persistent fields from one or more superclasses.

@Inheritance

@DiscriminatorColumn

@DiscriminatorValue

@MappedSuperclass

@AssociationOverride

@AssociationOverrides

@AttributeOverride

@AttributeOverrides


Locking

By default, TopLink JPA assumes that the application is responsible for data consistency.

It is recommended that you use this annotation to enable TopLink JPA-managed optimistic locking.

@Version


Lifecycle Callback Events

By default, TopLink JPA handles all persistence operations.

Use these annotations to associate methods with JPA lifecycle events if you need to invoke custom logic at any point during the entity lifecycle. Figure 1-1 illustrates the relationship amongst these lifecycle events.

@PrePersist

@PostPersist

@PreRemove

@PostRemove

@PreUpdate

@PostUpdate

@PostLoad

@EntityListeners

@ExcludeDefaultListeners

@ExcludeSuperclassListeners


Entity Manager

In an application that uses TopLink JPA, you perform all persistence operations (create, read, update, and delete) using an instance of EntityManager.

Use these annotations to declare or inject an entity manager or entity manager factory.

@PersistenceUnit

@PersistenceUnits

@PersistenceContext

@PersistenceContexts

@PersistenceProperty


Queries

In an application that uses TopLink JPA, you can use an entity manager to create and execute queries dynamically or you can pre-define queries and execute them by name at run time.

Use these annotations to pre-define queries and manage their result sets.

@NamedQuery

@NamedQueries

@NamedNativeQuery

@NamedNativeQueries

@QueryHint

@ColumnResult

@EntityResult

@FieldResult

@SqlResultSetMapping

@SqlResultSetMappings



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值