V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wym7223645
V2EX  ›  Java

关于业务实体中,公共字段处理的疑问?

  •  1
     
  •   wym7223645 · 2019-10-23 18:10:14 +08:00 · 2541 次点击
    这是一个创建于 1636 天前的主题,其中的信息可能已经有所发展或是发生改变。

    公共属性类

    public class BaseEntity implements Serializable {
    	private static final long serialVersionUID = -6287610802797091825L;
        /** 可用状态 */
        private String usableStatus;
        /** 创建日期时间 */
        private Date createDate;
        /** 创建用户 */
        private String createUserId;
        /** 创建 IP */
        private String createIp;
        /** 修改日期时间 */
        private Date modifyDate;
        /** 修改用户 */
        private String modifyUserId;
        //  其余公共字段
    }
    

    方式 1:将公共类,作为 test 的子属性引入

    public class test implements Serializable {
    	// 编号
    	private String code; 
    	// 名字
    	private String name;
    	// 引入公共属性
    	BaseEntity baseEntity;
    }
    

    方式 2:将公共属性类作为父类,让 test 继承

    public class test extends BaseEntity implements Serializable {
    	// 编号
    	private String code; 
    	// 名字
    	private String name;
    }
    

    实际项目中所有的表会有一部分公共属性,例如创建时间、修改时间、可用状态等。 我们新的项目被要求按照如上方式 1的方式进行处理,说是因为现在主流都是这样做的

    我个人比较偏向方式 2,因为方式 1如果要更改usableStatus需要 test.getBaseEntity().setUsableStatus("1")才能改,并且使用 JPA 查询的时候需要更深的层次才行

    有如下几个问题请教各位大佬

    1.这两种方式在使用过程中的利弊分别是什么?

    2.大家都使用哪种方式,是否真的主流都是方式 1

    本人菜鸟,各位大佬请喷~~~

    2 条回复    2019-10-24 18:08:16 +08:00
    killua67
        1
    killua67  
       2019-10-24 08:50:51 +08:00
    2 表操作方便
    x66
        2
    x66  
       2019-10-24 18:08:16 +08:00
    我们用 2
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5006 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 09:52 · PVG 17:52 · LAX 02:52 · JFK 05:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.