服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - hibernate属性级别注解实例代码

hibernate属性级别注解实例代码

2021-01-22 11:04黄小鱼ZZZ Java教程

这篇文章主要介绍了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配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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实体类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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();
  }
}

地址类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
  }
    
}

测试类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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属性级别注解实例代码的全部内容,希望对大家有所帮助。在此也非常希望大家对本站多多支持。

原文链接:http://blog.csdn.net/wojiaohuangyu/article/details/51768672

延伸 · 阅读

精彩推荐