一。实体Bean每个持久化POJO类都是一个实体Bean,通过在类的定义中使用@Entity注解来进行声明。声明实体Bean@EntitypublicclassFlightimplementsSerializable{Longid;@IdpublicLonggetId(){returnid;}publicvoidsetId(Longid){this.id=id;}}@Entity注解将一个类声明为实体Bean,@Id注解声明了该实体Bean的标识属性。Hibernate可以对类的属性或者方法进行注解。属性对应field类别,方法的getXxx()对应property类别。定义表通过@Table为实体Bean指定对应数据库表,目录和schema的名字。@Entity@Table(name=tbl_sky)publicclassSkyimplementsSerializable{...@Table注解包含一个schema和一个catelog属性,使用@UniqueConstraints可以定义表的唯一约束。@Table(name=tbl_sky,uniqueConstraints={@UniqueConstraint(columnNames={month,day})})上述代码在month和day两个field上加上uniqueconstrainst.@Version注解用于支持乐观锁版本控制。@EntitypublicclassFlightimplementsSerializable{...@Version@Column(name=OPTLOCK)publicIntegergetVersion(){...}}version属性映射到OPTLOCK列,entitymanager使用这个字段来检测冲突。一般可以用数字或者timestamp类型来支持version.实体Bean中所有非static非transient属性都可以被持久化,除非用@Transient注解。默认情况下,所有属性都用@Basic注解。publictransientintcounter;//transientpropertyprivateStringfirstname;//persistentproperty@TransientStringgetLengthInMeter(){...}//transientpropertyStringgetName(){...}//persistentproperty@BasicintgetLength(){...}//persistentproperty@Basic(fetch=FetchType.LAZY)StringgetDetailedComment(){...}//persistentproperty@Temporal(TemporalType.TIME)java.util.DategetDepartureTime(){...}//persistentproperty@Enumerated(EnumType.STRING)StarredgetNote(){...}//enumpersistedasStringindatabase上述代码中counter,lengthInMeter属性将忽略不被持久化,而firstname,name,length被定义为可持久化和可获取的。@TemporalType.(DATE,TIME,TIMESTAMP)分别Mapjava.sql.(Date,Time,Timestamp).@Lob注解属性将被持久化为Blog或Clob类型。具体的java.sql.Clob,Character[],char[]和java.lang.String将被持久化为Clob类型.java.sql.Blob,Byte[],byte[]和serializabletype将被持久化为Blob类型。@LobpublicStringgetFullText(){returnfullText;//clobtype}@Lobpublicbyte[]getFullCode(){returnfullCode;//blogtype}@Column注解将属性映射到列。@EntitypublicclassFlightimplementsSerializable{...@Column(updatable=false,name=flight_name,nullable=false,length=50)publicStringgetName(){...}定义name属性映射到flight_namecolumn,notnull,can'tupdate,lengthequal50@Column(name=columnName;(1)列名booleanunique()defaultfalse;(2)是否在该列上设置唯一约束booleannullable()defaulttrue;(3)列可空?booleaninsertable()defaulttrue;(4)该列是否作为生成insert语句的一个列booleanupdatable()defaulttrue;(5)该列是否作为生成update语句的一个列StringcolumnDefinition()default;(6)默认值Stringtable()default;(7)定义对应的表(deault是主表)intlength()default255;(8)列长度intprecision()default0;//decimalprecision(9)decimal精度intscale()default0;//decimalscale(10)decimal长度嵌入式对象(又称组件)也就是别的对象定义的属性组件类必须在类一级定义@Embeddable注解。在特定的实体关联属性上使用@Embeddable和@AttributeOverride注解可以覆盖该属性对应的嵌入式对象的列映射。@EntitypublicclassPersonimplementsSerializable{//PersistentcomponentusingdefaultsAddresshomeAddress;@Embedded@AttributeOverrides({@AttributeOverride(name=iso2,column=@Column(name=bornIso2)),@AttributeOverride(name=name,column=@Column(name=bornCountryName))})CountrybornIn;...}@EmbeddablepublicclassAddressimplementsSerializable{Stringcity;Countrynationality;//nooverridinghere}@EmbeddablepublicclassCountryimplementsSerializable{privateStringiso2;@Column(name=countryName)privateStringname;publicStringgetIso2(){returniso2;}publicvoidsetIso2(Stringiso2){this.iso2=iso2;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}...}Person类定义了Address和Country对象,具体两个类实现见上。无注解属性默认值:•属性为简单类型,则映射为@Basic•属性对应的类型定义了@Embeddable注解,则映射为@Embedded•属性对应的类型实现了Serializable,则属性被映射为@Basic并在一个列中保存该对象的serialized版本。•属性的类型为java.sql.Cloborjava.sql.Blob,则映射到@Lob对应的类型。映射主键属性@Id注解可将实体Bean中某个属性定义为主键,使用@GenerateValue注解可以定义该标识符的生成策略。•AUTO-可以是identitycolumn,sequence或者table类型,取决于不同底层的数据库•TABLE-使用table保存id值•IDENTITY-identitycolumn•SEQUENCE-sequence@Id@GeneratedValue(strategy=GenerationType.SEQUENCE,generator=SEQ_STORE)publicIntegergetId(){...}@Id@GeneratedValue(strategy=GenerationType.IDENTITY)publicLonggetId(){...}AUTO生成器,适用与可移值的应用,多个@Id可以共享同一个identifier生成器,只要把generator属性设成相同的值就可以。通过@SequenceGenerator和@TableGenerator可以配置不同的identifier生成器。table-generatorname=EMP_GENtable=GENERATOR_TABLEpk-column-name=keyvalue-column-name=hipk-column-value=EMPallocation-size=20///andtheannotationequivalent@javax.persistence.TableGenerator(name=EMP_GEN,table=GENERATOR_TABLE,pkColumnName=key,valueColumnName=hipkColumnValue=EMP,allocationSize=20)sequence-generatorname=SEQ_GENsequence-name=my_sequenceallocation-size=20///andtheannotationequivalent@javax.persistence.SequenceGenerator(name=SEQ_GEN,sequenceName=my_sequence,allocationSize=20)Thenextexampleshowsthedefinitionofasequencegeneratorinaclassscope:@Entity@javax.persistence.SequenceGenerator(name=SEQ_STORE,sequenceName=my_sequence)publicclassStoreimplementsSerializable{privateLongid;@Id@GeneratedValue(strategy=GenerationType.SEQUENCE,generator=SEQ_STORE)publicLonggetId(){returnid;}}Store类使用名为my_sequence的sequence,并且SEQ_STORE生成器对于其他类是不可见的。通过下面语法,你可以定义组合键。•将组件类注解为@Embeddable,并将组件的属性注解为@Id•将组件的属性注解为@EmbeddedId•将类注解为@IdClass,并将该实体中所有主键的属性都注解为@Id@Entity@IdClass(FootballerPk.class)publicclassFootballer{//partoftheidkey@IdpublicStringgetFirstname(){returnfirstname;}publicvoidsetFirstname(Stringfirstname){this.firstname=firstname;}//partoftheidke