1.@Entity 標識實體
2.@Table (name = "tableName") //指定物理表
@Table(name="tbl_sky",
uniqueConstraints = {@UniqueConstraint(columnNames={"month", "day"})}//唯一性約束
)
3.@Embeddable 被聲明的類可以嵌入其他實體中
public class Address {
private String street1;//persistent
public String getStreet1() { return street1; }
public void setStreet1() { this.street1 = street1; }
private hashCode; //not persistent
}
@Embedded 在實體中嵌入一個類型:常用的像名字,地址之類的
另,使用@AttributeOverrides標識覆蓋原類中的屬性取值,因為原實體可能引用的是其他字段。
@Embedded
@AttributeOverrides( {
@AttributeOverride(name="iso2", column = @Column(name="bornIso2") ),
@AttributeOverride(name="name", column = @Column(name="bornCountryName") )
} )
Country bornIn;
例子:
@Entity
class User {
@EmbeddedId
@AttributeOverride(name="firstName", column=@Column(name="fld_firstname")
UserId id;
Integer age;
}
@Embeddable
class UserId implements Serializable {//此處Serializable是必須的
String firstName;
String lastName;
}
4.@Access(AcessType.PROPERTY)
必須定義getter/setter方法才能實現持久化
還有另一種取值:AcessType.FILED,可以不定義getter/setter方法,也能實現持久化
此annotation也可以定義字段。
5.主鍵:
A.單鍵
@Id
@GeneratedValue (generator = "identity")
@GenericGenerator (name = "identity", strategy = "identity")
或者
@javax.persistence.SequenceGenerator(
name="SEQ_STORE",
sequenceName="my_sequence")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_STORE")
其中:
strategy取值為:
AUTO - either identity column, sequence or table depending on the underlying DB
TABLE - table holding the id
IDENTITY - identity column
SEQUENCE - sequence
B.復合組鍵
@Entity
class Customer {
@EmbeddedId CustomerId id;
boolean preferredCustomer;
@MapsId("userId")//user.id與customerId.userId 使用相同的值
@JoinColumns({
@JoinColumn(name="userfirstname_fk", referencedColumnName="firstName"),
@JoinColumn(name="userlastname_fk", referencedColumnName="lastName")
})
@OneToOne User user;
}
@Embeddable
class CustomerId implements Serializable {
UserId userId;
String customerNumber;
}
@Entity
class User {
@EmbeddedId UserId id;
Integer age;
}
@Embeddable
class UserId implements Serializable {
String firstName;
String lastName;
}
6.字段設置:
@Column(
name="columnName";
boolean un(2)ique() default false;
boolean nu(3)llable() default true;
boolean in(4)sertable() default true;
boolean up(5)datable() default true;
String col(6)umnDefinition() default "";
String tab(7)le() default "";
int length(8)() default 255;
int precis(9)ion() default 0; // decimal precision
int scale((10)) default 0; // decimal scale
@Transient 非持久化字段
@Basic 持久化字段
@Basic(fetch = FetchType.LAZY) basic 用于定義property的fetch屬性
@Enumerated(EnumType.STRING) 標識enum persisted as String in database
@Lob blob clob字段
@Formula("obj_length * obj_height * obj_width")//自定義輸出
public long getObjectVolume()
7.Mapping關系
A.一對多或者一對一:
@OneToOne(cascade = CascadeType.ALL) 一對一關系,級聯關系為all
@PrimaryKeyJoinColumn或者
指定關聯外鍵
@JoinColumn(name="passport_fk")
Passport passport,
一對一的另一端只需@OneToOne(mappedBy = "passport"),passport為前一個實體聲明的名字
@OneToMany(fetch = FetchType.LAZY , mappedBy = "adProduct")
@Cascade(value = {CascadeType.ALL,CascadeType.DELETE_ORPHAN})
@OrderBy(value = "id") //排序
B.多對一:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="adPosition_id",nullable=false)
8.Fetch and Lazy
Annotations | Lazy | Fetch |
@[One|Many]ToOne](fetch=FetchType.LAZY) |
@LazyToOne(PROXY) |
@Fetch(SELECT) |
@[One|Many]ToOne](fetch=FetchType.EAGER) |
@LazyToOne(FALSE) |
@Fetch(JOIN) |
@ManyTo[One|Many](fetch=FetchType.LAZY) |
@LazyCollection(TRUE) |
@Fetch(SELECT) |
@ManyTo[One|Many](fetch=FetchType.EAGER) |
@LazyCollection(FALSE) |
@Fetch(JOIN) |
9.Cascade
10.緩存
緩存的注釋寫法如下,加在Entity的java類上:
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
緩存的方式有四種,分別為:
- CacheConcurrencyStrategy.NONE
- CacheConcurrencyStrategy.READ_ONLY,只讀模式,在此模式下,如果對數據進行更新操作,會有異常;
- CacheConcurrencyStrategy.READ_WRITE,讀寫模式在更新緩存的時候會把緩存里面的數據換成一個鎖,其它事務如果去取相應的緩存數據,發現被鎖了直接就去數據庫查詢;
- CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,不嚴格的讀寫模式則不會對緩存數據加鎖;
- CacheConcurrencyStrategy.TRANSACTIONAL,事務模式指緩存支持事務,當事務回滾時,緩存也能回滾,只支持JTA環境。
11.No-Annotation 字段:
If the property is of a single type, it is mapped as @Basic
Otherwise, if the type of the property is annotated as @Embeddable, it is mapped as @Embedded
Otherwise, if the type of the property is Serializable, it is mapped as @Basic in a column holding the object in its serialized version
Otherwise, if the type of the property is java.sql.Clob or java.sql.Blob, it is mapped as @Lob with the appropriate LobType