JPA Springboot 新东西

本文介绍了在Java项目中如何使用Hibernate和相关依赖进行数据库操作,包括依赖管理(如Hypersistence和Hibernate-validator),UUID作为主键的使用,JSON类型的数据存储,以及如何处理嵌套主键和创建索引以优化性能。
摘要由CSDN通过智能技术生成

1.两个依赖

 <dependency>
            <groupId>io.hypersistence</groupId>
            <artifactId>hypersistence-utils-hibernate-63</artifactId>
            <version>3.7.3</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>8.0.1.Final</version>
        </dependency>

2.UUID主键

   @Id
    @GeneratedValue(generator = "my_uuid")
    @GenericGenerator(name = "my_uuid", type = UuidGenerator.class)
    @org.hibernate.annotations.UuidGenerator
    @Column(name = "id", columnDefinition = "varchar(55) comment '主键ID'")
    @Schema(description = "主键ID")
    private String id;

3.转JSON存数据库

  @Column(name = "dimensions", columnDefinition = "json  NOT NULL comment '维度集合json'")
    @Type(JsonType.class)
    @Schema(description = "维度集合json")
    private List<String> dimensions;

4.双主键

第一种




import io.hypersistence.utils.hibernate.type.json.JsonType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.UpdateTimestamp;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 统计图表参数 表
 */
@Data
@Table(name = "statistics_property")
@Entity
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class StatisticsProperty {

    @EmbeddedId
    @Schema(description = "主键ID")
    private PK id;

    @Column(name = "cn_name", columnDefinition = "varchar(255) NOT NULL comment '中文名称'")
    @Schema(description = "中文名称")
    private String cnName;

//List<String>
    @Column(name = "dimensions", columnDefinition = "json  NOT NULL comment '维度集合json'")
    @Type(JsonType.class)
    @Schema(description = "维度集合json")
    private List<String> dimensions;


// [{"metric":"","method":"sum"},{}]
    @Column(name = "statistics", columnDefinition = "json  NOT NULL comment '统计值集合json'")
    @Type(JsonType.class)
    @Schema(description = "统计值集合json")
    private List<Statistics> statistics;


    @Column(name = "chart_style", columnDefinition = "varchar(255)  NOT NULL comment '图表样式 选择一个 样式: 饼图 柱状图'")
    @Schema(description = "图表样式 选择一个 样式: 饼图 柱状图")
    private String chartStyle;

    @Column(name = "create_user_id", columnDefinition = "varchar(255)  NOT NULL comment '创建用户ID'")
    @Schema(description = "创建用户ID")
    private String createUserId;

    @Column(name = "update_user_id", columnDefinition = "varchar(255)  NOT NULL comment '修改用户ID'")
    @Schema(description = "修改用户ID")
    private String updateUserId;

    @Column(name = "create_time", columnDefinition = "DateTime  NOT NULL comment '创建时间'")
    @Schema(description = "创建时间")
    @CreationTimestamp
    private Date createTime;

    @Column(name = "update_time", columnDefinition = "DateTime  NOT NULL comment '修改时间'")
    @Schema(description = "修改时间")
    @UpdateTimestamp
    private Date updateTime;



    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Statistics{

        @Schema(description = "度量")
        private String metric;//度量
        @Schema(description = "算法")
        private String method;//算法
        @Schema(description = "度量分类")
        private String category;//度量分类

    }


    @Embeddable
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class PK implements Serializable {
        @Column(name = "name",columnDefinition = "varchar(255) comment '统计图标参数主键名称' ")
        @Schema(description = "统计图标参数主键名称")
        private String name;
        @Column(name = "data_set_name",columnDefinition = "varchar(255) comment '数据集名称' ")
        @Schema(description = "数据集名称")
        private String dataSetName;
    }
}

第二种



import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.io.Serializable;
import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Entity
@Table(name = "dimension_option",schema = "维度选项")
@IdClass(DimensionOption.PK.class)
public class DimensionOption {

    @Id
    @Column(name = "dimension_name", columnDefinition = "varchar(55) comment '维度名称'")
    @Schema(description = "维度名称")
    private String dimensionName;

    @Column(name = "option_value", columnDefinition = "varchar(255) NOT NULL comment '维度值'")
    @Schema(description = "维度值")
    @Id
    private String optionValue;

    @Column(name = "option_text", columnDefinition = "varchar(255) NOT NULL comment '维度显示文本'")
    @Schema(description = "维度显示文本")
    private String optionText;

    @Column(name = "color", columnDefinition = "varchar(55) NOT NULL comment '选项颜色'")
    @Schema(description = "选项颜色")
    private String color;


    @Column(name = "order_weight", columnDefinition = "double NOT NULL comment '排序'")
    @Schema(description = "排序")
    private Double orderWeight;

    @Column(name = "create_user_id", columnDefinition = "varchar(255)  NOT NULL comment '创建用户ID'")
    @Schema(description = "创建用户ID")
    private String createUserId;

    @Column(name = "update_user_id", columnDefinition = "varchar(255)  NOT NULL comment '修改用户ID'")
    @Schema(description = "修改用户ID")
    private String updateUserId;

    @Column(name = "create_time", columnDefinition = "DateTime  NOT NULL comment '创建时间'")
    @Schema(description = "创建时间")
    @CreationTimestamp
    private Date createTime;

    @Column(name = "update_time", columnDefinition = "DateTime  NOT NULL comment '修改时间'")
    @Schema(description = "修改时间")
    @UpdateTimestamp
    private Date updateTime;


    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Accessors(chain = true)
    public static class PK implements Serializable {
        private String dimensionName;
        private String optionValue;
    }


}

5.加索引


import com.tigeriot.organization.api.organizationtree.constant.OrganizationEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;
import org.hibernate.id.uuid.UuidGenerator;

import java.util.Date;

@Data
@Entity
@Table(name = "organization_relation_heatStation", indexes = {
        @Index(name = "org_idx_company_id", columnList = "company_id"),
        @Index(name = "org_uni_heat_station_id", columnList = "heat_station_id", unique = true),
        @Index(name = "org_uni_parent_id_and_heat_station_id", columnList = "parent_id,heat_station_id", unique = true)
})
@Comment("组织与换热站关联中间表")
public class OrganizationRelationHeatStation {

    @Id
    @GeneratedValue(generator = "my_uuid")
    @GenericGenerator(name = "my_uuid", type = UuidGenerator.class)
    @org.hibernate.annotations.UuidGenerator
    @Column(name = "id", columnDefinition = "varchar(255) comment '主键ID'")
    @Schema(description = "主键ID")
    private String id;


    /**
     * 所属热力公司ID
     */
    @Column(name = "company_id", nullable = false)
    @Comment("所属热力公司ID")
    private String companyId;


    /**
     * 父节点ID
     */
    @Column(name = "parent_id", nullable = false)
    @Comment("父组织ID")
    private String parentId;

    /**
     * 换热站ID
     */
    @Column(name = "heat_station_id", nullable = false)
    @Comment("换热站ID")
    private String heatStationId;

    /**
     * 排序
     */
    @Column(name = "show_order", columnDefinition = "double NOT NULL")
    @Comment("排序")
    private Double showOrder;


    @Column(name = "create_user_id", columnDefinition = "varchar(255)  NOT NULL comment '创建用户ID'")
    @Schema(description = "创建用户ID")
    private String createUserId;

    @Column(name = "update_user_id", columnDefinition = "varchar(255)  NOT NULL comment '修改用户ID'")
    @Schema(description = "修改用户ID")
    private String updateUserId;

    @Column(name = "create_time", columnDefinition = "DateTime  NOT NULL comment '创建时间'")
    @Schema(description = "创建时间")
    @CreationTimestamp
    private Date createTime;

    @Column(name = "update_time", columnDefinition = "DateTime  NOT NULL comment '修改时间'")
    @Schema(description = "修改时间")
    @UpdateTimestamp
    private Date updateTime;


    public static OrganizationRelationHeatStation build(String companyId, String parentId, String heatStationId) {
        OrganizationRelationHeatStation organizationRelationHeatStation = new OrganizationRelationHeatStation();
        organizationRelationHeatStation.setHeatStationId(heatStationId);
        organizationRelationHeatStation.setParentId(parentId);
        organizationRelationHeatStation.setCompanyId(companyId);
        organizationRelationHeatStation.setShowOrder(0.0d);
        organizationRelationHeatStation.setCreateUserId(OrganizationEnum.userIdConst);
        organizationRelationHeatStation.setUpdateUserId(OrganizationEnum.userIdConst);
        return organizationRelationHeatStation;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值