BaseEntity实体的继承:@MappedSuperclass

在创建实体时,经常有重复的id和时间的属性要创建,所以想弄一个父类,然后所有实体继承,但是碰到了问题,就用到了@MappedSuperclass,它的的用法
用在实体的继承过程中的父类上;
如:
IdEntity.java
Java代码   收藏代码
  1. package com.zpf.test.Entity;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. import javax.persistence.Column;  
  7. import javax.persistence.GeneratedValue;  
  8. import javax.persistence.GenerationType;  
  9. import javax.persistence.Id;  
  10. import javax.persistence.MappedSuperclass;  
  11.   
  12. /** 
  13.  * 基础实体类 
  14.  *  
  15.  * @author zpf 
  16.  *  
  17.  */  
  18. @MappedSuperclass  
  19. public class IdEntity implements Serializable {  
  20.   
  21.     /** 
  22.      *  
  23.      */  
  24.     private static final long serialVersionUID = 1L;  
  25.   
  26.     @Id  
  27.     @GeneratedValue(strategy = GenerationType.AUTO)  
  28.     private long id;  
  29.   
  30.     @Column(updatable = false)  
  31.     private Date createDate;  
  32.     private Date modifyDate;  
  33.   
  34.     public long getId() {  
  35.         return id;  
  36.     }  
  37.   
  38.     public void setId(long id) {  
  39.         this.id = id;  
  40.     }  
  41.   
  42.     public Date getCreateDate() {  
  43.         return createDate;  
  44.     }  
  45.   
  46.     public void setCreateDate(Date createDate) {  
  47.         this.createDate = createDate;  
  48.     }  
  49.   
  50.     public Date getModifyDate() {  
  51.         return modifyDate;  
  52.     }  
  53.   
  54.     public void setModifyDate(Date modifyDate) {  
  55.         this.modifyDate = modifyDate;  
  56.     }  
  57.   
  58. }  


Java代码   收藏代码
  1. package com.zpf.test.Entity;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. import javax.persistence.Entity;  
  6. import javax.persistence.Table;  
  7.   
  8. /** 
  9.  * 用户实体 
  10.  * @author zpf 
  11.  * 
  12.  */  
  13. @Entity  
  14. @Table(name="tb_user")  
  15. public class User extends IdEntity implements Serializable{  
  16.   
  17.     private static final long serialVersionUID = 1L;  
  18.   
  19.     private String name;  
  20.       
  21.     private String sex;  
  22.       
  23.     private int age;  
  24.   
  25.     public String getName() {  
  26.         return name;  
  27.     }  
  28.   
  29.     public void setName(String name) {  
  30.         this.name = name;  
  31.     }  
  32.   
  33.     public String getSex() {  
  34.         return sex;  
  35.     }  
  36.   
  37.     public void setSex(String sex) {  
  38.         this.sex = sex;  
  39.     }  
  40.   
  41.     public int getAge() {  
  42.         return age;  
  43.     }  
  44.   
  45.     public void setAge(int age) {  
  46.         this.age = age;  
  47.     }  
  48.       
  49.       
  50. }  


这样就会生成tb_user表而且包含父类属性,否则只会生成一张identity表,字段有:id,crearedate,modifydate,name,sex,age 原文地址:https://www.iteye.com/blog/peng13123-2062980

猜你喜欢

转载自www.cnblogs.com/jpfss/p/12083639.html