JPA Entity

@Entity

@Table
@Table(name="employee",schema="HR",catalog="HR")

Table用來定義entity主表的name,catalog,schema…等屬性。

屬性說明:

  • name: 表名
  • catalog: 對應關係資料庫中的catalog
  • schema:對應關係資料庫中的schema

@Id(主鍵)
@GeneratedValue
JPA 持續性提供程序管理為實體主鍵提供的唯一標識符資料庫的主鍵生成器類型
@GeneratedValue(stategy=Generation.AUTO| TABLE | SEQUENCE | IDENTITY)
id值的獲取方式有四種:TABLE, SEQUENCE, IDENTITY, AUTO。
Oracle和DB2支持SEQUENCE
SQL Server和Sybase支持IDENTITY,
mysql支持AUTO,所有的資料庫都可以指定為AUTO。


@GeneratedValue(stategy=Generation.AUTO)
private int id;
 
@GeneratedValue(stategy=Generation.TABLE)
private int id;
__________________________________________
TableGenerator定義一個主鍵值生成器
@TableGenerator(name="Emp_Gen")
@Id@GeneratedValue(generator="Emp_Gen")
private int id;
__________________________________________
@TableGenerator(name="Emp_Gen",
                            table="ID_GEN",
                            pkColumnName="GEN_NAME",
                            valueColumnName="GEN_VAL")

@TableGenerator(name="Emp_Gen",
                            table="ID_GEN",
                            pkColumnName="GEN_NAME",
                            valueColumnName="GEN_VAL",
                            pkColumnValue="Add_Gen",
                            initialValue=10000)
@Id@GenerateValue(genarator="Add_Gen")
 SequenceGenerator定義一個主鍵值生成器
@GeneratedValue(stategy=Generation.SEQUENCE)
private int id;
@SequenceGenerator(name="Emp_Gen",sequenceName="Emp_Seq")
@Id@SequencedValue(generator="Emp_Gen")
private int id;
_____________________________________________________________

@GeneratedValue(stategy=Generation.IDENTITY)
______________________________________________________________________________
@Column
@Coulmn(name="e_id",insertable=false,updateable=false)
private int eId;
Column定義了映射到資料庫的欄位和屬性
· insertable: 是否允許插入 
· updatable: 是否允許更新 

@Temporal
@Temporal(TemporalType.DATE | TIME | DATESTAMP)
DATE:日期
TIME:時間
DATESTAMP:日期+時間

@Lob
@Lob
private byte[] picture;
(圖片檔)
??
public enum EmployeeType{
                                          FULL_TIME_EMPLOYEE,
                                          PART_TIME_EMPLOYEE,
                                          CONTRACT_EMPLOYEE}
@Enumerated
@Enumerated(EnumType.STRING | ORDINAL)
private EmployeeType emptype;

JPA2.0  ?
@Access
@Access(AccessType.FIELD | PROPERTY)
http://download.oracle.com/javaee/6/api/javax/persistence/Basic.html

@Transient   ----->non-persistent
指定實体的非持久field或property

-----------------------------------------------
關聯式--四種
@OneToOne
Employee(1)---------------------------ParkingSpace(1)
@OneToOne                                     @OneToOne(mappedBy="parking")
private ParkingSpace parking             private Employee employee

cascade = CascadeType.ALL  ?
http://www.lixiao.name/archives/learn-openjpa_chapter-5-metadata.html
-------------------------------------------------------------------------------------------
@OneToMany
@ManyToOne
Employee(n)---------------------------Department(1)
@ManyToOne                                   @OneToMany (mappedBy="depart)
@JoinColumn(name = "DEPT_ID")
<foreign key>
private Department depart              private Collection<Employee> employee 

@ManyToMany
Employee(n)------------------------------Project(n)
@ManyToMany                                     @ManyToMany(mappedBy="pro")
 @JoinTable(name = "EMP_PROJ",
    joinColumns =
    @JoinColumn(name = "EMP_ID"),
    inverseJoinColumns =
   @JoinColumn(name = "PROJ_ID"))
private Collection<Project>pro             private Collection<Employee>employee         
____________________________________________
@JoinColumn
@JoinTable

___________________________________________________________________________________

@Embeddable
@Embedded

@Entity
@Table(name="employee")        employee
@id                                               id
private int id;                                name
private String name;                     salary
private long salary;
-----------------------------------------------------
@Embeddable
public class Address {
    private String street;
    private String city;
    private String state;
    private String zip;
}

@Entity                                         employee
@Table(name="employee")              id
@id                                                  name
private int id;                                   salary
private String name;                        street
private long salary;                          city
@Embedded                                    state     >PROVINEE
private Address address                   zip        >POSTAL_CO

_________________________________________________________________________________  
   
@AttributeOverrides
@AttributeOverride
@AttributeOverrides({@AttributeOverride(name=state,column=@Column(name="PROVINEE")),
                                @AttributeOverride(name=zip,column=@Column(name="POSTAL_CO"))
})
-------------------------------------------------------------------------------------------------------------
JPA2.0增加了兩個支持新的集合映射的註解:@ElementCollection 和 @CollectionTable。使用@ElementCollection註解指定集合的嵌入式物件
   這些集合是獨立存儲在集合表中的
使用@CollectionTable註解指定集合表的詳細信息,如它包含的欄位。
@ElementCollection
@ CollectionTable
-------------------------------
@Embeddable
public class FeatureType{
private int daysTaken;
}
@Entity  
public class Vehicle{  
  @Id int id;  
  @ElementCollection  
  @CollectionTable(name="VEH_OPTNS")  

 Collection<FeatureType>optionalFeatures; 
第一對註解@ElementCollection 和@CollectionTable指定FeatureType值存儲在VEH_OPTNS集合表中
Vehicle     VEH_OPTNS
id              Vehicle_id
                 daysTaken
_________________________________________________________________________
@Order By(ASC--[小-->大]/DESC[大-->小])--不包含支持維護一個持久性
JPA2.0
@OrderColumn--持久化提供者需要使用分離的欄位來維護,預設值是按物件的主鍵升序排序
------------------------------------------
JPA2.0
@MayKeyColumn
@MayKeyEnumerated
@MayKey
Comments