标注 (Annotations) 目录

 
 
javax.persistence.Column:

POJO时注释的写法:

Example 1:
@Column(name="DESC"//表示数据库的字段//, //千万不要忘记逗号//nullable=false, length=512//表示数据库的字段的长度//)
public String getDescription() { return description; }

Example 2:
@Column(name="DESC",
         columnDefinition="CLOB NOT NULL",
         table="EMP_DETAIL")
@Lob
public String getDescription() { return description; }

Example 3:
@Column(name="ORDER_COST", updatable=false, precision=12, scale=2)
public BigDecimal getCost() { return cost; } 

基于Hibernate用标注(annotations)描述映射关系

实体类型用标注:@Entity

实体对应的表用标注:@Table

映射实体类中表示主键的属性的标注是:@Id

映射实体类中表示普通属性的标注是:@Basic

实体属性对应的列用表注:@Column

标注暂态属性(不映射到库中):@Transient

映射一对一关联关系的标注是:@OneToOne

映射多对一关联关系的标注是:@ManyToOne

映射多对多关系的标注是:@ManyToMany

描述单一外键列用标注:@JoinColumn

连接表用表注:@JoinTable

如果一个类型不是实体,但他是某个实体的组成部分,我们用标注:@Embeddable 

在描述继承关系时,用标注:@Inheritance,该标注的属性strategy 用来指定映射策略

代表双向关联关系时表示不维护表中外键关系的标注属性名是:mappedBy

代表级联操作的属性名是:cascade

使用乐观锁是我们通常会在实体类型中增加一整型属性,该属性用标注:@Version

描述主键生成机制的JPA标注是:@GeneratedValue

 

分类说明标注

Entity

(实体)

By default, a JPA persistence provider 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, a JPA persistence provider 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, a JPA persistence provider 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, a JPA persistence provider 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

A JPA persistence provider requires that you map relationships explicitly.

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

By default, a JPA persistence provider assumes that every entity is mapped to its own table.

Use these annotations to override this behavior for entities that are owned by other entities.

@Embeddable

@Embedded

@AttributeOverride

@AttributeOverrides

@AssociationOverride

@AssociationOverrides


Inheritance

By default, a JPA persistence provider 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, a JPA persistence provider assumes that the application is responsible for data consistency.

Use this annotation to enable JPA-managed optimistic locking (recommended).

@Version


Lifecycle Callback Events

By default, a JPA persistence provider handles all persistence operations.

Use these annotations to associate entity 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 a JPA persistence provider, you perform all persistence operations (create, read, update, and delete) using an instance of EntityManager.

Use these annotations to associate an entity with an entity manager and to customize the entity manager's environment.

@PersistenceUnit

@PersistenceUnits

@PersistenceContext

@PersistenceContexts

@PersistenceProperty


Queries

In an application that uses a JPA persistence provider, 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、付费专栏及课程。

余额充值