How to tell Hibernate to conditionally ignore columns in CRUD operations

Is it possible to somehow force Hibernate to conditionally ignore a missing column in the database table when performing CRUD operations?

I have a Java application using Hibernate as a persistence layer. I would like to tell Hibernate somehow: if the database version is <50, then ignore this column annotation (or install it with a transient).

This situation arises due to different versions of the database on different clients, but the same entity code for all sites. For example, I have a class where a column description2may not be present in some databases.

@Entity
@Table(name = "MY_TABLE")
public class MyTable implements java.io.Serializable {

    private Integer serialNo;    
    private String pickCode;
    private String description1;
    private String description2;

    @Id
    @Column(name = "Serial_No", nullable = false)
    @GenericGenerator(name = "generator", strategy = "increment")
    @GeneratedValue(generator = "generator")
    public Integer getSerialNo() {
        return this.serialNo;
    }

    @Column(name = "Pick_Code", length = 25)
    public String getPickCode() {
        return this.pickCode;
    }

    @Column(name = "Description1")
    public String getDescription1() {
        return this.description1;
    }

    @Column(name = "Description2")     // <- this column might miss in some databases
    //@TransientIf(...)  <- something like this would be nice, or any other solution
    public String getDescription2() {
        return this.description2;
    }
}

: . , ( 500 500) , (, ). , . , . , ( ), .

+4
1

, , , . , , .

xml xml . 500 , , .

, , , . . , null , . .

+3

Source: https://habr.com/ru/post/1523334/


All Articles