Hibernate注解

 

2.2. Mapping with JPA

2.2.1. Marking a POJO as persistent entity实体注解

@Entity
public class Flight implements Serializable {
    Long id;

    @Id
    public Long getId() { return id; }

    public void setId(Long id) { this.id = id; }
}

@Entity declares the class as an entity (i.e. a persistent POJO class), @Id declares the identifier property of this entity. The other mapping declarations are implicit. The class Flight is mapped to the Flight table, using the column id as its primary key column.

@Entity声明一个实体类。

@Id声明该类的主键

2.2.1.1. Defining the table

@Table is set at the class level; it allows you to define the table, catalog, and schema names for your entity mapping. If no @Table is defined the default values are used: the unqualified class name of the entity.

@Table声明在类级别。用来定义表名,如果不声明,默认类名即为表名。

@Entity
@Table(name="tbl_sky")
public class Sky implements Serializable {
   ...
}  

2.2.2. Mapping simple properties通用属性注解

2.2.2.1. Declaring basic property mappings

Every non static non transient property (field or method depending on the access type) of an entity is considered persistent, unless you annotate it as @Transient.

任务非静态非临时变量都会被持久化,除非声明为@Transient

public transient int counter; //transient property

private String firstname; //persistent property

@Transient
String getLengthInMeter() { ... } //transient property

String getName() {... } // persistent property

@Basic
int getLength() { ... } // persistent property

@Basic(fetch = FetchType.LAZY)
String getDetailedComment() { ... } // persistent property

@Temporal(TemporalType.TIME)
java.util.Date getDepartureTime() { ... } // persistent property           

@Enumerated(EnumType.STRING)
Starred getNote() { ... } //enum persisted as String in database
@Lob
public String getFullText() {     return fullText; } @Lob  public byte[] getFullCode() {     return fullCode; }

@Transient, will be ignored by the entity manager。不会被持久化。

fetch = FetchType.LAZY 延迟加载。The detailedComment property value will be lazily fetched from the database once a lazy property of the entity is accessed for the first time.

Temporal data can have DATE, TIME, or TIMESTAMP precision (ie the actual date, only the time, or both). Use the @Temporal annotation to fine tune that.

@Lob indicates that the property should be persisted in a Blob or a Clob depending on the property type: java.sql.Clob, Character[], char[] and java.lang.String will be persisted in a Clob. java.sql.Blob, Byte[], byte[] and serializable type will be persisted in a Blob.

2.2.3. Mapping identifier properties主键注解

The @Id annotation lets you define which property is the identifier of your entity. This property can be set by the application itself or be generated by Hibernate (preferred). You can define the identifier generation strategy thanks to the @GeneratedValue annotation.

@Id定义主键

@GeneratedValue定义主键生成策略

2.2.3.1. Generating the identifier property

JPA defines five types of identifier generation strategies:五种主键生成策略

  • AUTO - either identity column, sequence or table depending on the underlying DB

  • TABLE - table holding the id

  • IDENTITY - identity column

  • SEQUENCE - sequence

  • identity copy - the identity is copied from another entity

 

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
public Integer getId() { ... }  

 

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() { ... }     

 

 

 

 

2.2.5. Mapping entity associations/relationships关系映射注解

 

 

官网:http://hibernate.org/

说明文档:

英文:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

中文:http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/

转载于:https://www.cnblogs.com/sunilsun/p/4766008.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值