如何在使用Spring Data JPA时处理更改了结构的数据库。

huangapple go评论95阅读模式
英文:

how to hundle a database if we changed its structure using Spring Data JPA

问题

我是Spring Boot的新手,昨天遇到了这个问题,找不到任何解决方案,所以你是我最后的机会,
我创建了一个叫做data4D.java的实体,以及它的JpaRepository data4dJpaRepository:

@Entity
@Data @AllArgsConstructor @NoArgsConstructor @ToString
public class Data4D {
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private Double dim1;
    private Double dim2;
    private Double dim3;
    private Double dim4;

    public Data4D(Double dim1, Double dim2, Double dim3, Double dim4) {
        this.dim1 = dim1;
        this.dim2 = dim2;
        this.dim3 = dim3;
        this.dim4 = dim4;
    }
}

Data4dRepository.java:

public interface Data4dRepository extends JpaRepository<Data4D, Long> {
}

但是如果我们有一个有着 +50 维度的表,将所有这 50 个维度添加到 POJO 类中是不健康的。而且,作为第二种情况,如果我们指向另一个具有不同结构和不同列名的数据库,是不是必须要添加实体,并且从头开始重复所有的过程,还是有一种通用的方式来处理所有的情况呢?

英文:

I'm new to Spring Boot, I ran into this issue yesterday and couldn't find any solution so you are my last chance,
I created this entity called data4D.java and its JpaRepository data4dJpaRepository :

@Entity
@Data @AllArgsConstructor @NoArgsConstructor @ToString
public class Data4D {
@Id 
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

private Double dim1;
private Double dim2;
private Double dim3;
private Double dim4;

public Data4D(Double dim1,Double dim2,Double dim3,Double dim4) {
	this.dim1 = dim1;
	this.dim2 = dim2;
	this.dim3 = dim3;
	this.dim4 = dim4;

 }
}

Data4dRepository.java :

public interface Data4dRepository extends JpaRepository&lt;Data4D, Long&gt;{

}

but what if we have a table with +50 dims it is not healthy to add all the 50 dims to the POJO class.
and as 2nd scenario if we point to another database which has a different structure with a different column names, is it necessary to add entity and repeat all the procedures from scratch or there is a generic way to handle all scenarios

答案1

得分: 1

  1. +50,您可以使用维度列表
List<Double> dims = new ArrayList<>();
  1. 在模式差异方面,您可以使用 Liquibase 进行数据迁移。
    https://docs.liquibase.com/home.html

以下是一个将 Liquibase 与 Spring Boot 集成的良好教程
https://www.baeldung.com/liquibase-refactor-schema-of-java-app

英文:
  1. +50,you can use a list of dims
List&lt;Double&gt; dims = new ArrayList&lt;&gt;();

  1. diff in schema, you can use liquibase for data migration
    https://docs.liquibase.com/home.html

this is a good tutorial to integrate liquibase with spring boot
https://www.baeldung.com/liquibase-refactor-schema-of-java-app

huangapple
  • 本文由 发表于 2020年7月29日 00:02:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63138274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定