'Is there a way to treat fields as @Transient in a configurable way?

We have a Database not owned by us, it deploys to production only after performance testing and CR requirements, etc, and it is a bottle neck. Lower environments don't have the same restrictions.

When making model changes, our code base is ready for production well before the DB, and can't be deployed until the DB is ready, otherwise new columns included in SELECT or INSERT queries will not exist and cause errors. This causes a holding pattern, general headaches, etc.

Fortunately, @Transient works perfectly to unblock prod, but it would be deployed to all environments. Ideally, we could make that behaviour configurable so for example only production would treat those fields as @Transient

@Getter
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "INCOME", schema = "dbo")
public class Income {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = INCOME_ID)
    private long incomeId;

    @Column(name = RELATED_ID, insertable = false, updatable = false)
    @Transient
    private Long relatedId; 

Any suggestions or possible approaches?



Solution 1:[1]

You can take advantage of using xml mapping. In your case it will look like:

<?xml version="1.0" encoding="UTF-8"?>

<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd"
                 version="2.1">

    <entity class="package.name.Income">
        <attributes>
           <transient name="relatedId"/>
        </attributes>
    </entity>

</entity-mappings>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrey B. Panfilov