I have a table Orders. Entity Class
@Entity
@Table(name = "orders", schema = "myschema")
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now I want the schema should change dynamically during runtime. I have two different Schema(public and myschema) in the database(Postgres SQL), both have the same table, but based on the condition it should call the schema. Is there a way to do it?
This is my JPA class
public interface OrdersRepository extends JpaRepository<Orders, Integer> {
@Query(value = "SELECT * From secondpublic.orders", nativeQuery = true)
public List<Orders> findAllSecondOrders();
@Query(value = "SELECT * From public.orders", nativeQuery = true)
public List<Orders> findAllPublicOrders();
}
Controller class
@RestController
@RequestMapping("/orders")
public class OrdersController {
@Autowired
private OrdersRepository ordersRepo;
@Value("${name}")
private String name;
@GetMapping()
public List<Orders> getEmployees(@RequestParam String mode) {
return ordersRepo.findAll();
}
}
Based on some condition, schema should be variable/dynamic.
@Table(name = "orders", schema = "{myschema}")