0

When using JPA in Spring, I want to use a different table for each phase. In the example below, I would like to use "this-is-dev", "this-is-prod" table for each phase. All columns in the table and JPA repository methods are same. Is there a good way?

@Entity
@Table("this-is-dev") // for dev
// @Table("this-is-prod") //for prod
public class TestEntity {

    @Id
    private Long id;

    // ... same columns
}
@Repository
public interface TestRepository extends JpaRepository<TestEntity, Long> {
    // ... same methods
}
@Repository
@RequiredArgsConstructor
public class TestService {
    private final TestRepository repository;
}
rain
  • 1
  • 1
  • I don't think that will work with annotations, since `@Table` annotations are not from Spring (they are from jpa) and thus you wont be able to conditionalize them. Also kind of duplicate of https://stackoverflow.com/questions/3879607/change-table-name-of-an-entity-on-runtime – J Asgarov May 30 '23 at 11:42
  • I would rather use different database instances for the development phases. This is how it is usually done. – Mar-Z May 30 '23 at 11:48
  • Can you show me example? – rain May 30 '23 at 11:50
  • It rather fundamental question, why you will have different DB schema for Prod and Non-prod env – Tsvetoslav Tsvetkov May 30 '23 at 11:52
  • Dev and prod have different data. I want to separate them – rain May 31 '23 at 01:47
  • Of course data will be different. Having separate database schema or instance is the best solution. – Mar-Z May 31 '23 at 07:35

0 Answers0