英文:
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<Data4D, Long>{
}
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
- +50,您可以使用维度列表
List<Double> dims = new ArrayList<>();
- 在模式差异方面,您可以使用 Liquibase 进行数据迁移。
https://docs.liquibase.com/home.html
以下是一个将 Liquibase 与 Spring Boot 集成的良好教程
https://www.baeldung.com/liquibase-refactor-schema-of-java-app
英文:
- +50,you can use a list of dims
List<Double> dims = new ArrayList<>();
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论