User.java
1 package com.test.bean;
2
3 import java.util.Date;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import javax.persistence.Basic;
8 import javax.persistence.CascadeType;
9 import javax.persistence.Column;
10 import javax.persistence.Entity;
11 import javax.persistence.FetchType;
12 import javax.persistence.GeneratedValue;
13 import javax.persistence.Id;
14 import javax.persistence.JoinColumn;
15 import javax.persistence.JoinTable;
16 import javax.persistence.Lob;
17 import javax.persistence.ManyToMany;
18 import javax.persistence.ManyToOne;
19 import javax.persistence.OneToOne;
20 import javax.persistence.Table;
21 import javax.persistence.Temporal;
22 import javax.persistence.TemporalType;
23 import javax.persistence.Transient;
24
25 import org.hibernate.annotations.GenericGenerator;
26 /**
27 * 標注@Entity注釋的類,表示該類是一個可持久化的實體
28 * 其中Entity中的“name”屬性表示實體的名稱,若不做設置,默認為標注實體的類的名稱,一般默認;
29 * 表@Table 屬性不區(qū)分大小寫
30 * name屬性表示實體所對應表的名稱,默認表名為實體的名稱。
31 * catalog和schema屬性表示實體指定的目錄名或是數(shù)據(jù)庫名,這根據(jù)不同的數(shù)據(jù)庫類型有所不同。
32 * uniqueConstraints屬性表示該實體所關聯(lián)的唯一約束條件,一個實體可以有多個唯一約束條件,默認沒有約束條件。
33 * 若使用uniqueConstraints標記時,需要配合標記UniqueConstraint標記來使用。
34 * 例如:uniqueConstraints = {@UniqueConstraint(columnNames = { "name", "email" }),@UniqueConstraint(columnNames = { "col_1", "col_2" })}
35 */
36 @Entity
37 @Table(name="UserInfos")
38 public class User {
39 // 普通屬性
40 /**
41 * 一個實體類至少要有一個主鍵(Primary Key),設置主鍵@Id
42 * 生成策略@GeneratedValue用于主鍵的生成策略
43 * @Target({METHOD, FIELD}) @Retention(RUNTIME)
44 * public @interface GeneratedValue {
45 * GenerationType strategy() default AUTO;
46 * strategy屬性表示生成主鍵的策略。有四種類型,分別定義在枚舉類型GenerationType中
47 *
48 * String generator() default "";
49 * generator為不同策略類型所對應的生成的規(guī)則名,它的值根據(jù)不同的策略有不同的設置。
50 * }
51 */
52 @Id
53 @GeneratedValue(generator="system-uuid")
54 @GenericGenerator(name="system-uuid", strategy="uuid")
55 @Column(length=32)
56 private String id;
57
58 /**
59 * @Target({METHOD, FIELD}) @Retention(RUNTIME)
60 * public @interface Column {
61 * String name() default "";
62 * 字段名稱
63 *
64 * boolean unique() default false;
65 * unique屬性表示該字段是否為唯一標識,默認為false。如果表中有一個字段需要唯一標識,則既可以使用該標記,
66 * 也可以使用@Table標記中的@UniqueConstraint。
67 *
68 * boolean nullable() default true;
69 * nullable屬性表示該字段是否可以為null值,默認為true。
70 *
71 * boolean insertable() default true;
72 * insertable屬性表示在使用“INSERT”腳本插入數(shù)據(jù)時,是否需要插入該字段的值。
73 *
74 * boolean updatable() default true;
75 * updatable屬性表示在使用“UPDATE”腳本插入數(shù)據(jù)時,是否需要更新該字段的值。
76 * insertable和updatable屬性一般多用于只讀的屬性,例如主鍵和外鍵等。這些字段的值通常是自動生成的。
77 *
78 * String columnDefinition() default "";
79 * columnDefinition屬性表示創(chuàng)建表時,該字段創(chuàng)建的SQL語句,一般用于通過Entity生成表定義時使用。
80 *
81 * String table() default "";
82 * table屬性表示當映射多個表時,指定表的表中的字段。默認值為主表的表名。
83 *
84 * int length() default 255;
85 * length屬性表示字段的長度,當字段的類型為varchar時,該屬性才有效,默認為255個字符。
86 *
87 * int precision() default 0;
88 * int scale() default 0;
89 * precision屬性和scale屬性表示精度,當字段類型為double時,precision表示數(shù)值的總長度,scale表示小數(shù)點所占的位數(shù)。
90 * }
91 *
92 *
93 *
94 * @Target({METHOD, FIELD}) @Retention(RUNTIME)
95 * public @interface Basic {
96 * FetchType fetch() default EAGER;
97 * 有兩種加載方式,LAZY和EAGER。EAGER 表示即時加載、LAZY表示惰性加載。默認為即時加載
98 *
99 * boolean optional() default true;
100 * 屬性optional表示屬性是否可為null,不能用于Java基本數(shù)據(jù)類型byte,int,short,long,boolean,char,float,double的使用。
101 * }
102 */
103 @Basic(fetch=FetchType.EAGER)
104 @Column(length = 50,name="USER_CODING", nullable=false)
105 private String userCoding;// 用戶編碼
106
107 @Column(length = 50,name="USER_NAME", nullable=false)
108 private String userName;
109
110 /**
111 * 標注@Temporal注釋來說明轉化成java.util包中的類型。
112 * 默認為TemporalType.TIMESTAMP類型
113 * java.sql.Date日期型,精確到年月日,例如“2008-08-08”
114 * java.sql.Time時間型,精確到時分秒,例如“20:00:00”
115 * java.sql.Timestamp時間戳,精確到納秒,例如“2008-08-08 20:00:00.000000001”
116 */
117 @Column(name="USER_BIRTHDAY")
118 @Temporal(TemporalType.DATE)
119 private Date userBirthday;// 用戶生日
120
121 @Column(length = 50,name="IDCARD")
122 private String IDCard;// 身份證號碼
123
124 /**
125 * 當容器加載實體時,將認為標注了@Transient注釋的屬性是非持久化的,將不會對應到表中的字段。
126 */
127 @Transient
128 private String userHomeplace;// 用戶出生地(籍貫)
129
130 @Column(length = 25,name="USER_STATURE")
131 private String userStature;// 用戶身高
132
133 @Column(length = 25,name="USER_AVOIRDUPOIS")
134 private String userAvoirdupois;// 用戶體重
135
136 @Column(length = 25,name="USER_HEALTH_STATUS")
137 private String userHealthStatus;// 用戶健康情況
138
139 @Column(length = 5, name="USER_MARRIAGE_STATUS")
140 private Integer userMarriageStatus;// 用戶婚姻情況 未婚:0 已婚:1
141
142 @Column(length = 25, name="USER_HOMEPHONE")
143 private String userHomephone;// 用戶固定電話
144
145 @Column(length = 25, name="USER_MOBILETELEPHONE")
146 private String userMobileTelephone;// 用戶移動電話
147
148 @Column(length = 50,name="USER_EMAIL")
149 private String userEmail;// 用戶電子郵箱
150
151 @Column(length = 50, name="USERD_WELLINGPLACE")
152 private String userDwellingPlace;// 用戶住址
153
154 @Column(length = 10, name="USER_POST_CODING")
155 private String userPostCoding;// 用戶郵政編碼
156
157 @Column(length = 50, name="DEGREE")
158 private String degree;// 臨時保存學歷用
159
160 @Column(length = 50, name="JOIN_WORK_DATE")
161 private String joinworkdate; // 參加工作日期
162
163 /**
164 * @Lob適用于標注字段類型為Clob和Blob類型。
165 * Clob(Character Large Ojects)類型是長字符串類型,映射為實體中的類型可為char[]、Character[]、或者String類型。
166 * Blob(Binary Large Objects)類型是字節(jié)類型,映射為實體中的類型可為byte[]、Byte[]、或者實現(xiàn)了Serializable接口的類。
167 * 因為這兩種類型的數(shù)據(jù)一般占用的內存空間比較大,所以通常使用惰性加載的方式,所以一般都要與@Basic標記同時使用,設置加載方式為FetchType.LAZY。
168 */
169 @Lob
170 @Column(name="USER_PHOTO")
171 @Basic(fetch=FetchType.LAZY)
172 private byte[] userPhoto;// 用戶照片
173
174 //關聯(lián)屬性《一對一外鍵關聯(lián)》一個user創(chuàng)建一個certificate
175 /**
176 * @Target({METHOD, FIELD}) @Retention(RUNTIME)
177 * public @interface OneToOne {
178 * Class targetEntity() default void.class;
179 * targetEntity屬性表示默認關聯(lián)的實體類型,默認為當前標注的實體類。例如使用默認設置與以下所示的設置的效果相同。一般情況使用默認設置就可以了。
180 *
181 * CascadeType[] cascade() default {};
182 * cascade屬性表示與此實體一對一關聯(lián)的實體的聯(lián)級樣式類型。聯(lián)級樣式是當對實體進行操作時策略,默認情況下,不關聯(lián)任何操作。
183 *
184 * FetchType fetch() default EAGER;
185 * fetch屬性是該實體的加載方式,默認為及時加載EAGER,也可以使用惰性加載LAZY。
186 *
187 * boolean optional() default true;
188 * optional屬性表示關聯(lián)的該實體是否能夠存在null值。默認為true,表示可以存在null值。
189 * 如果設置為false,則該實體不能為null,并且要同時配合使用@JoinColumn標記,將保存實體關系的字段設置為唯一的unique=true、
190 * 不為nullnullable=false,并且不能更新的updatable=false。
191 *
192 * String mappedBy() default "";
193 * mappedBy屬性用于雙向關聯(lián)實體時,標注在不保存關系的實體中
194 * }
195 *
196 *@Target({METHOD, FIELD}) @Retention(RUNTIME)
197 *public @interface JoinColumn {
198 * String name() default "";
199 * String referencedColumnName() default "";
200 * boolean unique() default false;
201 * boolean nullable() default true;
202 * boolean insertable() default true;
203 * boolean updatable() default true;
204 * String columnDefinition() default "";
205 * String table() default "";
206 *}
207 * 1.@JoinColumn與@Column標記一樣,是用于注釋表中的字段的。它的屬性與@Column屬性有很多相同之處
208 * 2.@JoinColumn注釋的是保存表與表之間關系的字段,它要標注在實體屬性上
209 * 3.與@Column標記一樣,name屬性是用來標識表中所對應的字段的名稱。
210 * 4.OneToOne中name=關聯(lián)表的名稱+“_”+ 關聯(lián)表主鍵的字段名
211 *
212 */
213 @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
214 @JoinColumn(name="CERTIFICATE_MAKER",unique=true, nullable=false,updatable=false)
215 private Certificate certificate;
216
217 // 關聯(lián)屬性《多對一 多個user屬于一個department和Nation》
218 /**
219 * @Target({METHOD, FIELD}) @Retention(RUNTIME)
220 * public @interface ManyToOne {
221 * Class targetEntity() default void.class;
222 * CascadeType[] cascade() default {};
223 * FetchType fetch() default EAGER;
224 * boolean optional() default true;
225 * }
226 */
227 @ManyToOne(cascade=CascadeType.ALL)
228 @JoinColumn(name="department_id")
229 private Department department;// 用戶部門信息
230
231 @ManyToOne(cascade=CascadeType.ALL)
232 @JoinColumn(name="userNation_id")
233 private Nation userNation;// 用戶的民族編碼
234
235
236 // 關聯(lián)屬性《多對多》
237 /**
238 * @Target({METHOD, FIELD})
239 * public @interface JoinTable {
240 * String name() default "";
241 * name屬性表示實體所對應表的名稱,name屬性為連接兩個表的表名稱。若不指定,則使用默認的表名稱如下所示。“表名1”+“_”+“表名2”。
242 *
243 * String catalog() default "";
244 * String schema() default "";
245 * catalog和schema屬性表示實體指定的目錄名或是數(shù)據(jù)庫名,這根據(jù)不同的數(shù)據(jù)庫類型有所不同。
246 *
247 * JoinColumn[] joinColumns() default {};
248 * joinColumns屬性表示,在保存關系中的表中,所保存關聯(lián)關系的外鍵的字段。并配合@JoinColumn標記使用。
249 *
250 * JoinColumn[] inverseJoinColumns() default {};
251 * inverseJoinColumns屬性與joinColumns屬性類似,它保存的是保存關系的另一個外鍵字段。
252 *
253 * UniqueConstraint[] uniqueConstraints default {};
254 * uniqueConstraints屬性表示該實體所關聯(lián)的唯一約束條件,一個實體可以有多個唯一約束條件,默認沒有約束條件。
255 * 若使用uniqueConstraints標記時,需要配合標記UniqueConstraint標記來使用。
256 * 例如:uniqueConstraints = {@UniqueConstraint(columnNames = { "name", "email" }),@UniqueConstraint(columnNames = { "col_1", "col_2" })}
257 * }
258 */
259 @ManyToMany(cascade=CascadeType.ALL)
260 @JoinTable(
261 name="re_user_item",
262 joinColumns={
263 @JoinColumn(name="user_id",referencedColumnName="id")
264 },
265 inverseJoinColumns={
266 @JoinColumn(name="item_id",referencedColumnName="id")
267 }
268 )
269 private Set<Item> items = new HashSet<Item>();// 用戶可以參加的項目
270
271 public String getId() {
272 return id;
273 }
274
275 public void setId(String id) {
276 this.id = id;
277 }
278
279 public String getUserCoding() {
280 return userCoding;
281 }
282
283 public void setUserCoding(String userCoding) {
284 this.userCoding = userCoding;
285 }
286
287 public String getUserName() {
288 return userName;
289 }
290
291 public void setUserName(String userName) {
292 this.userName = userName;
293 }
294
295 public Date getUserBirthday() {
296 return userBirthday;
297 }
298
299 public void setUserBirthday(Date userBirthday) {
300 this.userBirthday = userBirthday;
301 }
302
303 public String getIDCard() {
304 return IDCard;
305 }
306
307 public void setIDCard(String card) {
308 IDCard = card;
309 }
310
311 public String getUserHomeplace() {
312 return userHomeplace;
313 }
314
315 public void setUserHomeplace(String userHomeplace) {
316 this.userHomeplace = userHomeplace;
317 }
318
319 public String getUserStature() {
320 return userStature;
321 }
322
323 public void setUserStature(String userStature) {
324 this.userStature = userStature;
325 }
326
327 public String getUserAvoirdupois() {
328 return userAvoirdupois;
329 }
330
331 public void setUserAvoirdupois(String userAvoirdupois) {
332 this.userAvoirdupois = userAvoirdupois;
333 }
334
335 public String getUserHealthStatus() {
336 return userHealthStatus;
337 }
338
339 public void setUserHealthStatus(String userHealthStatus) {
340 this.userHealthStatus = userHealthStatus;
341 }
342
343 public Integer getUserMarriageStatus() {
344 return userMarriageStatus;
345 }
346
347 public void setUserMarriageStatus(Integer userMarriageStatus) {
348 this.userMarriageStatus = userMarriageStatus;
349 }
350
351 public String getUserHomephone() {
352 return userHomephone;
353 }
354
355 public void setUserHomephone(String userHomephone) {
356 this.userHomephone = userHomephone;
357 }
358
359 public String getUserMobileTelephone() {
360 return userMobileTelephone;
361 }
362
363 public void setUserMobileTelephone(String userMobileTelephone) {
364 this.userMobileTelephone = userMobileTelephone;
365 }
366
367 public String getUserEmail() {
368 return userEmail;
369 }
370
371 public void setUserEmail(String userEmail) {
372 this.userEmail = userEmail;
373 }
374
375 public String getUserDwellingPlace() {
376 return userDwellingPlace;
377 }
378
379 public void setUserDwellingPlace(String userDwellingPlace) {
380 this.userDwellingPlace = userDwellingPlace;
381 }
382
383 public String getUserPostCoding() {
384 return userPostCoding;
385 }
386
387 public void setUserPostCoding(String userPostCoding) {
388 this.userPostCoding = userPostCoding;
389 }
390
391 public String getDegree() {
392 return degree;
393 }
394
395 public void setDegree(String degree) {
396 this.degree = degree;
397 }
398
399 public String getJoinworkdate() {
400 return joinworkdate;
401 }
402
403 public void setJoinworkdate(String joinworkdate) {
404 this.joinworkdate = joinworkdate;
405 }
406
407 public byte[] getUserPhoto() {
408 return userPhoto;
409 }
410
411 public void setUserPhoto(byte[] userPhoto) {
412 this.userPhoto = userPhoto;
413 }
414
415 public Certificate getCertificate() {
416 return certificate;
417 }
418
419 public void setCertificate(Certificate certificate) {
420 this.certificate = certificate;
421 }
422
423 public Department getDepartment() {
424 return department;
425 }
426
427 public void setDepartment(Department department) {
428 this.department = department;
429 }
430
431 public Nation getUserNation() {
432 return userNation;
433 }
434
435 public void setUserNation(Nation userNation) {
436 this.userNation = userNation;
437 }
438
439 public Set<Item> getItems() {
440 return items;
441 }
442
443 public void setItems(Set<Item> items) {
444 this.items = items;
445 }
446
447 }
448
Certificate.java
1 package com.test.bean;
2
3 import javax.persistence.Column;
4 import javax.persistence.Entity;
5 import javax.persistence.GeneratedValue;
6 import javax.persistence.Id;
7 import javax.persistence.OneToOne;
8 import javax.persistence.Table;
9
10 import org.hibernate.annotations.GenericGenerator;
11
12 /**
13 *<br> 文 件 名: Certificate.java
14 *<br> 創(chuàng) 建 人: zzn
15 *<br> 創(chuàng)建日期: Jul 7, 2010 3:41:45 PM
16 */
17 @Entity
18 @Table(name="certificate")
19 public class Certificate {
20
21 @Id
22 @GeneratedValue(generator = "system-uuid")
23 @GenericGenerator(name = "system-uuid", strategy = "uuid")
24 @Column(length=32)
25 private String id;
26
27 @Column(name="CERTIFICATE_NAME", length=100, nullable=false)
28 private String certificateName;// 證書名稱
29
30 @Column(name="CERTIFICATE_CODE", length=100, nullable=false)
31 private String certificateCode;// 證書編號
32
33 @Column(name="VALID_DATE", length=50)
34 private String validDate;// 有效期;
35
36 @Column(name="MAKE_DATE", length=100)
37 private String makeDate;// 創(chuàng)建日期
38
39 @OneToOne(mappedBy = "certificate")
40 private User user;
41
42 public User getUser() {
43 return user;
44 }
45 public void setUser(User user) {
46 this.user = user;
47 }
48 public String getId() {
49 return id;
50 }
51 public void setId(String id) {
52 this.id = id;
53 }
54 public String getCertificateName() {
55 return certificateName;
56 }
57 public void setCertificateName(String certificateName) {
58 this.certificateName = certificateName;
59 }
60 public String getCertificateCode() {
61 return certificateCode;
62 }
63 public void setCertificateCode(String certificateCode) {
64 this.certificateCode = certificateCode;
65 }
66 public String getValidDate() {
67 return validDate;
68 }
69 public void setValidDate(String validDate) {
70 this.validDate = validDate;
71 }
72 public String getMakeDate() {
73 return makeDate;
74 }
75 public void setMakeDate(String makeDate) {
76 this.makeDate = makeDate;
77 }
78
79 }
80
Nation.java
1 package com.test.bean;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import javax.persistence.Column;
7 import javax.persistence.Entity;
8 import javax.persistence.GeneratedValue;
9 import javax.persistence.Id;
10 import javax.persistence.OneToMany;
11 import javax.persistence.Table;
12
13 import org.hibernate.annotations.GenericGenerator;
14
15 /**
16 *<br> 文 件 名: Nation.java
17 *<br> 創(chuàng) 建 人: zzn
18 *<br> 創(chuàng)建日期: Jul 7, 2010 3:41:34 PM
19 */
20 @Entity
21 @Table(name="nation")
22 public class Nation {
23
24 @Id
25 @GeneratedValue(generator = "system-uuid")
26 @GenericGenerator(name = "system-uuid", strategy = "uuid")
27 @Column(length=32)
28 private String id;
29
30 @Column(length=50,name="NATION_NAME")
31 private String nationName;// 民族名稱
32
33 /**
34 * 如果使用了List集合,可以同時配合注釋@OrderBy使查詢出來的集合類按照一定的順序排列
35 * 例如:@OrderBy("id ASC,postcode DESC")postcode是屬性
36 * @OneToMany(mappedBy="customer")
37 * @OrderBy("postcode ASC")
38 * public List<AddressEO> getAddresses() {
39 * return addresses;
40 * }
41 *
42 * 如果使用了Map集合,可以同時配合注釋@MapKey指定Map中存放的key值。
43 * 如:@MapKey(name="id")將AddressEO中的屬性id作為key值保存在Map中。
44 * @OneToMany(mappedBy="customer")
45 * @MapKey(name="id")
46 * public Map<Integer, AddressEO> getAddresses() {
47 * return addresses;
48 * }
49 */
50 @OneToMany(mappedBy="userNation")
51 private Set<User> user = new HashSet<User>();
52
53 @Override
54 public int hashCode() {
55 final int PRIME = 31;
56 int result = 1;
57 result = PRIME * result + ((id == null) ? 0 : id.hashCode());
58 result = PRIME * result + ((nationName == null) ? 0 : nationName.hashCode());
59 return result;
60 }
61
62 @Override
63 public boolean equals(Object obj) {
64 if (this == obj)
65 return true;
66 if (obj == null)
67 return false;
68 if (getClass() != obj.getClass())
69 return false;
70 final Nation other = (Nation) obj;
71 if (id == null) {
72 if (other.id != null)
73 return false;
74 } else if (!id.equals(other.id))
75 return false;
76 if (nationName == null) {
77 if (other.nationName != null)
78 return false;
79 } else if (!nationName.equals(other.nationName))
80 return false;
81 return true;
82 }
83
84 public String getId() {
85 return id;
86 }
87
88 public void setId(String id) {
89 this.id = id;
90 }
91
92 public String getNationName() {
93 return nationName;
94 }
95
96 public void setNationName(String nationName) {
97 this.nationName = nationName;
98 }
99
100 public Set<User> getUser() {
101 return user;
102 }
103
104 public void setUser(Set<User> user) {
105 this.user = user;
106 }
107
108
109 }
110
Department.java
1 package com.test.bean;
2
3 import java.util.Date;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import javax.persistence.Column;
8 import javax.persistence.Entity;
9 import javax.persistence.GeneratedValue;
10 import javax.persistence.Id;
11 import javax.persistence.OneToMany;
12 import javax.persistence.Table;
13 import javax.persistence.Temporal;
14 import javax.persistence.TemporalType;
15
16 import org.hibernate.annotations.GenericGenerator;
17
18
19 /**
20 *<br> 文 件 名: Department.java
21 *<br> 創(chuàng) 建 人: zzn
22 *<br> 創(chuàng)建日期: Jul 7, 2010 1:08:09 PM
23 */
24 @Entity
25 @Table(name="DEPARTMENTS")
26 public class Department {
27
28 @Id
29 @GeneratedValue(generator = "system-uuid")
30 @GenericGenerator(name = "system-uuid", strategy = "uuid")
31 @Column(length=32)
32 private String id;
33
34 @Column(name="CREATE_TIME")
35 @Temporal(TemporalType.DATE)
36 private Date createTime; //創(chuàng)建時間
37
38 @Column(name="DEPARTMENT_NAME", length=100)
39 private String departmentName;// 部門名稱
40
41 @Column(name="DEPARTMENT_CODING", length=100)
42 private String departmentCoding;// 部門編號
43
44 @Column(name="DEPARTMENT_MANAGER", length=100)
45 private String departmentManager;// 部門經理
46
47 @Column(name="DEPARTMEN_TTELEPHONE", length=50)
48 private String departmentTelephone;// 聯(lián)系電話
49
50 @Column(name="DEPARTMENT_FOUNCTION_DESC", length=1000)
51 private String departmentFounctionDesc;// 部門職能描述
52
53 @Column(name="LINK_MAN", length=25)
54 private String linkMan;// 培訓聯(lián)系人
55
56 /**
57 * @Target({METHOD, FIELD}) @Retention(RUNTIME)
58 * public @interface OneToMany {
59 * Class targetEntity() default void.class;
60 * targetEntity屬性表示默認關聯(lián)的實體類型。因為一對多的實體集合時保存在集合類中,所以必須指明集合類中保存的具體類型。
61 * 1/指定集合泛型的具體類型;2/指定targetEntity屬性類型
62 * 1.本例;2.@OneToMany(targetEntity=User.class,cascade = CascadeType.ALL)
63 *
64 * CascadeType[] cascade() default {};
65 * cascade屬性表示與此實體一對多關聯(lián)的實體的聯(lián)級樣式類型。聯(lián)級樣式是當對實體進行操作時策略,默認情況下,不關聯(lián)任何操作。
66 * 本例:當刪除Department時會同時刪除所有的User
67 *
68 * FetchType fetch() default LAZY;
69 * fetch屬性默認為惰性加載LAZY的,本例:加載Department延遲加載User
70 *
71 * String mappedBy() default "";
72 * mappedBy屬性只有在實體間雙向關聯(lián)時使用。
73 * mappedBy屬性的值為User實體中所引用的department實體的屬性名
74 * }
75 */
76 @OneToMany(mappedBy="department")
77 private Set<User> userInfos = new HashSet<User>();// 部門成員信息
78
79 // private Department fatherDepartment;// 上級直屬部門
80
81 // private Set<Department> departments = new HashSet<Department>();// 所包含部門
82
83 // private Set<String> parentsId = new HashSet<String>();//父部門id
84
85 public String getId() {
86 return id;
87 }
88 public void setId(String id) {
89 this.id = id;
90 }
91 public Date getCreateTime() {
92 return createTime;
93 }
94 public void setCreateTime(Date createTime) {
95 this.createTime = createTime;
96 }
97 public String getDepartmentName() {
98 return departmentName;
99 }
100 public void setDepartmentName(String departmentName) {
101 this.departmentName = departmentName;
102 }
103 public String getDepartmentCoding() {
104 return departmentCoding;
105 }
106 public void setDepartmentCoding(String departmentCoding) {
107 this.departmentCoding = departmentCoding;
108 }
109 public String getDepartmentManager() {
110 return departmentManager;
111 }
112 public void setDepartmentManager(String departmentManager) {
113 this.departmentManager = departmentManager;
114 }
115 public String getDepartmentTelephone() {
116 return departmentTelephone;
117 }
118 public void setDepartmentTelephone(String departmentTelephone) {
119 this.departmentTelephone = departmentTelephone;
120 }
121 public String getDepartmentFounctionDesc() {
122 return departmentFounctionDesc;
123 }
124 public void setDepartmentFounctionDesc(String departmentFounctionDesc) {
125 this.departmentFounctionDesc = departmentFounctionDesc;
126 }
127 public String getLinkMan() {
128 return linkMan;
129 }
130 public void setLinkMan(String linkMan) {
131 this.linkMan = linkMan;
132 }
133 public void setUserInfos(Set<User> userInfos) {
134 this.userInfos = userInfos;
135 }
136 public Set<User> getUserInfos() {
137 return userInfos;
138 }
139 // public Department getFatherDepartment() {
140 // return fatherDepartment;
141 // }
142 // public void setFatherDepartment(Department fatherDepartment) {
143 // this.fatherDepartment = fatherDepartment;
144 // }
145 // public Set<Department> getDepartments() {
146 // return departments;
147 // }
148 // public Set<String> getParentsId() {
149 // return parentsId;
150 // }
151 // public void setDepartments(Set<Department> departments) {
152 // this.departments = departments;
153 // }
154 // public void setParentsId(Set<String> parentsId) {
155 // this.parentsId = parentsId;
156 // }
157
158 }
159
Item.java
1 package com.test.bean;
2
3 import java.util.Date;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import javax.persistence.Column;
8 import javax.persistence.Entity;
9 import javax.persistence.EnumType;
10 import javax.persistence.Enumerated;
11 import javax.persistence.GeneratedValue;
12 import javax.persistence.Id;
13 import javax.persistence.ManyToMany;
14 import javax.persistence.Table;
15 import javax.persistence.Temporal;
16 import javax.persistence.TemporalType;
17
18 import org.hibernate.annotations.GenericGenerator;
19
20 /**
21 *<br> 文 件 名: Item.java
22 *<br> 創(chuàng) 建 人: zzn
23 *<br> 創(chuàng)建日期: Jul 7, 2010 3:42:30 PM
24 */
25 @Entity
26 @Table(name="item")
27 public class Item {
28
29 @Id
30 @GeneratedValue(generator = "system-uuid")
31 @GenericGenerator(name = "system-uuid", strategy = "uuid")
32 @Column(length=32)
33 private String id;
34
35 @Column(name="ITEM_NUMBER", length=100)
36 private String itemNumber; /* 項目_編號 */
37
38 @Column(name="ITEM_NAME", length=100)
39 private String name; /* 項目名稱 */
40
41 @Column(name="TYPE", length=10)
42 private String type; /* 所含類型 */ //1.外送 2.內部 3.在崗輔導 4.在線學習 5.學歷與進修
43
44 @Temporal(TemporalType.DATE)
45 @Column(name="START_TIME")
46 private Date startTime; /* 開始時間 */
47
48 @Temporal(TemporalType.DATE)
49 @Column(name="END_TIME")
50 private Date endTime; /* 結束時間 */
51
52 @Temporal(TemporalType.DATE)
53 @Column(name="EDIT_TIME")
54 private Date editTime; /* 編輯時間 */
55
56 @Column(name="ITEM_TIME", length=10)
57 private Integer item_time; /*項目學時*/
58
59 @Column(name="CREDIT_HOUR", length=10)
60 private Integer creditHour;/* 獲得學分 */
61
62 @Column(name="TRAIN_MANAGER", length=10)
63 private String trainManager; /* 培訓經理 */
64
65 @Column(name="ITEM_TARGER", length=1000)
66 private String itemTarget; /* 項目目標 */
67
68 @Column(name="TRAINING_ADDRESS", length=200)
69 private String trainingAddress; //培訓地址
70
71 @Column(name="REMARK", length=1000)
72 private String remark;//備注
73
74 @Column(name="ITEM_STATUS", length=20)
75 @Enumerated(EnumType.STRING)
76 private String status;
77
78 //項目狀態(tài)
79 public enum itemStatus{
80 未開始,運行中,關閉
81 }
82
83 @Column(name="ITEM_PROPERTY", length=20)
84 @Enumerated(EnumType.STRING)
85 private String property;
86
87 public enum itemProperty{
88 新員工類,崗前培訓類,定崗或轉正培訓類,績效提升類,晉升類
89 }
90
91 @ManyToMany(mappedBy="items")
92 private Set<User> userId = new HashSet<User>(); // 針對選修項目,指定可以參與該項目的用戶
93
94 // private Department depaId;//所屬部門;項目(n)-->部門(1)(單向)
95
96 // private Set<Certificate> certificates;//項目頒發(fā)的證書
97
98 public String getId() {
99 return id;
100 }
101 public void setId(String id) {
102 this.id = id;
103 }
104 public String getItemNumber() {
105 return itemNumber;
106 }
107 public void setItemNumber(String itemNumber) {
108 this.itemNumber = itemNumber;
109 }
110 public String getName() {
111 return name;
112 }
113 public void setName(String name) {
114 this.name = name;
115 }
116 public String getType() {
117 return type;
118 }
119 public void setType(String type) {
120 this.type = type;
121 }
122 public Date getStartTime() {
123 return startTime;
124 }
125 public void setStartTime(Date startTime) {
126 this.startTime = startTime;
127 }
128 public Date getEndTime() {
129 return endTime;
130 }
131 public void setEndTime(Date endTime) {
132 this.endTime = endTime;
133 }
134 public Date getEditTime() {
135 return editTime;
136 }
137 public void setEditTime(Date editTime) {
138 this.editTime = editTime;
139 }
140 public Integer getItem_time() {
141 return item_time;
142 }
143 public void setItem_time(Integer item_time) {
144 this.item_time = item_time;
145 }
146 public Integer getCreditHour() {
147 return creditHour;
148 }
149 public void setCreditHour(Integer creditHour) {
150 this.creditHour = creditHour;
151 }
152 public String getTrainManager() {
153 return trainManager;
154 }
155 public void setTrainManager(String trainManager) {
156 this.trainManager = trainManager;
157 }
158 public Set<User> getUserId() {
159 return userId;
160 }
161 public void setUserId(Set<User> userId) {
162 this.userId = userId;
163 }
164 public String getItemTarget() {
165 return itemTarget;
166 }
167 public void setItemTarget(String itemTarget) {
168 this.itemTarget = itemTarget;
169 }
170 public String getTrainingAddress() {
171 return trainingAddress;
172 }
173 public void setTrainingAddress(String trainingAddress) {
174 this.trainingAddress = trainingAddress;
175 }
176 public String getRemark() {
177 return remark;
178 }
179 public void setRemark(String remark) {
180 this.remark = remark;
181 }
182 // public Department getDepaId() {
183 // return depaId;
184 // }
185 // public void setDepaId(Department depaId) {
186 // this.depaId = depaId;
187 // }
188 public String getProperty() {
189 return property;
190 }
191 public void setProperty(String property) {
192 this.property = property;
193 }
194 public String getStatus() {
195 return status;
196 }
197 public void setStatus(String status) {
198 this.status = status;
199 }
200
201 // public Set<Certificate> getCertificates() {
202 // return certificates;
203 // }
204 // public void setCertificates(Set<Certificate> certificates) {
205 // this.certificates = certificates;
206 // }
207 }
208
Resource.java
1 package com.test.bean;
2
3 import java.io.Serializable;
4
5 import javax.persistence.Column;
6 import javax.persistence.GeneratedValue;
7 import javax.persistence.Id;
8
9 import org.hibernate.annotations.GenericGenerator;
10
11 /**
12 *<br> 文 件 名: Resources.java
13 *<br> 創(chuàng) 建 人: zzn
14 *<br> 創(chuàng)建日期: Jul 14, 2010 10:15:39 AM
15 */
16
17 /**
18 *####################################################################################################################################
19 * 1.繼承關系的實體保存在一個表(Single Table per Class Hierarchy Strategy)
20 * 建議:這種方法雖然只有一個表,但是會出現(xiàn)很多的null值,所以不建議使用
21 * 繼承關系的實體中,所有的實體類都映射到一個表中,表中使用一個特殊的標識字段(discriminator column),來標識一條記錄屬于那個子類。
22 * 如:
23 * @Entity
24 * 使用@Entity注釋,標識該類以及所有的子類都映射到指定的表中,如果不標注,也可使用默認值。
25 *
26 * @Table(name = "Resource")
27 *
28 * @Inheritance(strategy=InheritanceType.SINGLE_TABLE)
29 * 使用@Inheritance注釋,標識該類的子類繼承映射的方式,InheritanceType有三種類型,三種類型定義為常量,默認為SINGLE_TABLE。
30 * public enum InheritanceType{ SINGLE_TABLE, JOINED, TABLE_PER_CLASS };
31 * SINGLE_TABLE表示繼承關系的實體保存在一個表;JOINED表示每個實體子類保存在一個表;TABLE_PER_CLASS表示每個實體類保存在一個表
32 *
33 * @DiscriminatorColumn(name="resource_type",discriminatorType=DiscriminatorType.STRING)
34 * 使用@DiscriminatorColumn注釋,標識改繼承層次上所區(qū)別每個實體的類型字段。
35 * @Target({TYPE}) @Retention(RUNTIME)
36 * public @interface DiscriminatorColumn {
37 * String name() default "DTYPE";
38 * DiscriminatorType discriminatorType() default STRING;
39 * String columnDefinition() default "";
40 * int length() default 31;
41 * }
42 * 注意:
43 * 1.@DiscriminatorColumn只能標注在頂層的類中,而不能標注在子類中。
44 * 2.@DiscriminatorColumn只在繼承策略為“SINGLE_TABLE”和“JOINED”時使用。
45 * 3.name屬性表示所標識具體類型的字段名稱,默認為“DTYPE”
46 * 4.discriminatorType屬性表示標識值的類型,默認為STRING字符串。public enum DiscriminatorType { STRING, CHAR, INTEGER };
47 * 5.columnDefinition屬性表示生成字段的DDL語句,與@Column中的columnDefinition屬性類似。
48 * 6.length屬性表示為標識值的長度,默認為31。該屬性只在使用DiscriminatorType. STRING時才需要設置。
49 *
50 * @DiscriminatorValue("resource")
51 * 使用@DiscriminatorValue注釋,標注該實體類所實體標識字段的值.value的值表示所該實體的標注值。
52 * 如 @DiscriminatorValue("courseware")表示 標識字段resource_type的值為courseware是,可以認為是Courseware實體
53 *####################################################################################################################################
54 * 2.每個子類實體保存在一個表(Joined Subclass Strategy)
55 * 所有實體類的基類保存在一個表中,每增加一個子類增加一個映射子類的表。子類的表與父類中的通過主鍵(Primary Key)來關聯(lián)。類似父類與子類之間是一對一的關系映射。
56 * 如:
57 * @Entity
58 * @Table(name = "Resource")
59 * @Inheritance(strategy=InheritanceType.JOINED)
60 * @DiscriminatorColumn(name="resource_type",discriminatorType=DiscriminatorType.STRING)
61 * @DiscriminatorValue("resource")
62 * 注意:這種用的比較多。采用每個實體類保存在一個表的繼承策略,雖然避免了表中大量null值的數(shù)據(jù),但每個實體是通過關聯(lián)來獲得的。
63 * 當有多個子類時,進行大量的查詢會耗時很大,所以采取此策略時需要注意這些問題。
64 *#####################################################################################################################################
65 * 3.每個實體類保存在一個表(Table per Class Strategy)
66 * 每個實體都保存為一個表中,每個表中都包含實體類的所有屬性(父類的和子類的)。
67 * @Entity
68 * @Table(name = "Resource")
69 * @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
70 * @TableGenerator(
71 * name = "employee_gen",
72 * able = "tb_generator",
73 * pkColumnName = "gen_name",
74 * valueColumnName = "gen_value",
75 * pkColumnValue = "employee_id",
76 * allocationSize = 1)
77 */
78 public class Resource implements Serializable{
79 private static final long serialVersionUID = 8763866117518530428L;
80 @Id
81 @GeneratedValue(generator="system-uuid")
82 @GenericGenerator(name="system-uuid", strategy="uuid")
83 @Column(length=32)
84 private String id;
85
86 @Column(length = 50,name="NAME", nullable=false)
87 private String name; //名稱
88
89 @Column(length = 50,name="resNumber")
90 private String resNumber; //編號
91
92 @Column(length = 1000,name="description")
93 private String description; //描述
94
95 public String getId() {
96 return id;
97 }
98
99 public void setId(String id) {
100 this.id = id;
101 }
102
103 public String getName() {
104 return name;
105 }
106
107 public void setName(String name) {
108 this.name = name;
109 }
110
111 public String getResNumber() {
112 return resNumber;
113 }
114
115 public void setResNumber(String resNumber) {
116 this.resNumber = resNumber;
117 }
118
119 public String getDescription() {
120 return description;
121 }
122
123 public void setDescription(String description) {
124 this.description = description;
125 }
126
127
128
129 }
130
Courseware.java
1 package com.test.bean;
2
3 import java.io.Serializable;
4
5 import javax.persistence.Column;
6
7 /**
8 *<br> 文 件 名: Courseware.java
9 *<br> 創(chuàng) 建 人: zzn
10 *<br> 創(chuàng)建日期: Jul 14, 2010 10:43:05 AM
11 */
12 /**
13 * 1.繼承關系的實體保存在一個表
14 * @Entity
15 * @DiscriminatorValue("courseware")
16 *
17 * 2.每個子類實體保存在一個表
18 * @Entity
19 * @Table(name = "courseware_resource")
20 * @DiscriminatorValue("courseware")
21 * @PrimaryKeyJoinColumn(name="id")
22 *
23 * 3.每個實體類保存在一個表
24 * @Entity
25 * @Table(name = "courseware")
26 *
27 */
28
29 public class Courseware extends Resource implements Serializable {
30 private static final long serialVersionUID = 5859492207003397084L;
31
32 /**
33 * 如果是1.繼承關系的實體保存在一個表,id用的是父類的id
34 * 如果是2.每個子類實體保存在一個表,主鍵關聯(lián)
35 *
36 */
37 //private String id;
38
39 @Column(name="courseTime")
40 private Integer courseTime; //學時
41
42 @Column(name="coursePoint")
43 private Integer coursePoint; //學分
44
45 @Column(name="aim", length=1000)
46 private String aim; //目標
47
48 // @Override
49 // public String getId() {
50 // return id;
51 // }
52 //
53 // @Override
54 // public void setId(String id) {
55 // this.id = id;
56 // }
57
58 public Integer getCourseTime() {
59 return courseTime;
60 }
61
62 public void setCourseTime(Integer courseTime) {
63 this.courseTime = courseTime;
64 }
65
66 public Integer getCoursePoint() {
67 return coursePoint;
68 }
69
70 public void setCoursePoint(Integer coursePoint) {
71 this.coursePoint = coursePoint;
72 }
73
74 public String getAim() {
75 return aim;
76 }
77
78 public void setAim(String aim) {
79 this.aim = aim;
80 }
81
82 }
83
參考資料:
http://blog.csdn.net/EJB_JPA/archive/2008/05.aspx