BaseEntity实体的继承:@MappedSuperclass
Posted 星朝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BaseEntity实体的继承:@MappedSuperclass相关的知识,希望对你有一定的参考价值。
在创建实体时,经常有重复的id和时间的属性要创建,所以想弄一个父类,然后所有实体继承,但是碰到了问题,就用到了@MappedSuperclass,它的的用法
用在实体的继承过程中的父类上;
如:
IdEntity.java
这样就会生成tb_user表而且包含父类属性,否则只会生成一张identity表,字段有:id,crearedate,modifydate,name,sex,age 原文地址:https://www.iteye.com/blog/peng13123-2062980
用在实体的继承过程中的父类上;
如:
IdEntity.java
Java代码
- package com.zpf.test.Entity;
- import java.io.Serializable;
- import java.util.Date;
- import javax.persistence.Column;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.MappedSuperclass;
- /**
- * 基础实体类
- *
- * @author zpf
- *
- */
- @MappedSuperclass
- public class IdEntity implements Serializable {
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private long id;
- @Column(updatable = false)
- private Date createDate;
- private Date modifyDate;
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public Date getCreateDate() {
- return createDate;
- }
- public void setCreateDate(Date createDate) {
- this.createDate = createDate;
- }
- public Date getModifyDate() {
- return modifyDate;
- }
- public void setModifyDate(Date modifyDate) {
- this.modifyDate = modifyDate;
- }
- }
package com.zpf.test.Entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.MappedSuperclass; /** * 基础实体类 * * @author zpf * */ @MappedSuperclass public class IdEntity implements Serializable { /** * */ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; @Column(updatable = false) private Date createDate; private Date modifyDate; public long getId() { return id; } public void setId(long id) { this.id = id; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Date getModifyDate() { return modifyDate; } public void setModifyDate(Date modifyDate) { this.modifyDate = modifyDate; } }
Java代码
- package com.zpf.test.Entity;
- import java.io.Serializable;
- import javax.persistence.Entity;
- import javax.persistence.Table;
- /**
- * 用户实体
- * @author zpf
- *
- */
- @Entity
- @Table(name="tb_user")
- public class User extends IdEntity implements Serializable{
- private static final long serialVersionUID = 1L;
- private String name;
- private String sex;
- private int age;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- }
package com.zpf.test.Entity; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Table; /** * 用户实体 * @author zpf * */ @Entity @Table(name="tb_user") public class User extends IdEntity implements Serializable{ private static final long serialVersionUID = 1L; private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
这样就会生成tb_user表而且包含父类属性,否则只会生成一张identity表,字段有:id,crearedate,modifydate,name,sex,age 原文地址:https://www.iteye.com/blog/peng13123-2062980
以上是关于BaseEntity实体的继承:@MappedSuperclass的主要内容,如果未能解决你的问题,请参考以下文章
当我们需要生成模板输出工件时,我们如何用 Roslyn 替换 T4