Handwritten Hibernate ORM framework-01-annotation definition, constant definition

Keywords: Java Hibernate simulator Programming

Catalog

Content of this section

Carry out annotation definition and constant definition of Hibernate

Annotation definition

Simulate Hibernate directly to define several of the most common annotations.

@Entity

package com.ryo.hibernate.simulator.hibernate.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Entity - Comment
 * Created by houbinbin on 16/6/5.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface Entity {
    /**
     * Table name
     * @return Table name
     */
    String value() default "";
}

@Column

package com.ryo.hibernate.simulator.hibernate.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Column annotation
 * Created by houbinbin on 16/6/5.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface Column {

    /**
     * Column names
     * @return Column names
     */
    String value() default "";

    /**
     * Can it be empty
     * @return {@code true} Sure
     */
    boolean nullable() default true;

    /**
     * Length of field
     * @return Length of field
     */
    int length() default 255;

}

@Id

package com.ryo.hibernate.simulator.hibernate.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Primary key identifier
 * Created by houbinbin on 16/6/5.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
@Documented
public @interface Id {
}

@GenerateValue

package com.ryo.hibernate.simulator.hibernate.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Self growing values - Comments
 * Created by houbinbin on 16/6/5.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
public @interface GenerateValue {
}

Constant definition

It is convenient for later programming development, global definition, and unified modification and maintenance.

HibernateConstant.java

For entity column generation

package com.ryo.hibernate.simulator.hibernate.constants;

/**
 *
 * hibernate constant
 * @author houbinbin
 * @date 16/6/5
 */
public class HibernateConstant {

    /**
     * Default string size {@ value}
     */
    public static final int DEFAULT_STRING_SIZE = 255;

}

TypeMap.java

For bean and database column field type mapping

package com.ryo.hibernate.simulator.hibernate.constants;

import java.util.HashMap;
import java.util.Map;

/**
 * Storage type map
 * Created by houbinbin on 16/6/5.
 * @author houbinbin
 */
public class TypeMap {

    private static final Map<String, String> TYPE_MAP = new HashMap<>();

    static {
        TYPE_MAP.put("java.lang.String", "VARCHAR");
        TYPE_MAP.put("char", "CHAR");
        TYPE_MAP.put("java.lang.Character", "CHAR");
        TYPE_MAP.put("boolean", "BIT");
        TYPE_MAP.put("java.lang.Boolean", "BIT");
        TYPE_MAP.put("byte", "TINYINT");
        TYPE_MAP.put("short", "SMALLINT");
        TYPE_MAP.put("java.lang.Byte", "SMALLINT");
        TYPE_MAP.put("int", "INTEGER");
        TYPE_MAP.put("java.lang.Integer", "INTEGER");
        TYPE_MAP.put("long", "BIGINT");
        TYPE_MAP.put("java.lang.Long", "BIGINT");
        TYPE_MAP.put("float", "FLOAT");
        TYPE_MAP.put("java.lang.Float", "FLOAT");
        TYPE_MAP.put("double", "DOUBLE");
        TYPE_MAP.put("java.lang.Double", "DOUBLE");
        TYPE_MAP.put("java.util.Date", "DATETIME");
    }

    /**
     * Get map list
     * @return map
     */
    public static Map<String, String> getTypeMap() {
        return TYPE_MAP;
    }
}

Directory navigation

Directory navigation

Posted by azhan on Thu, 26 Mar 2020 08:11:37 -0700