Hibernate注解

  • 格式:doc
  • 大小:56.00 KB
  • 文档页数:6

Hibernate注解常用的hibernate annotation标签如下:@Entity--注释声明该类为持久类。

@Table(name="promotion_info")--持久性映射的表(表名="promotion_info)。

@Column(name=”DESC”,nullable=false,length=512)--用于指定持久属性或字段的映射列。

@Id--注释可以表明哪种属性是该类中的独特标识符(即相当于数据表的主键)。

@GeneratedValue--定义自动增长的主键的生成策略。

@Transient--将忽略这些字段和属性,不用持久化到数据库。

@Temporal(TemporalType.TIMESTAMP)--声明时间格式。

@Enumerated--声明枚举@Version--声明添加对乐观锁定的支持@OneToOne--可以建立实体bean之间的一对一的关联@OneToMany--可以建立实体bean之间的一对多的关联@ManyToOne--可以建立实体bean之间的多对一的关联@ManyToMany--可以建立实体bean之间的多对多的关联@Formula--一个SQL表达式,这种属性是只读的,不在数据库生成属性(可以使用sum、average、max等)@OrderBy--Many端某个字段排序(List)下面是对以上常用Hibernate注解标签的详细介绍与举例:@Entity--注释声明该类为持久类。

将一个Javabean类声明为一个实体的数据库表映射类,最好实现序列化.此时,默认情况下,所有的类属性都为映射到数据表的持久性字段.若在类中,添加另外属性,而非映射来数据库的, 要用下面的Transient来注解.@Table(name="promotion_info")--持久性映射的表(表名="promotion_info).@T able是类一级的注解,定义在@Entity下,为实体bean映射表,目录和schema的名字,默认为实体bean的类名,不带包名.示例:@Entity@T able(name="CUST", schema="RECORDS")public class Customer { ... }@Column(name=”DESC”,nullable=false,length=512)--用于指定持久属性或字段的映射列。

如果没有指定Column 注释,则应用默认值。

该注释下的一些具体属性如下:①unique属性表示该字段是否为唯一标识,默认为false。

如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用@T able标记中的@UniqueConstraint。

②nullable属性表示该字段是否可以为null值,默认为true。

③insertable属性表示在使用“INSERT”脚本插入数据时,是否需要插入该字段的值。

④updatable属性表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。

insertable和updatable属性一般多用于只读的属性,例如主键和外键等。

这些字段的值通常是自动生成的。

⑤columnDefinition属性表示创建表时,该字段创建的SQL语句,一般用于通过Entity 生成表定义时使用。

⑥table属性表示当映射多个表时,指定表的表中的字段。

默认值为主表的表名。

⑦length属性表示字段的长度,当字段的类型为varchar时,该属性才有效,默认为255个字符。

⑧precision属性和scale属性表示精度,当字段类型为double时,precision表示数值的总长度,scale表示小数点所占的位数。

示例1:@Column(name="DESC", nullable=false, length=512)public String getDescription() { return description; }示例2:@Column(name="DESC",columnDefinition="CLOBNOT NULL",table="EMP_DETAIL")@Lobpublic String getDescription() {return description;}示例3:@Column(name="ORDER_COST", updatable=false, precision=12, scale=2)public BigDecimal getCost() { return cost; }@Id--注释可以表明哪种属性是该类中的独特标识符(即相当于数据表的主键)。

示例:@Idpublic Long getId() { return id; }@GeneratedValue--定义自动增长的主键的生成策略.示例1:@Id@GeneratedValue(strategy=SEQUENCE,generator="CUST_SEQ")@Column(name="CUST_ID")public Long getId() { return id; }示例2:@Id@GeneratedValue(strategy=TABLE,generator="CUST_GEN")@Column(name="CUST_ID")Long id;@Transient--将忽略这些字段和属性,不用持久化到数据库.适用于,在当前的持久类中,某些属性不是用于映射到数据表,而是用于其它的业务逻辑需要,这时,须将这些属性进行transient的注解.否则系统会因映射不到数据表相应字段而出错.示例:@Entitypublic class Employee {@Id int id;@Transient User currentUser;...}@Temporal(TemporalType.TIMESTAMP)--声明时间格式示例:@Temporal(DATE)protected java.util.Date endDate;@Enumerated--声明枚举示例:public enum EmployeeStatus {FULL_TIME, PART_TIME, CONTRACT}public enum SalaryRate {JUNIOR, SENIOR, MANAGER, EXECUTIVE}@Entity public class Employee {public EmployeeStatus getStatus() {...}@Enumerated(STRING)public SalaryRate getPayScale() {...}...}@Version--声明添加对乐观锁定的支持示例:@Version@Column(name="OPTLOCK")protected int getVersionNum() { return versionNum; }@OneToOne--可以建立实体bean之间的一对一的关联一般不必显式指定关联的目标实体,因为通常可以根据引用的对象类型推断出来。

示例1:映射外键列的一对一关联在Customer 类中:@OneToOne(optional=false)@JoinColumn(name="CUSTREC_ID", unique=true, nullable=false, updatable=false)public CustomerRecord getCustomerRecord() { return customerRecord; }在CustomerRecord 类中:@OneToOne(optional=false, mappedBy="customerRecord")public Customer getCustomer() { return customer; }示例2:假定源和目标共享同一个主键值的一对一关联。

在Employee 类中:@Entitypublic class Employee {@Id Integer id;@OneToOne @PrimaryKeyJoinColumnEmployeeInfo info;...}对于EmployeeInfo 类:@Entitypublic class EmployeeInfo {@Id Integer id;...}@OneToMany--可以建立实体bean之间的一对多的关联如果将Collection 的元素类型指定为generic 类型,则不必指定关联的目标实体类型;否则必须指定目标实体类。

示例1:使用generic 的一对多关联在Customer 类中:@OneToMany(cascade=ALL, mappedBy="customer")public SetgetOrders() { return orders; }在Order 类中:@ManyToOne@JoinColumn(name="CUST_ID", nullable=false)public Customer getCustomer() { return customer; }示例2:没有使用generic 的一对多关联在Customer 类中:@OneToMany(targetEntity=com.acme.Order.class,cascade=ALL, mappedBy="customer")public Set getOrders() { return orders; }在Order 类中:@ManyToOne@JoinColumn(name="CUST_ID", nullable=false)public Customer getCustomer() { return customer; }@ManyToOne--可以建立实体bean之间的多对一的关联此注释定义对另一个带有多对一多样性的实体类的单值关联。

一般不必显式指定目标实体,因为通常可以根据引用的对象类型推断出来。

示例:@ManyToOne(optional=false)@JoinColumn(name="CUST_ID", nullable=false, updatable=false)public Customer getCustomer() { return customer; }@ManyToMany--可以建立实体bean之间的多对多的关联如果将Collection 的元素类型指定为generic 类型,则不必指定关联的目标实体类;否则必须指定。