想飞就别怕摔

大爷的并TM骂人

Hibernate Annotation -- (JPA)

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 属性不区分大小写
 30  * name属性表示实体所对应表的名称,默认表名为实体的名称。
 31  * catalog和schema属性表示实体指定的目录名或是数据库名,这根据不同的数据库类型有所不同。
 32  * uniqueConstraints属性表示该实体所关联的唯一约束条件,一个实体可以有多个唯一约束条件,默认没有约束条件。
 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为不同策略类型所对应的生成的规则名,它的值根据不同的策略有不同的设置。
 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”脚本插入数据时,是否需要插入该字段的值。
 73      * 
 74      *     boolean updatable() default true;
 75      *         updatable属性表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。
 76      *         insertable和updatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。
 77      * 
 78      *     String columnDefinition() default "";
 79      *         columnDefinition属性表示创建表时,该字段创建的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表示数值的总长度,scale表示小数点所占的位数。
 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基本数据类型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)类型是字节类型,映射为实体中的类型可为byte[]、Byte[]、或者实现了Serializable接口的类。
167      * 因为这两种类型的数据一般占用的内存空间比较大,所以通常使用惰性加载的方式,所以一般都要与@Basic标记同时使用,设置加载方式为FetchType.LAZY。
168      */
169     @Lob
170     @Column(name="USER_PHOTO")
171     @Basic(fetch=FetchType.LAZY)
172     private byte[] userPhoto;// 用户照片
173     
174     //关联属性《一对一外键关联》一个user创建一个certificate
175     /**
176      * @Target({METHOD, FIELD}) @Retention(RUNTIME)
177      *        public @interface OneToOne {
178      *        Class targetEntity() default void.class;
179      *        targetEntity属性表示默认关联的实体类型,默认为当前标注的实体类。例如使用默认设置与以下所示的设置的效果相同。一般情况使用默认设置就可以了。
180      *
181      *        CascadeType[] cascade() default {};
182      *        cascade属性表示与此实体一对一关联的实体的联级样式类型。联级样式是当对实体进行操作时策略,默认情况下,不关联任何操作。
183      *
184      *        FetchType fetch() default EAGER;
185      *        fetch属性是该实体的加载方式,默认为及时加载EAGER,也可以使用惰性加载LAZY。
186      *
187      *        boolean optional() default true;
188      *        optional属性表示关联的该实体是否能够存在null值。默认为true,表示可以存在null值。
189      *        如果设置为false,则该实体不能为null,并且要同时配合使用@JoinColumn标记,将保存实体关系的字段设置为唯一的unique=true、
190      *        不为nullnullable=false,并且不能更新的updatable=false。
191      *
192      *        String mappedBy() default "";
193      *        mappedBy属性用于双向关联实体时,标注在不保存关系的实体中
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=关联表的名称+“_”+ 关联表主键的字段名
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     // 关联属性《多对一 多个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     // 关联属性《多对多》
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属性表示实体指定的目录名或是数据库名,这根据不同的数据库类型有所不同。
246      *
247      *        JoinColumn[] joinColumns() default {};
248      *            joinColumns属性表示,在保存关系中的表中,所保存关联关系的外键的字段。并配合@JoinColumn标记使用。
249      *
250      *        JoinColumn[] inverseJoinColumns() default {};
251      *             inverseJoinColumns属性与joinColumns属性类似,它保存的是保存关系的另一个外键字段。
252      *
253      *        UniqueConstraint[] uniqueConstraints default {};
254      *            uniqueConstraints属性表示该实体所关联的唯一约束条件,一个实体可以有多个唯一约束条件,默认没有约束条件。
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> 创 建 人: zzn
15  *<br> 创建日期: 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;// 创建日期
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> 创 建 人: zzn
 18  *<br> 创建日期: 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> 创 建 人: zzn
 22  *<br> 创建日期: 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; //创建时间
 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;// 联系电话
 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;// 培训联系人
 55     
 56     /**
 57      * @Target({METHOD, FIELD}) @Retention(RUNTIME)
 58      *    public @interface OneToMany {
 59      *    Class targetEntity() default void.class;
 60      *        targetEntity属性表示默认关联的实体类型。因为一对多的实体集合时保存在集合类中,所以必须指明集合类中保存的具体类型。
 61      *        1/指定集合泛型的具体类型;2/指定targetEntity属性类型
 62      *        1.本例;2.@OneToMany(targetEntity=User.class,cascade = CascadeType.ALL)
 63      *
 64      *    CascadeType[] cascade() default {};
 65      *        cascade属性表示与此实体一对多关联的实体的联级样式类型。联级样式是当对实体进行操作时策略,默认情况下,不关联任何操作。
 66      *        本例:当删除Department时会同时删除所有的User
 67      *
 68      *    FetchType fetch() default LAZY;
 69      *      fetch属性默认为惰性加载LAZY的,本例:加载Department延迟加载User
 70      *  
 71      *    String mappedBy() default "";
 72      *        mappedBy属性只有在实体间双向关联时使用。
 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> 创 建 人: zzn
 23  *<br> 创建日期: 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     //项目状态
 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;//项目颁发的证书
 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> 创 建 人: zzn
 14  *<br> 创建日期: Jul 14, 2010 10:15:39 AM
 15  */
 16 
 17 /**
 18  *####################################################################################################################################
 19  * 1.继承关系的实体保存在一个表(Single Table per Class Hierarchy Strategy)
 20  * 建议:这种方法虽然只有一个表,但是会出现很多的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注释,标识改继承层次上所区别每个实体的类型字段。
 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)来关联。类似父类与子类之间是一对一的关系映射。
 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值的数据,但每个实体是通过关联来获得的。
 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> 创 建 人: zzn
10  *<br> 创建日期: 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.每个子类实体保存在一个表,主键关联
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

posted on 2010-07-14 22:31 生命的绽放 阅读(2592) 评论(0)  编辑  收藏 所属分类: Hibernate


只有注册用户登录后才能发表评论。


网站导航:
 
<2010年7月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(5)

随笔分类(94)

随笔档案(93)

文章分类(5)

文章档案(5)

相册

JAVA之桥

SQL之音

兄弟之窗

常用工具下载

积分与排名

最新评论

阅读排行榜