Search code examples
spring-bootjpa

How to use JPA annotation if the column is not present now but it will be added later in time


I'm using JPA annotations to bind table columns to entities properties. One of my entities have amount and currency properties. Currently, only the amount property is being mapped to database - in a near future, the currency property is going to be mapped too.

I want both properties in my entity, even if the currency column isn't present in the table yet. For now, it should be null.


Solution

  • Unfortunately, I can't add this as a comment because I don't have enough reputation.

    To handle this you could use a DTO (Data Transfer Object) to represent the full response including the amount and currency fields. You can add them to your DTO right now, and they will be null as long as you don't set them. Later, when you add the columns to the database you can add the corresponding fields to your entity and start setting them in your DTO.

    Seems like there is no out-of-the-box functionality to ignore non-existing columns.

    I hope it gives you some direction.