Hibernate Anotation使用

http://shiwj1010.iteye.com/blog/385405
http://www.iteye.com/topic/95822
http://www.iteye.com/topic/76359
http://java.chinaitlab.com/Hibernate/763968.html
基于按annotation的hibernate主键生成策略之一
http://hi.baidu.com/babala_234/blog/item/8aae7445e30e933686947359.html
基于按annotation的hibernate主键生成策略之二
http://hi.baidu.com/babala_234/blog/item/84b25809c6e5b12d6a60fb5a.html
http://hi.baidu.com/%E7%F9%BB%C3%CE%E7%D2%B9/blog/item/4b24412c78bf093d359bf779.html
id生成策略:TABLE SEQUENCE ASSIGN UUID AUTO
@Id
 @TableGenerator(
      name="ID_GENERATOR",
      table="ID_GEN",
      pkColumnName = "GEN_NAME",
      pkColumnValue = "NBR_PK",
      valueColumnName = "GEN_VALUE",
      initialValue = 1,
      allocationSize = 1
  )
  @GeneratedValue(strategy=GenerationType.TABLE, generator="ID_GENERATOR")
  @Column(name="NBR", length=10)
  private Long nbr=0L; 


JPA 会执行这样的语句:

update ID_GEN set GEN_VALUE = GEN_VALUE + 1 where GEN_NAME = ‘ NBR_PK’

通过这个sql语句就会明白注解里面各个属性的含义。

对于ID_GEN 表,不需要创建相应的Java entity

这种方式有个好处就是数据库的可移植
package org.app.dict.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Index;

import com.ria.model.BaseVo;

@Entity
//表名+唯一约束限制[如果 表名与实体类名不一致就需要声明]
@Table(name="T_SYS_dict",uniqueConstraints = {@UniqueConstraint(columnNames = {"dict_name"}) })
@org.hibernate.annotations.Table(appliesTo = "T_SYS_dict",comment="字典表")
public class T_SYS_dict extends BaseVo {
	private static final long serialVersionUID = 1L;
	
	@Column(name="AD_Client_ID")
	private int AD_Client_ID;
	
	@Column(name="AD_Org_ID")
	private int AD_Org_ID;
	
	@Column(name="IsActive")
	boolean IsActive = true;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="Created")
	private java.util.Date Created;
	@Column(name="CreatedBy")
	private int CreatedBy;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="Updated")
	private java.util.Date Updated;
	
	@Column(name="UpdatedBy")
	private int UpdatedBy;
	/**
	@Id
    @GeneratedValue(generator = "system-uuid")   
    @GenericGenerator(name = "system-uuid", strategy = "uuid") //hibernate的uuid机制,生成32为字符串
    --or--
    @GeneratedValue(generator = "c-assigned")
    @GenericGenerator(name = "c-assigned", strategy = "assigned") //hibernate的自定义机制
    --or--
    @GeneratedValue(strategy = GenerationType.AUTO,generator="test_sequence")
	@SequenceGenerator(name = "test_sequence",sequenceName="TEST_SEQ")//采用 db sequence
	--or--
	@GeneratedValue(generator="myForeignGenerator")
	@org.hibernate.annotations.GenericGenerator(
   		name="myForeigngenerator",
   		strategy="foreign",
   		parameters=@Parameter(name="property",value="User")
	)
	 */
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Index(name="T_SYS_INDEXS",columnNames={"T_SYS_dict_id"}) //加索引
    @Column(name="T_SYS_dict_id",unique=true)
    private long T_SYS_dict_id;

	@Index(name="T_SYS_INDEXS",columnNames={"dict_name"}) //加索引
	@Column(name = "dict_name", updatable = true, nullable = false, length = 36)
    private String dict_name;

	@Column(name = "remark", updatable = true, nullable = true, length = 255)
    private String remark;

	@Column(name="parent_id")
    private long parent_id;
	@Column(name="dict_order",length=3)
	private int dict_order;
	
}


@TemporalType.(DATE,TIME,TIMESTAMP) 分别Map java.sql.(Date, Time, Timestamp).

@Lob 注解属性将被持久化为 Blog 或 Clob 类型。具体的java.sql.Clob, Character[], char[] 和 java.lang.String 将被持久化为 Clob 类型. java.sql.Blob, Byte[], byte[] 和 serializable type 将被持久化为 Blob 类型。
version属性映射到 "OPTLOCK" 列,entity manager 使用这个字段来检测冲突。 一般可以用 数字 或者 timestamp 类型来支持 version.

实体Bean中所有非static 非 transient 属性都可以被持久化,除非用@Transient注解。

你可能感兴趣的:(java,sql,Hibernate,jpa,Blog)