@MappedSuperclass的用法

实体类baseEntity.java
import java.util.Date;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
//@MappedSuperclass 用在父类上面。当这个类肯定是父类时,加此标注。如果改成@Entity,则继承后,多个类继承,只会生成一个表,而不是多个继承,生成多个表
@MappedSuperclass
public abstract class BaseEntity {
  private Integer id;// 数据库主键
 private Date creationTime;//创建时间
 private Date modificationTime;//修改时间
 
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public Date getCreationTime() {
  return creationTime;
 }
 public void setCreationTime(Date creationTime) {
  this.creationTime = creationTime;
 }
 public Date getModificationTime() {
  return modificationTime;
 }
 public void setModificationTime(Date modificationTime) {
  this.modificationTime = modificationTime;
 }
 
}
实体类Test_No1.java
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import com.rock.cft.hibernate.BaseEntity;
@Entity
@Table(name="test_no2")
public class Test_No1 extends BaseEntity implements Serializable {
 

 private String name;
 private int age;
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
  
}
实体类Test_No2.java
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import com.rock.cft.hibernate.BaseEntity;
@Entity
@Table(name="test_no2")
public class Test_NO2 extends BaseEntity implements Serializable {
 
 
 private Date testBri;
 
 private String testAdr;
 
 public Date getTestBri() {
  return testBri;
 }
 public void setTestBri(Date testBri) {
  this.testBri = testBri;
 }
 public String getTestAdr() {
  return testAdr;
 }
 public void setTestAdr(String testAdr) {
  this.testAdr = testAdr;
 }
 
}

这样在生成表的时候只生成了:test_no1、test_no2两张表,而且两张表中都含有id、creationTime、modificationTime三个属性

但是如果把@MappedSuperclass换成@Entity那么就会另外在生成一张baseEntity的表


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值