wym7223645
V2EX  ›  Java

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

  •  1
     
  •   wym7223645 · Oct 23, 2019 · 3587 views
    This topic created in 2439 days ago, the information mentioned may be changed or developed.

    公共属性类

    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 replies    2019-10-24 18:08:16 +08:00
    killua67
        1
    killua67  
       Oct 24, 2019
    2 表操作方便
    x66
        2
    x66  
       Oct 24, 2019
    我们用 2
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2668 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 01:58 · PVG 09:58 · LAX 18:58 · JFK 21:58
    ♥ Do have faith in what you're doing.