如何告诉Hibernate有条件地忽略CRUD操作中的列

时间:2022-01-01 07:20:32

Is it possible to somehow tell Hibernate to conditionally ignore a missing column in a database table while doing the CRUD operations?

是否有可能以某种方式告诉Hibernate在执行CRUD操作时有条件地忽略数据库表中的缺失列?

I've got a Java application using Hibernate as persistence layer. I'd like to be able to somehow tell Hibernate: If database version < 50, then ignore this column annotation (or set it transient).

我有一个使用Hibernate作为持久层的Java应用程序。我希望能以某种方式告诉Hibernate:如果数据库版本<50,则忽略此列注释(或将其设置为瞬态)。

This situation arises due to different database versions at different clients, but same entity code for all sites. For example, I've got a class, where the column description2 might miss in some databases.

出现这种情况的原因是不同客户端的数据库版本不同,但所有站点的实体代码相同。例如,我有一个类,其中列description2可能会在某些数据库中遗漏。

@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;
    }
}

Background: I have a large application with a lot of customizations for different clients. Now it happens from time to time that one client (out of lets say 500) gets a new feature that requires a database structure update (e.g. a new field in a table). I release a new version for him, he runs a database schema update and can use the new feature. But all other clients won't do an incremental database update each time when any user gets a new feature. They just want to use the latest version, but are affected by the new feature (for that one client) they will never use.

背景:我有一个大型应用程序,为不同的客户端提供了大量自定义。现在不时发生一个客户端(超出500个)获得需要更新数据库结构的新功能(例如表中的新字段)。我为他发布了一个新版本,他运行数据库架构更新并可以使用新功能。但是,每当任何用户获得新功能时,所有其他客户端都不会执行增量数据库更新。他们只想使用最新版本,但受新功能(对于那个客户端)的影响,他们永远不会使用。

1 个解决方案

#1


3  

I think it is only possible if you separate the mapping definition from the entities so that you can replace it. Thus you can not use annotation based mapping.

我认为只有将映射定义与实体分开才可以替换它。因此,您无法使用基于注释的映射。

Instead I would suggest to use xml based mapping and create different xml mapping files for each client. Since you have about 500 clients you might want to create groups of clients who all share the same mapping file.

相反,我建议使用基于xml的映射并为每个客户端创建不同的xml映射文件。由于您有大约500个客户端,因此您可能希望创建所有共享相同映射文件的客户端组。

At least I think it will be very hard to maintain the different clients needs with one entity model and it will lead to a complex code structure. E.g. if you add properties to the enties that can be null for some clients than you will also add a lot more null checks to your code. One null check for each client specific property.

至少我认为用一个实体模型维护不同的客户需求是非常困难的,这将导致复杂的代码结构。例如。如果你为某些客户端添加的属性添加属性,那么你还需要为代码添加更多的空值检查。对每个客户特定属性进行一次空检查。

#1


3  

I think it is only possible if you separate the mapping definition from the entities so that you can replace it. Thus you can not use annotation based mapping.

我认为只有将映射定义与实体分开才可以替换它。因此,您无法使用基于注释的映射。

Instead I would suggest to use xml based mapping and create different xml mapping files for each client. Since you have about 500 clients you might want to create groups of clients who all share the same mapping file.

相反,我建议使用基于xml的映射并为每个客户端创建不同的xml映射文件。由于您有大约500个客户端,因此您可能希望创建所有共享相同映射文件的客户端组。

At least I think it will be very hard to maintain the different clients needs with one entity model and it will lead to a complex code structure. E.g. if you add properties to the enties that can be null for some clients than you will also add a lot more null checks to your code. One null check for each client specific property.

至少我认为用一个实体模型维护不同的客户需求是非常困难的,这将导致复杂的代码结构。例如。如果你为某些客户端添加的属性添加属性,那么你还需要为代码添加更多的空值检查。对每个客户特定属性进行一次空检查。