资源预览内容
第1页 / 共22页
第2页 / 共22页
第3页 / 共22页
第4页 / 共22页
第5页 / 共22页
第6页 / 共22页
第7页 / 共22页
第8页 / 共22页
第9页 / 共22页
第10页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
Hibernate 注释大全收藏如下:声明实体 BeanEntitypublic class Flight implements Serializable Long id;Idpublic Long getId() return id; public void setId(Long id) this.id = id; Entity 注解将一个类声明为实体 Bean, Id 注解声明了该实体 Bean 的标识属性。Hibernate 可以对类的属性或者方法进行注解。属性对应 field 类别,方法的 getXxx()对应property 类别。定义表通过 Table 为实体 Bean 指定对应数据库表,目录和 schema 的名字。EntityTable(name=tbl_sky)public class Sky implements Serializable .Table 注解包含一个 schema 和一个 catelog 属性,使用UniqueConstraints 可以定义表的唯一约束。Table(name=tbl_sky,uniqueConstraints = UniqueConstraint(columnNames=month, day)上述代码在 month 和 day 两个 field 上加上 unique constrainst.Version 注解用于支持乐观锁版本控制。Entitypublic class Flight implements Serializable .VersionColumn(name=OPTLOCK)public Integer getVersion() . version 属性映射到 OPTLOCK 列,entity manager 使用这个字段来检测冲突。 一般可以用 数字 或者 timestamp 类型来支持 version.实体 Bean 中所有非 static 非 transient 属性都可以被持久化,除非用Transient 注解。默认情况下,所有属性都用 Basic 注解。public transient int counter; /transient propertyprivate String firstname; /persistent propertyTransientString getLengthInMeter() . /transient propertyString getName() . / persistent propertyBasicint getLength() . / persistent propertyBasic(fetch = FetchType.LAZY)String getDetailedComment() . / persistent propertyTemporal(TemporalType.TIME)java.util.Date getDepartureTime() . / persistent propertyEnumerated(EnumType.STRING)Starred getNote() . /enum persisted as String in database上述代码中 counter, lengthInMeter 属性将忽略不被持久化,而 firstname, name, length 被定义为可持久化和可获取的。 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 类型。Lobpublic String getFullText() return fullText; / clob typeLobpublic byte getFullCode() return fullCode; / blog typeColumn 注解将属性映射到列。Entitypublic class Flight implements Serializable .Column(updatable = false, name = flight_name, nullable = false, length=50)public String getName() . 定义 name 属性映射到 flight_name column, not null, cant update, length equal 50Column(name=columnName; (1) 列名boolean unique() default false; (2) 是否在该列上设置唯一约束boolean nullable() default true; (3) 列可空?boolean insertable() default true; (4) 该列是否作为生成 insert 语句的一个列boolean updatable() default true; (5) 该列是否作为生成 update 语句的一个列String columnDefinition() default ; (6) 默认值String table() default ; (7) 定义对应的表( deault 是主表)int length() default 255; (8) 列长度int precision() default 0; / decimal precision (9) decimal 精度int scale() default 0; / decimal scale (10) decimal 长度嵌入式对象(又称组件)也就是别的对象定义的属性组件类必须在类一级定义 Embeddable 注解。在特定的实体关联属性上使用 Embeddable 和 AttributeOverride 注解可以覆盖该属性对应的嵌入式对象的列映射。Entitypublic class Person implements Serializable / Persistent component using defaultsAddress homeAddress;EmbeddedAttributeOverrides( AttributeOverride(name=iso2, column = Column(name=bornIso2) ),AttributeOverride(name=name, column = Column(name=bornCountryName) ) )Country bornIn;.Embeddablepublic class Address implements Serializable String city;Country nationality; /no overriding hereEmbeddablepublic class Country implements Serializable private String iso2;Column(name=countryName) private String name;public String getIso2() return iso2; public void setIso2(String iso2) this.iso2 = iso2; public String getName() return name; public void setName(String name) this.name = name; .Person 类定义了 Address 和 Country 对象,具体两个类实现见上。无注解属性默认值: 属性为简单类型,则映射为 Basic 属性对应的类型定义了 Embeddable 注解,则映射为 Embedded 属性对应的类型实现了 Serializable,则属性被映射为Basic 并在一个列中保存该对象的serialized 版本。 属性的类型为 java.sql.Clob or java.sql.Blob, 则映射到 Lob 对应的类型。映射主键属性Id 注解可将实体 Bean 中某个属性定义为主键,使用GenerateValue 注解可以定义该标识符的生成策略。 AUTO - 可以是 identity column, sequence 或者 table 类型,取决于不同底层的数据库 TABLE - 使用 table 保存 id 值 IDENTITY - identity column SEQUENCE - sequence Id GeneratedValue(strategy=GenerationType.SEQUENCE, generator=SEQ_STORE)public Integer getId() . Id GeneratedValue(strategy=GenerationType.IDENTITY)public Long getId() . AUTO 生成器,适用与可移值的应用,多个Id 可以共享同一个 identifier 生成器,只要把 generator 属性设成相同的值就可以。通过SequenceGenerator 和 TableGenerator 可以配置不同的 identifier 生成器。 /and the annotation equivalentjavax.persistence.TableGenerator(name=EMP_GEN,table=GENERATOR_TABLE,pkColumnName = key,valueColumnName = hipkColumnValue=EMP,allocationSize=20)/and the annotation equivalentjavax.persistence.SequenceGenerator(name=SEQ_GEN,sequenceName=my_sequence,allocationSize=20)The next example shows the definition of a sequence generator in a class scope:Entityjavax.persistence.SequenceGenerator(name=SEQ_STORE,sequenceName=my_sequence)public class Store implements Serializable private Long id;Id GeneratedValue(strategy=GenerationType.SEQUENCE, generator=SEQ_STORE)public Long getId() return id;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号