当前位置:   article > 正文

Hiberate基础用法实例

hibernatey用法

下面通过一个实际的列子来展示Hibernate的基本用法。

实例:

  1. package com.ydoing.domain;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import javax.persistence.Basic;
  10. import javax.persistence.CollectionTable;
  11. import javax.persistence.Column;
  12. import javax.persistence.ElementCollection;
  13. import javax.persistence.Entity;
  14. import javax.persistence.FetchType;
  15. import javax.persistence.GeneratedValue;
  16. import javax.persistence.GenerationType;
  17. import javax.persistence.Id;
  18. import javax.persistence.JoinColumn;
  19. import javax.persistence.Lob;
  20. import javax.persistence.MapKeyClass;
  21. import javax.persistence.MapKeyColumn;
  22. import javax.persistence.OrderColumn;
  23. import javax.persistence.Table;
  24. import javax.persistence.Temporal;
  25. import javax.persistence.TemporalType;
  26. import javax.persistence.Transient;
  27. import org.hibernate.annotations.Formula;
  28. @Entity
  29. @Table(name = "person")
  30. public class Person {
  31. // 主键为ID,最好设置为包装类型Integer
  32. @Id
  33. @Column(name = "person_id")
  34. @GeneratedValue(strategy = GenerationType.AUTO)
  35. private Integer id;
  36. // 指定字符长度
  37. @Column(name = "name", length = 50, nullable = false)
  38. private String name;
  39. @Column(name = "age")
  40. private int age;
  41. // 也可以不指定列名,默认为类的属性名
  42. @Column
  43. private String sex;
  44. // @Transient表示不用持久化
  45. @Transient
  46. private String alias;
  47. // @Lob表示大数据类型,FetchType.LAZY表示延迟加载,减少性能开销
  48. @Lob
  49. @Basic(fetch = FetchType.LAZY)
  50. private byte[] image;
  51. // 指定为日期类型
  52. @Temporal(TemporalType.DATE)
  53. private Date birthdate;
  54. // 属性动态生成
  55. @Formula("(select concat(p.name, p.age, p.sex) from person p where p.person_id = person_id)")
  56. private String profile;
  57. // List集合类型
  58. // 需要显示初始化集合的类型
  59. @ElementCollection(targetClass = String.class)
  60. @CollectionTable(name = "pet_inf",// 指定表名
  61. // 用于映射外键
  62. joinColumns = @JoinColumn(name = "person_id", nullable = false))
  63. @Column(name = "pet_name")
  64. @OrderColumn(name = "list_order")
  65. private List<String> pets = new ArrayList<>();
  66. // 数组属性
  67. @ElementCollection(targetClass = String.class)
  68. @CollectionTable(name = "school_inf", joinColumns = @JoinColumn(name = "person_id", nullable = false))
  69. @Column(name = "school_name")
  70. @OrderColumn(name = "array_order")
  71. private String[] schools;
  72. // Set集合属性,Set是无序的,不可重复的,因此Set集合属性无须使用@OrderColumn注解映射集合的索引列
  73. @ElementCollection(targetClass = String.class)
  74. @CollectionTable(name = "car_inf", joinColumns = @JoinColumn(name = "person_id", nullable = false))
  75. @Column(name = "car_name")
  76. private Set<String> cars = new HashSet<>();// 必须指定接口实现类
  77. // Map集合属性
  78. @ElementCollection(targetClass = Float.class)
  79. @CollectionTable(name = "salary_inf", joinColumns = @JoinColumn(name = "person_id", nullable = false))
  80. // 指定key的类型
  81. @MapKeyColumn(name = "salary_type")
  82. @MapKeyClass(String.class)
  83. // 映射Map的value
  84. @Column(name = "money")
  85. private Map<String, Float> payroll = new HashMap<>(); // 必须指定接口实现类
  86. // 映射组件属性,所谓组件的意思是:不是基本数据类型,而是自定义的复合类型,且不是持久化实体
  87. private Address address;
  88. public Address getAddress() {
  89. return address;
  90. }
  91. public void setAddress(Address address) {
  92. this.address = address;
  93. }
  94. public Map<String, Float> getPayroll() {
  95. return payroll;
  96. }
  97. public void setPayroll(Map<String, Float> payroll) {
  98. this.payroll = payroll;
  99. }
  100. public String[] getSchools() {
  101. return schools;
  102. }
  103. public void setSchools(String[] schools) {
  104. this.schools = schools;
  105. }
  106. public String getSex() {
  107. return sex;
  108. }
  109. public Set<String> getCars() {
  110. return cars;
  111. }
  112. public void setCars(Set<String> cars) {
  113. this.cars = cars;
  114. }
  115. public void setSex(String sex) {
  116. this.sex = sex;
  117. }
  118. public String getProfile() {
  119. return profile;
  120. }
  121. public void setProfile(String profile) {
  122. this.profile = profile;
  123. }
  124. public Person() {
  125. }
  126. public int getId() {
  127. return id;
  128. }
  129. public void setId(int id) {
  130. this.id = id;
  131. }
  132. public String getName() {
  133. return name;
  134. }
  135. public void setName(String name) {
  136. this.name = name;
  137. }
  138. public int getAge() {
  139. return age;
  140. }
  141. public void setAge(int age) {
  142. this.age = age;
  143. }
  144. public String getAlias() {
  145. return alias;
  146. }
  147. public void setAlias(String alias) {
  148. this.alias = alias;
  149. }
  150. public byte[] getImage() {
  151. return image;
  152. }
  153. public void setImage(byte[] image) {
  154. this.image = image;
  155. }
  156. public Date getBirthdate() {
  157. return birthdate;
  158. }
  159. public void setBirthdate(Date birthdate) {
  160. this.birthdate = birthdate;
  161. }
  162. public List<String> getPets() {
  163. return pets;
  164. }
  165. public void setPets(List<String> pets) {
  166. this.pets = pets;
  167. }
  168. public void setId(Integer id) {
  169. this.id = id;
  170. }
  171. }
  • 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
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217

组件类

  1. package com.ydoing.domain;
  2. import javax.persistence.Column;
  3. import javax.persistence.Embeddable;
  4. import org.hibernate.annotations.Parent;
  5. @Embeddable
  6. public class Address {
  7. @Column(name = "person_province")
  8. private String province;
  9. @Column(name = "person_city")
  10. private String city;
  11. public Address() {
  12. }
  13. // 属于哪个实体组件
  14. @Parent
  15. private Person person;
  16. public Person getPerson() {
  17. return person;
  18. }
  19. public void setPerson(Person person) {
  20. this.person = person;
  21. }
  22. public Address(String province, String city) {
  23. super();
  24. this.province = province;
  25. this.city = city;
  26. }
  27. public String getProvince() {
  28. return province;
  29. }
  30. public void setProvince(String province) {
  31. this.province = province;
  32. }
  33. public String getCity() {
  34. return city;
  35. }
  36. public void setCity(String city) {
  37. this.city = city;
  38. }
  39. }
  • 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

测试:

  1. package com.ydoing.test;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import org.hibernate.Session;
  5. import org.hibernate.SessionFactory;
  6. import org.hibernate.Transaction;
  7. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  8. import org.hibernate.cfg.Configuration;
  9. import org.hibernate.service.ServiceRegistry;
  10. import com.ydoing.domain.Address;
  11. import com.ydoing.domain.Person;
  12. public class Main {
  13. public static void main(String[] args) {
  14. Configuration conf = new Configuration();
  15. conf.configure();
  16. // ServiceRegistry serviceRegistry = new
  17. // ServiceRegistryBuilder().applySettings(conf.getProperties())
  18. // .buildServiceRegistry();
  19. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(conf.getProperties())
  20. .build();
  21. SessionFactory factory = conf.buildSessionFactory(serviceRegistry);
  22. Session session = factory.openSession();
  23. Person person = new Person();
  24. person.setName("Jack");
  25. person.setAge(22);
  26. person.setSex("男");
  27. // 不会持久化
  28. person.setAlias("Arron");
  29. // List属性
  30. String[] array = { "cat", "dog", "snake" };
  31. List<String> pets = Arrays.asList(array);
  32. person.setPets(pets);
  33. // 数组属性
  34. String[] schools = { "清华", "北大" };
  35. person.setSchools(schools);
  36. // Set集合属性
  37. person.getCars().add("奥迪");
  38. person.getCars().add("大众");
  39. // Map属性
  40. person.getPayroll().put("基本工资", (float) 5000);
  41. person.getPayroll().put("绩效", (float) 3000);
  42. person.setAddress(new Address("浙江", "杭州"));
  43. Transaction tx = session.getTransaction();
  44. tx.begin();
  45. session.save(person);
  46. Person p = (Person) session.load(Person.class, 12);
  47. System.out.println(p.getProfile());
  48. tx.commit();
  49. session.close();
  50. }
  51. }
  • 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
<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>

版权声明:本文为博主原创文章,未经博主允许不得转载。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/870874
推荐阅读
相关标签
  

闽ICP备14008679号