hibernate属性级别注解实例代码

  


  

  

<强>添加方式:
  

  

,,,,,,,写在属性字段上面
  写在属性的被访问器的上面
  

  

@ id
  

  

,,,必须,定义了映射到数据库表的主键的属性,一个实体类可以有一个或者多个属性被映射为主键,可置于主键属性或者getXxx()前,注意:如果有多个属性定义为主键属性,该实体类必须实现可序列化的接口
  

  

generator
  @GeneratedValue
  ,@GeneratedValue(策略=GenerationType发电机=" "):可选,用于定义主键生成策略
  

  

,<强>策略表示主键生成策略,取值有:
  

  

,,,,,,GenerationType。汽车:根据底层数据库自动选择(默认)
  GenerationType。单位:根据数据库的身份字段生成
  GenerationType。序列:使用序列来决定主键的取值
  GenerationType。表:使用指定表来决定主键取值,结合@TableGenerator使用
  

  

@ column
  

  

,,,,@ column -可将属性映射到列,使用该注解来覆盖默认值,@ column描述了数据库表中该字段的详细定义,这对于根据JPA
  注解生成数据库表结构的工具非常有作用。
  

  

<强>常用属性:
  

  

名称:可选,表示数据库表中该字段的名称,默认情形属性名称一致
  可以为空:可选,表示该字段是否允许为null,默认为真正的
  独特:可选,表示该字段是否为唯一标识,默认为假
  长度:可选,表示该字段的大小,仅对字符串类型的字段有效,默认值225,主键不能使用默认值
  可插入的:可选,表示在ORM框架执行插入操作时,该字段是否应出现INSERTRT语句中,默认为真正的
  可更新:可选,表示在ORM框架执行更新操作时,该字段是否应该出现更新语在句中,默认为真的。对于已经创建
  就不可以更改的字段,该属性非常有用
  

  

@Embedded
  

  

@Embedded是注释属性的,表示该属性的类是嵌入类。
  

  

注意:同时嵌入类也必须标注@Embeddable注解
  

  

@EmbeddedId
  

  

@EmbeddedId使用嵌入式主键类实现复合主键
  

  

嵌入式主键类必须实现可序列化的接口,必须有默认的公众无参数的构造方法,必须覆盖equals和hashCode方法
  

  

@Lob
  @Version
  @Basic
  @Transient
  

  

,,,,,可选,表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性,如果一个属性并非数据库表的字段映射,就
  务必将其标示为@Transient,否则ORM框架默认其注解为@Basic
  

  

<>强实例:
  

  

<强>冬眠。属性配置:
  

        & lt; !DOCTYPE hibernate配置公共   ”——//Hibernate/Hibernate配置3.0 DTD//EN "   “http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd”在      & lt; hibernate-configuration>   & lt; session-factory>   & lt;属性名=" hibernate.dialect "祝辞org.hibernate.dialect.MySQLDialect   & lt; !   & lt;属性名=" hibernate.connection.driver_class "祝辞com.mysql.jdbc.Driver   & lt;属性名=" hibernate.connection.url "祝辞jdbc: mysql:///hibernate_struts_stumanager   ——比;   & lt;属性名=" hibernate.connection.driver_class "祝辞com.mysql.jdbc.Driver   & lt;属性名=" hibernate.connection.url "祝辞jdbc: mysql://localhost: 3306/mypage         & lt;属性名=" hibernate.connection.username "祝辞root   & lt;属性名=" hibernate.connection.password "祝辞root   & lt;属性名=" hibernate.show_sql "祝辞false   & lt;属性名=癮uto”祝辞create   & lt; !——& lt;属性名=" hibernate_current_session_context_class "祝辞thread——比;   & lt;属性名=" current_session_context_class "祝辞thread   & lt;映射类=" com.entity.Students "/比;   & lt;/session-factory>   & lt;/hibernate-configuration>之前      

<>强实体类学生:

        包com.entity;   进口java.io.Serializable;   进口java.util.Date;   进口javax.persistence.Column;   进口javax.persistence.Embedded;   进口javax.persistence.EmbeddedId;   进口javax.persistence.Entity;//JPA注解   进口javax.persistence.GeneratedValue;   进口javax.persistence.GenerationType;   进口javax.persistence.Id;   进口javax.persistence.Table;   进口javax.persistence.Transient;   进口org.hibernate.annotations.GenericGenerator;/*   *学生实体类   *///@ entity////@ entity (name=" t_students ")//如果不添加名字,则默认与实体类名字相同,如果想要自行设置表明,就需要自己进行添加   @ entity   @ table (name=" t_students1 "模式=癿ypage”)   公共类学生实现Serializable {   @ id//@GeneratedValue//@GeneratedValue(策略=GenerationType.AUTO)//与@GeneratedValue结果相同,字符串类型不能指定为汽车类型//私人int sid;//学号//@GeneratedValue(发电机=皊id”)//@GenericGenerator (name=皊id”策略=胺峙洹?//@ column(长度=11)//私人字符串sid;   @EmbeddedId   私人StudentPK pk;//@ id////@ column(长度=11)   私人字符串sname;//姓名   私人字符串性别;//性别   私人生日日期;//出生日期   私人字符串主要;//专业   私有地址添加;   @Transient//表示该字段不会被ORM映射到表里的字段   私人双倍工资;//薪水   公共的学生(){   }   sname公共学生(StudentPK pk,字符串,字符串性别,   日期日期、字符串主要地址添加、双工资){//超级();   这一点。pk=pk;   这一点。sname=sname;   这一点。性别=性别;   这一点。生日=日期;   这一点。重大=;   这一点。添加=添加;   这一点。工资=工资;   }   公共StudentPK getPk () {   返回pk;   }   公共空间setPk (StudentPK pk) {   这一点。pk=pk;   }   公共字符串getSname () {   返回sname;   }   公共空间setSname(字符串sname) {   这一点。sname=sname;   }   公共字符串getGender () {   返回性别;   }   公共空间setGender(字符串性别){   这一点。性别=性别;   }   上市日期getBirthday () {   返回的生日;   }   公共空间setBirthday(日期的生日){   这一点。生日=生日;   }   公共字符串getMajor () {   返回大;   }   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

hibernate属性级别注解实例代码