您好,登录后才能下订单哦!
属性级别注解
添加方式:
写在属性字段上面
写在属性的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配置:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_struts_stumanager</property> --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mypage</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.show_sql">false</property> <property name="hibernate.hbm2ddl.auto">create</property> <!-- <property name="hibernate_current_session_context_class">thread</property> --> <property name="current_session_context_class">thread</property> <mapping class="com.entity.Students"/> </session-factory> </hibernate-configuration>
实体类Student:
package com.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.EmbeddedId; import javax.persistence.Entity;//JPA注解 import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; import org.hibernate.annotations.GenericGenerator; /* * 学生实体类 */ //@Entity// //@Entity(name="t_students")//如果不添加名字,则默认与实体类名字相同,如果想要自行设置表明,就需要自己进行添加 @Entity @Table(name="t_students1",schema="mypage") public class Students implements Serializable{ @Id // @GeneratedValue // @GeneratedValue(strategy=GenerationType.AUTO)//与@GeneratedValue结果相同,字符串类型不能指定为AUTO类型 // private int sid; //学号 // @GeneratedValue(generator="sid") // @GenericGenerator(name="sid",strategy="assigned") // @Column(length=11) // private String sid; @EmbeddedId private StudentPK pk; // @Id // // @Column(length=11) private String sname;//姓名 private String gender;//性别 private Date birthday;//出生日期 private String major;//专业 private Address add; @Transient //表示该字段不会被ORM映射到表里的字段 private double salary;//薪水 public Students(){ } public Students(StudentPK pk, String sname, String gender, Date date, String major,Address add,double salary) { // super(); this.pk = pk; this.sname = sname; this.gender = gender; this.birthday = date; this.major = major; this.add = add; this.salary = salary; } public StudentPK getPk() { return pk; } public void setPk(StudentPK pk) { this.pk = pk; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getMajor() { return major; } public void setMajor(String major) { this.major = major; } public Address getAdd() { return add; } public void setAdd(Address add) { this.add = add; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
StudentPK实体类:
package com.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; @Embeddable public class StudentPK implements Serializable{ /** * */ private static final long serialVersionUID = 1L; @Column(length=18) private String id;//身份证号 @Column(length=8) private String sid;//学号 public StudentPK(){ } public StudentPK(String id, String sid) { this.id = id; this.sid = sid; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub return super.equals(obj); } @Override public int hashCode() { // TODO Auto-generated method stub return super.hashCode(); } }
地址类:
package com.entity; import javax.persistence.Embeddable; import javax.persistence.Embedded; // 地址类 @Embeddable //表示是一个嵌入类,这个类的对象在另一个实体类中充当属性 public class Address { private String postCode;//邮编 private String address;//地址 private String phone;//联系电话 public Address(){ } public Address(String postCode, String address, String phone) { this.postCode = postCode; this.address = address; this.phone = phone; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } @Embedded public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
测试类:
package com.entity; import java.util.Date; import java.util.EnumSet; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.Test; public class TestStudents { @Test public void testShemaExport(){ //创建hibernate配置对象 Configuration config = new Configuration().configure(); //创建服务注册对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //生成SessionFactory SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); SchemaExport export = new SchemaExport(config); export.create(true,true); } @Test public void addStudents(){ //创建hibernate配置对象 Configuration config = new Configuration().configure(); //创建服务注册对象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //生成SessionFactory SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); //创建会话 Session session = sessionFactory.getCurrentSession(); //创建事务 Transaction tx = session.beginTransaction(); Address add = new Address("700005","河南理工大学","15039117115"); StudentPK pk = new StudentPK("012345678912345678","55555555"); Students s = new Students(pk,"张三丰","男",new Date(),"太极拳",add,7899); session.save(s); tx.commit(); // SchemaExport export = new SchemaExport(config); // export.create(true,true); } }
总结
以上就是本文关于hibernate属性级别注解实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:Hibernate核心思想与接口简介、Hibernate实现悲观锁和乐观锁代码介绍、快速了解hibernate配置文件与映射文件等。正所谓书中自有颜如玉,下面附几本不错的书籍,供大家学习参考,在此也非常希望大家对本站多多支持。
一键下载,免费的哦:
hibernate官方入门教程 中文PDF版
https://www.jb51.net/books/496598.html
Java设计模式深入研究 PDF
https://www.jb51.net/books/581548.html
数据挖掘:实用机器学习技术及Java实现(英文第2版)高清PDF
https://www.jb51.net/books/577815.html
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。