hibernate属性级别注解实例代码
添加方式:
写在属性字段上面
写在属性的get访问器的上面
@Id
必须,定义了映射到数据库表的主键的属性,一个实体类可以有一个或者多个属性被映射为主键,可置于主键属性或者getXxx()前,注意:如果有多个属性定义为主键属性,该实体类必须实现serializable接口
@SequenceGenerator
@GeneratedValue
@GeneratedValue(strategy=GenerationType,generator=""):可选,用于定义主键生成策略
strategy表示主键生成策略,取值有:
GenerationType.AUTO:根据底层数据库自动选择(默认)
GenerationType.INDENTITY:根据数据库的Identity字段生成
GenerationType.SEQUENCE:使用Sequence来决定主键的取值
GenerationType.TABLE:使用指定表来决定主键取值,结合@TableGenerator使用
@Column
@Column-可将属性映射到列,使用该注解来覆盖默认值,@Column描述了数据库表中该字段的详细定义,这对于根据JPA
注解生成数据库表结构的工具非常有作用。
常用属性:
name:可选,表示数据库表中该字段的名称,默认情形属性名称一致
nullable:可选,表示该字段是否允许为null,默认为true
unique:可选,表示该字段是否为唯一标识,默认为false
length:可选,表示该字段的大小,仅对String类型的字段有效,默认值225,主键不能使用默认值
insertable:可选,表示在ORM框架执行插入操作时,该字段是否应出现INSERTRT语句中,默认为true
updateable:可选,表示在ORM框架执行更新操作时,该字段是否应该出现在UPDATE语句中,默认为true。对于已经创建
就不可以更改的字段,该属性非常有用
@Embedded
@Embedded是注释属性的,表示该属性的类是嵌入类。
注意:同时嵌入类也必须标注@Embeddable注解
@EmbeddedId
@EmbeddedId使用嵌入式主键类实现复合主键
注意:嵌入式主键类必须实现Serializable接口、必须有默认的public无参数的构造方法、必须覆盖equals和hashCode方法
@Lob
@Version
@Basic
@Transient
可选,表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性,如果一个属性并非数据库表的字段映射,就
务必将其标示为@Transient,否则ORM框架默认其注解为@Basic
实例:
hibernate.properties配置:
org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/mypage root root false create thread
实体类Student:
packagecom.entity; importjava.io.Serializable; importjava.util.Date; importjavax.persistence.Column; importjavax.persistence.Embedded; importjavax.persistence.EmbeddedId; importjavax.persistence.Entity;//JPA注解 importjavax.persistence.GeneratedValue; importjavax.persistence.GenerationType; importjavax.persistence.Id; importjavax.persistence.Table; importjavax.persistence.Transient; importorg.hibernate.annotations.GenericGenerator; /* *学生实体类 */ //@Entity// //@Entity(name="t_students")//如果不添加名字,则默认与实体类名字相同,如果想要自行设置表明,就需要自己进行添加 @Entity @Table(name="t_students1",schema="mypage") publicclassStudentsimplementsSerializable{ @Id //@GeneratedValue //@GeneratedValue(strategy=GenerationType.AUTO)//与@GeneratedValue结果相同,字符串类型不能指定为AUTO类型 //privateintsid;//学号 //@GeneratedValue(generator="sid") //@GenericGenerator(name="sid",strategy="assigned") //@Column(length=11) //privateStringsid; @EmbeddedId privateStudentPKpk; //@Id // //@Column(length=11) privateStringsname;//姓名 privateStringgender;//性别 privateDatebirthday;//出生日期 privateStringmajor;//专业 privateAddressadd; @Transient//表示该字段不会被ORM映射到表里的字段 privatedoublesalary;//薪水 publicStudents(){ } publicStudents(StudentPKpk,Stringsname,Stringgender, Datedate,Stringmajor,Addressadd,doublesalary){ //super(); this.pk=pk; this.sname=sname; this.gender=gender; this.birthday=date; this.major=major; this.add=add; this.salary=salary; } publicStudentPKgetPk(){ returnpk; } publicvoidsetPk(StudentPKpk){ this.pk=pk; } publicStringgetSname(){ returnsname; } publicvoidsetSname(Stringsname){ this.sname=sname; } publicStringgetGender(){ returngender; } publicvoidsetGender(Stringgender){ this.gender=gender; } publicDategetBirthday(){ returnbirthday; } publicvoidsetBirthday(Datebirthday){ this.birthday=birthday; } publicStringgetMajor(){ returnmajor; } publicvoidsetMajor(Stringmajor){ this.major=major; } publicAddressgetAdd(){ returnadd; } publicvoidsetAdd(Addressadd){ this.add=add; } publicdoublegetSalary(){ returnsalary; } publicvoidsetSalary(doublesalary){ this.salary=salary; } }
StudentPK实体类:
packagecom.entity; importjava.io.Serializable; importjavax.persistence.Column; importjavax.persistence.Embeddable; @Embeddable publicclassStudentPKimplementsSerializable{ /** * */ privatestaticfinallongserialVersionUID=1L; @Column(length=18) privateStringid;//身份证号 @Column(length=8) privateStringsid;//学号 publicStudentPK(){ } publicStudentPK(Stringid,Stringsid){ this.id=id; this.sid=sid; } publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetSid(){ returnsid; } publicvoidsetSid(Stringsid){ this.sid=sid; } @Override publicbooleanequals(Objectobj){ //TODOAuto-generatedmethodstub returnsuper.equals(obj); } @Override publicinthashCode(){ //TODOAuto-generatedmethodstub returnsuper.hashCode(); } }
地址类:
packagecom.entity; importjavax.persistence.Embeddable; importjavax.persistence.Embedded; //地址类 @Embeddable//表示是一个嵌入类,这个类的对象在另一个实体类中充当属性 publicclassAddress{ privateStringpostCode;//邮编 privateStringaddress;//地址 privateStringphone;//联系电话 publicAddress(){ } publicAddress(StringpostCode,Stringaddress,Stringphone){ this.postCode=postCode; this.address=address; this.phone=phone; } publicStringgetPostCode(){ returnpostCode; } publicvoidsetPostCode(StringpostCode){ this.postCode=postCode; } @Embedded publicStringgetAddress(){ returnaddress; } publicvoidsetAddress(Stringaddress){ this.address=address; } publicStringgetPhone(){ returnphone; } publicvoidsetPhone(Stringphone){ this.phone=phone; } }
测试类:
packagecom.entity; importjava.util.Date; importjava.util.EnumSet; importorg.hibernate.Session; importorg.hibernate.SessionFactory; importorg.hibernate.Transaction; importorg.hibernate.cfg.Configuration; importorg.hibernate.service.ServiceRegistry; importorg.hibernate.service.ServiceRegistryBuilder; importorg.hibernate.tool.hbm2ddl.SchemaExport; importorg.junit.Test; publicclassTestStudents{ @Test publicvoidtestShemaExport(){ //创建hibernate配置对象 Configurationconfig=newConfiguration().configure(); //创建服务注册对象 ServiceRegistryserviceRegistry=newServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //生成SessionFactory SessionFactorysessionFactory=config.buildSessionFactory(serviceRegistry); SchemaExportexport=newSchemaExport(config); export.create(true,true); } @Test publicvoidaddStudents(){ //创建hibernate配置对象 Configurationconfig=newConfiguration().configure(); //创建服务注册对象 ServiceRegistryserviceRegistry=newServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //生成SessionFactory SessionFactorysessionFactory=config.buildSessionFactory(serviceRegistry); //创建会话 Sessionsession=sessionFactory.getCurrentSession(); //创建事务 Transactiontx=session.beginTransaction(); Addressadd=newAddress("700005","河南理工大学","15039117115"); StudentPKpk=newStudentPK("012345678912345678","55555555"); Studentss=newStudents(pk,"张三丰","男",newDate(),"太极拳",add,7899); session.save(s); tx.commit(); //SchemaExportexport=newSchemaExport(config); //export.create(true,true); } }
以上就是本文关于hibernate属性级别注解实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Hibernate核心思想与接口简介、Hibernate实现悲观锁和乐观锁代码介绍、快速了解hibernate配置文件与映射文件等。正所谓书中自有颜如玉,下面附几本不错的书籍,供大家学习参考,在此也非常希望大家对本站多多支持。
一键下载,免费的哦:
hibernate官方入门教程中文PDF版
https://www.nhooo.com/books/496598.html
Java设计模式深入研究PDF
https://www.nhooo.com/books/581548.html
数据挖掘:实用机器学习技术及Java实现(英文第2版)高清PDF
https://www.nhooo.com/books/577815.html