Data entity (Entity)


Data entity annotations
  • @Entity: declare a class as a data entity object;

    value: entity name (database table name) , the current class name is used by default;

    @Entity("tb_demo")
    public class Demo {
        //...
    }
  • ##@Id: declare a class member as the primary key;

    No parameters, used with @Property annotation;

    @Entity("tb_demo")
    public class Demo {
    
        @Id
        @Property
        private String id;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    }
  • @Property: declare a class member as data Entity attributes;

    name: Implementation attribute name, the default is the current member name;

    autoincrement: Whether it is automatic growth, the default is false;

    sequenceName: Sequence Name, suitable for databases such as Oracle, used together with the autoincrement parameter;

    nullable: allowed to be empty, the default is true;

    unsigned: whether it is unsigned, the default is false;

    length: data length, the default is 0 for no limit;

    decimals: the number of decimal places, the default is 0 for no decimals;

    type: data type, the default is Type.FIELD. VARCHAR;

    @Entity("tb_user")
    public class User {
    
        @Id
        @Property
        private String id;
    
        @Property(name = "user_name",
                  nullable = false,
                  length = 32)
        private String username;
    
        @Property(name = "age",
                  unsigned = true,
                  type = Type.FIELD.INT)
        private Integer age;
    
        // 省略Get/Set方法...
    }
  • @PK: Declare a class as a composite primary key object of a certain data entity;

    No parameters;

    @PK
    public class UserExtPK {
    
        @Property
        private String uid;
    
        @Property(name = "wx_id")
        private String wxId;
    
        // 省略Get/Set方法...
    }
    
    @Entity("tb_user_ext")
    public class UserExt {
    
        @Id
        private UserExtPK id;
    
        @Property(name = "open_id",
                  nullable = false,
                  length = 32)
        private String openId;
    
        // 省略Get/Set方法...
    }
  • ##@Readonly: Declare a member as a read-only attribute when the data entity is updated It will be ignored;
  • has no parameters and is used with the @Property annotation;

    @Entity("tb_demo")
    public class Demo {
    
        @Id
        @Property
        private String id;
    
        @Property(name = "create_time")
        @Readonly
        private Date createTime;
    
        // 省略Get/Set方法...
    }
  • @Indexes: declares the index of a set of data entities;
  • @Index: declares the index of a data entity;
  • @Comment: comment Content;
  • @Default: Specify a default value for a member attribute or method parameter;
  • Looking at so many annotations, do you feel that Writing entities is very troublesome, don’t worry, the framework provides a method to automatically generate entities, look down:)

Note

: Some of the above annotations or annotation parameters are for future capabilities. Creating database table structures (and SQL script files) directly through entity objects can be ignored for the time being;