@MappedSuperclass的用法

实体类baseEntity.java

 
 

package com.rock.cft.hibernate;

import java.util.Date;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

//@MappedSuperclass 用在父类上面。当这个类肯定是父类时,加此标注。如果改成@Entity,则继承后,多个类继承,只会生成一个表,而不是多个继承,生成多个表
@MappedSuperclass
publicabstractclassBaseEntity{
privateInteger id;// 数据库主键
privateDate creationTime;//创建时间
privateDate modificationTime;//修改时间

@Id
@GeneratedValue(strategy =GenerationType.AUTO)
publicInteger getId(){
return id;
}
publicvoid setId(Integer id){
this.id = id;
}
publicDate getCreationTime(){
return creationTime;
}
publicvoid setCreationTime(Date creationTime){
this.creationTime = creationTime;
}
publicDate getModificationTime(){
return modificationTime;
}
publicvoid setModificationTime(Date modificationTime){
this.modificationTime = modificationTime;
}


}


实体类Test_No1.java
 
 

package com.rock.cft.test.model;

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")
publicclassTest_No1extendsBaseEntityimplementsSerializable{


privateString name;
privateint age;

publicString getName(){
return name;
}
publicvoid setName(String name){
this.name = name;
}
publicint getAge(){
return age;
}
publicvoid setAge(int age){
this.age = age;
}

}


实体类Test_NO2.java
 
 

package com.rock.cft.test.model;

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")
publicclassTest_NO2extendsBaseEntityimplementsSerializable{


privateDate testBri;

privateString testAdr;

publicDate getTestBri(){
return testBri;
}
publicvoid setTestBri(Date testBri){
this.testBri = testBri;
}
publicString getTestAdr(){
return testAdr;
}
publicvoid setTestAdr(String testAdr){
this.testAdr = testAdr;
}




}

 
 
这样在生成表的时候只生成了:test_no1、test_no2两张表,而且两张表中都含有id、creationTime、modificationTime三个属性
 
 
但是如果把@MappedSuperclass换成@Entity那么就会另外在生成一张baseEntity的表


你可能感兴趣的:(@MappedSuperclass的用法)