【@MappedSuperclass的用法】但使书种多,会有岁稔时。这篇文章主要讲述@MappedSuperclass的用法相关的知识,希望能为你提供帮助。
1、定义实体类的父类
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 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; } }
2、实体类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") 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; }}
3、实体类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") 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的表
推荐阅读
- 用React Native编写跨平台APP
- Android studio的常用快捷键
- Azure Stack技术深入浅出系列5(在Azure Stack上使用Web App PaaS服务及其背后原理窥探(开发案例))
- Dapper入门教程——Dapper Query查询
- android源码编译时拷贝替换指定文件
- app测试更多机型系统解决方法
- windows环境,下载android源码
- 忘记了你的Windows 11密码(这是重置Windows 11密码的方法)
- 什么是 Rectify11(重新设计的 Windows 11)以及如何安装它