/**      

* @author liuguangyi  

* @content ejb3注解的API定义在javax.persistence.*包里面。  

*   

* 注释说明:  

* @Entity —— 将一个类声明为一个实体bean(即一个持久化POJO)  

              String name

          The name of an entity.

* @Id —— 注解声明了该实体bean的标识属性(对应表中的主键)。  

              Specifies the primary key property or field of an entity.

 

            Example:

 

            @Id

              public Long getId() { return id; }

* @Table —— 注解声明了该实体bean映射指定的表(table,目录(catalog)和schema的名字  

              If no Table annotation is specified for an entity class, the default values apply.

 

            Example:

 

           @Entity

           @Table(name="CUST", schema="RECORDS")

           public class Customer { ... }

 

              属性:

              String catalog   (Optional) The catalog of the table.

             String name  (Optional) The name of the table.

             String schema  (Optional) The schema of the table.

             UniqueConstraint[]  uniqueConstraints

         (Optional) Unique constraints that are to be placed on the table.

      

              public @interface UniqueConstraint

              This annotation is used to specify that a unique constraint is to be included in the generated DDL for a primary or secondary table.

              属性:

              String[] columnNames

          (Required) An array of the column names that make up the constraint.

 

               Example:

           @Entity

           @Table(

               name="EMPLOYEE",

               uniqueConstraints=

                   @UniqueConstraint(columnNames={"EMP_ID", "EMP_NAME"})

           )

             public class Employee { ... }

             

 

* @Column —— 注解声明了属性到列的映射。该注解有如下的属性  

* name 可选,列名(默认值是属性名)   

* unique 可选,是否在该列上设置唯一约束(默认值false   

* nullable 可选,是否设置该列的值可以为空(默认值false  

* insertable 可选,该列是否作为生成的insert语句中的一个列(默认值true  

* updatable 可选,该列是否作为生成的update语句中的一个列(默认值true  

* columnDefinition 可选,为这个特定列覆盖sql ddl片段(这可能导致无法在不同数据库间移植)  

* table 可选,定义对应的表(默认为主表)  

* length 可选,列长度(默认值255  

* precision 可选,列十进制精度(decimal precision)(默认值0  

* scale 可选,如果列十进制数值范围(decimal scale)可用,在此设置(默认值0  

Example 1:

      @Column(name="DESC", nullable=false, length=512)

      public String getDescription() { return description; }

 

* @GeneratedValue —— 注解声明了主键的生成策略。该注解有如下属性  

* strategy 指定生成的策略(JPA定义的),这是一个GenerationType。默认是GenerationType. AUTO  

* GenerationType.AUTO 主键由程序控制  

* GenerationType.TABLE 使用一个特定的数据库表格来保存主键  

* GenerationType.IDENTITY 主键由数据库自动生成(主要是自动增长类型)  

* GenerationType.SEQUENCE 根据底层数据库的序列来生成主键,条件是数据库支持序列。(这个值要与generator一起使用)  

* generator 指定生成主键使用的生成器(可能是orcale中的序列)。 

        Example 1:

     @Id

     @GeneratedValue(strategy=SEQUENCE, generator="CUST_SEQ")

     @Column(name="CUST_ID")

     public Long getId() { return id; }

 

     Example 2:

     @Id

     @GeneratedValue(strategy=TABLE, generator="CUST_GEN")

     @Column(name="CUST_ID")

     Long id;

 

* @SequenceGenerator —— 注解声明了一个数据库序列。该注解有如下属性  

* name 表示该表主键生成策略名称,它被引用在@GeneratedValue中设置的“generator”值中  

* sequenceName 表示生成策略用到的数据库序列名称。  

* initialValue 表示主键初始值,默认为0.  

* allocationSize 每次主键值增加的大小,例如设置成1,则表示每次创建新记录后自动加1,默认为50. 

         Example 1:

     @SequenceGenerator(name="EMP_SEQ", allocationSize=25)

 

* @GenericGenerator —— 注解声明了一个hibernate的主键生成策略。支持十三种策略。该注解有如下属性  

* name 指定生成器名称  

* strategy 指定具体生成器的类名(指定生成策略)。  

* parameters 得到strategy指定的具体生成器所用到的参数。  

*    其十三种策略(strategy属性的值)如下:  

*      1.native 对于orcale采用Sequence方式,对于MySQLSQL Server采用identity(处境主键生成机制)  

*        native就是将主键的生成工作将由数据库完成,hibernate不管(很常用)  

*        例:@GeneratedValue(generator = "paymentableGenerator")      

*          @GenericGenerator(name = "paymentableGenerator", strategy = "native")  

*      2.uuid 采用128位的uuid算法生成主键,uuid被编码为一个3216进制数字的字符串。占用空间大(字符串类型)。  

*       例:@GeneratedValue(generator = "paymentableGenerator")      

*          @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")   

*      3.hilo 要在数据库中建立一张额外的表,默认表名为hibernate_unque_key,默认字段为integer类型,名称是next_hi(比较少  用)  

*        例:@GeneratedValue(generator = "paymentableGenerator")      

*          @GenericGenerator(name = "paymentableGenerator", strategy = "hilo")  

*      4.assigned 在插入数据的时候主键由程序处理(很常用),这是<generator>元素没有指定时的默认生成策略。等同于JPA中的AUTO  

*         例:@GeneratedValue(generator = "paymentableGenerator")      

*          @GenericGenerator(name = "paymentableGenerator", strategy = "assigned")  

*      5.identity 使用SQL ServerMySQL的自增字段,这个方法不能放到Oracle中,Oracle不支持自增字段,要设定sequence(MySQL    SQL Server中很常用)。等同于JPA中的IDENTITY  

*        例:@GeneratedValue(generator = "paymentableGenerator")      

*          @GenericGenerator(name = "paymentableGenerator", strategy = "identity")   

*      6.select 使用触发器生成主键(主要用于早期的数据库主键生成机制,少用)  

*       例:@GeneratedValue(generator = "paymentableGenerator")      

*          @GenericGenerator(name = "paymentableGenerator", strategy = "select")  

*      7.sequence 调用谨慎数据库的序列来生成主键,要设定序列名,不然hibernate无法找到。   

*       例:@GeneratedValue(generator = "paymentableGenerator")     

                    @GenericGenerator(name = "paymentableGenerator", strategy = "sequence",      

*                 parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })  

*      8.seqhilo 通过hilo算法实现,但是主键历史保存在Sequence中,适用于支持Sequence的数据库,如Orcale(比较少用)   

*         例:@GeneratedValue(generator = "paymentableGenerator")     

*         @GenericGenerator(name = "paymentableGenerator", strategy = "seqhilo",      

*         parameters = { @Parameter(name = "max_lo", value = "5") })  

*     9.increnment 插入数据的时候hibernate会给主键添加一个自增的主键,但是一个hibernate实例就维护一个计数器,所以在多个实     例运行的时候不能使用这个方法。  

*        例:@GeneratedValue(generator = "paymentableGenerator")      

*          @GenericGenerator(name = "paymentableGenerator", strategy = "increnment")  

*      10.foreign 使用另一个相关的对象的主键。通常和<one-to-one>联合起来使用。  

*          例:@Id      

*          @GeneratedValue(generator = "idGenerator")     

*          @GenericGenerator(name = "idGenerator", strategy = "foreign",      

*         parameters = { @Parameter(name = "property", value = "info") })      

*         Integer id;  

*          @OneToOne   

*          EmployeeInfo info;  

*      11.guid 采用数据库底层的guid算法机制,对应MySQLuuid()函数,SQL Servernewid()函数,ORCALE rawtohex(sys_guid())函数等  

*          例:@GeneratedValue(generator = "paymentableGenerator")       

*          @GenericGenerator(name = "paymentableGenerator", strategy = "guid")   

*      12.uuid.hex uudi,建议用uuid替换  

*         例:@GeneratedValue(generator = "paymentableGenerator")       

*         @GenericGenerator(name = "paymentableGenerator", strategy = "uuid.hex")  

*      13.sequence-identity sequence策略的扩展,采用立即检索策略来获取sequence值,需要JDBC3.0JDK4以上(含1.4)版本  

*         例:@GeneratedValue(generator = "paymentableGenerator")     

*         @GenericGenerator(name = "paymentableGenerator", strategy = "sequence-identity",      

*         parameters = { @Parameter(name = "sequence", value = "seq_payablemoney") })    

*        

*  @OneToOne 设置一对一 关联。cascade属性有五个值(只有CascadeType.ALL好用?很奇怪),分别是

               CascadeType.PERSIST(级联新建)

               CascadeType.REMOVE(级联删除),

               CascadeType.REFRESH(级联刷新),

               CascadeType.MERGE(级联更新),

               CascadeType.ALL(全部四项)  

*    方法一  (主键关联)

*    主表: ?@OneToOne(cascade = CascadeType.ALL)  

*      @PrimaryKeyJoinColumn  

*      public 从表类 get从表类(){return 从表对象}  

*    从表:没有主表类。  

*      注意:这种方法要求主表与从表的主键值相对应。 

Example 2: One-to-one association that assumes both the source and target share the same primary key values.

 

    On Employee class:

 

    @Entity

    public class Employee {

        @Id Integer id;

   

        @OneToOne @PrimaryKeyJoinColumn

        EmployeeInfo info;

        ...

    }

 

    On EmployeeInfo class:

 

    @Entity

    public class EmployeeInfo {

        @Id Integer id;

        ...

    }

*   方法二  

*      主表?@OneToOne(cascade = CascadeType.ALL)  

*          @JoinColumn(name="主表外键") //这里指定的是数据库中的外键字段。  

*          public 从表类 get从表类(){return 从表类 

*      从表@OneToOne(mappedBy = "主表类中的从表属性")//例主表User中有一个从表属性是Heart类型的heart,这里就填heart  

*          public 主表类 get主表类(){return 主表对象}   

*     

注意:@JoinColumn是可选的。默认值是从表变量名+"_"+从表的主键(注意,这里加的是主键。而不是主键对应的变量)。

Example 1: One-to-one association that maps a foreign key column

 

    On Customer class:

 

    @OneToOne(optional=false)

    @JoinColumn(

        name="CUSTREC_ID", unique=true, nullable=false, updatable=false)

    public CustomerRecord getCustomerRecord() { return customerRecord; }

 

    On CustomerRecord class:

 

    @OneToOne(optional=false, mappedBy="customerRecord")

    public Customer getCustomer() { return customer; }  

* 方法三  

*      主表:@OneToOne(cascade=CascadeType.ALL)  

*          @JoinTable( name="关联表名",  

*     joinColumns = @JoinColumn(name="主表外键"),  

*     inverseJoinColumns = @JoinColumn(name="从表外键")  

*          )  

*      从表@OneToOne(mappedBy = "主表类中的从表属性")//例主表User中有一个从表属性是Heart类型的heart,这里就填heart  

*          public 主表类 get主表类(){return 主表对象}  

  

public @interface JoinTable

JoinColumn[]

inverseJoinColumns
          (Optional) The foreign key columns of the join table which reference the primary table of the entity that does not own the association (i.e. the inverse side of the association).

 JoinColumn[]

joinColumns
          (Optional) The foreign key columns of the join table which reference the primary table of the entity owning the association (i.e. the owning side of the association).

 String

name
          (Optional) The name of the join table.

Example:

    @JoinTable(

    name="CUST_PHONE",

    joinColumns=

        @JoinColumn(name="CUST_ID", referencedColumnName="ID"),

    inverseJoinColumns=

        @JoinColumn(name="PHONE_ID", referencedColumnName="ID")

)

public @interface JoinColumn

 

Is used to specify a mapped column for joining an entity association.

 

   Example:

 

   @ManyToOne

   @JoinColumn(name="ADDR_ID")

   public Address getAddress() { return address; }

 String

name
          (Optional) The name of the foreign key column.

 boolean

nullable
          (Optional) Whether the foreign key column is nullable.

 String

referencedColumnName
          (Optional) The name of the column referenced by this foreign key column.

 

* @ManyToOne 设置多对一关联  

*    方法一   (无连接表)       

*    @ManyToOne(cascade={CasCadeType.PERSIST,CascadeType.MERGE})  

*    @JoinColumn(name="外键")  

*    public 主表类 get主表类(){return 主表对象}  

        Example:

 

     @ManyToOne(optional=false)

     @JoinColumn(name="CUST_ID", nullable=false, updatable=false)

     public Customer getCustomer() { return customer; }

*   

       方法二   (有连接表)

*    @ManyToOne(cascade={CascadeType.PERSIST,CascadeType.MERGE})  

*    @JoinTable(name="关联表名"  

*    joinColumns = @JoinColumn(name="主表外键"),  

*    inverseJoinColumns = @JoinColumns(name="从表外键")  

*    )  

 

* @OneToMany 设置一对多关联。cascade属性指定关联级别,参考@OneToOne中的说明。fetch指定是否延迟加载,值为  FetchType.LAZY表示延迟,为FetchType.EAGER表示立即加载  

*    方法一     使用这种配置,在为“一端”添加“多端”时,不会修改“多端”的外键。在“一端”加载时,不会得到“多端”。如果使用延迟加载,在读“多端”列表时会出异常,立即加载在得到多端时,是一个空集合(集合元素为0)。  

*    “一端”配置  

*    @OneToMany(mappedBy="“多端”的属性")  

*    public List<“多端”类> get“多端”列表(){return “多端”列表}  

*    “多端”配置参考@ManyToOne.       

*   方法二  

*    “一端”配置  

*    @OneToMany(mappedBy="“多端”的属性")  

*    @MapKey(name="“多端”做为Key的属性")  

*    public Map<“多端”做为Key的属性的类,主表类> get“多端”列表(){return “多端”列表}  

*    “多端”配置参考@ManyToOne.   

*   方法三 使用这种配置,在为“一端”添加“多端”时,可以修改“多端”的外键。  

*    “一端”配置  

*    @OneToMany  

*    @JoinColumn(name="“多端”外键")   

*    public List<“多端”类> get“多端”列表(){return “多端”列表}  

*    “多端”配置参考@ManyToOne.