简体   繁体   English

Spring / Hibernate同一实体不同的数据库结构

[英]Spring/Hibernate Same Entity different database structure

I have an app used in 3 different countries. 我有3个不同国家/地区使用的应用程序。 It is almost all the same, with a few country dependent exceptions, for example an order would have salesTax in USA, but not in the UK. 几乎都是一样,但有一些因国家而异的例外,例如,一个订单在美国有营业税,而在英国则没有。 Each country has its own database, completely separate to each other, with slight differences in the table structure for these country specific properties. 每个国家/地区都有自己的数据库,彼此完全分开,这些国家/地区特定属性的表结构略有不同。 The table names are always identical, as are the columns that all countries share; 表名始终相同,所有国家/地区共享的列也相同。 it is only the extra columns that make them different. 只有多余的列才使它们与众不同。

Currently as a result I actually have 3 separate applications, with about 80% identical code. 目前,实际上,我实际上有3个独立的应用程序,其中大约80%的代码相同。 Every time I develop something, I need to copy it into each individual application which is a bit laborious. 每次开发某些东西时,都需要将其复制到每个单独的应用程序中,这有些麻烦。

My plan is to try and have a global application, and then extend this with the 3 much smaller country specific ones. 我的计划是尝试在全球范围内推广应用,然后将其扩展到3个小得多的特定于国家/地区的应用。

For my services and daos this can be handled by my interfaces being global, and any implementations that vary between the apps being local. 对于我的服务和指导,这可以通过我的全局接口以及在本地应用之间有所不同的任何实现来处理。 The local apps will pull in the global as a dependency. 本地应用将依赖项拉入全局。

However, my problem is with the business objects. 但是,我的问题是业务对象。 With the order as an example again, many of my services and daos use the Order object, and they would be global since the code is identical across countries. 再次以订单为例,我的许多服务和指令都使用Order对象,并且它们是全局的,因为代码在各个国家/地区都是相同的。 So I need to have some sort of GLOBAL Order object. 因此,我需要某种GLOBAL Order对象。 But I also want to make sure that depending on the country, the extra properties are available and persisted if I go and save them again. 但是我还要确保,根据国家/地区的不同,如果我再次保存它们,可以使用并保留额外的属性。

As an example, I have a service that just checks the tracking of an order and marks it as Delivered. 例如,我有一项服务,该服务仅检查订单的跟踪并将其标记为“已交付”。 This service is global, so would have access to the GLOBAL Order object. 该服务是全局的,因此可以访问GLOBAL Order对象。 it would call orderDao.getOrder(id) thanks to the orderDao interface being global. 由于orderDao接口是全局的,因此它将调用orderDao.getOrder(id)。 The orderDaoImpl would be local, so the getOrder(id) method would actually return a localized Order instance, complete with extra fields. orderDaoImpl将是本地的,因此getOrder(id)方法实际上将返回本地化的Order实例,并带有额外的字段。 When this is passed up through the layers it reaches the tracking service again, which is expecting a GLOBAL Order, meaning the localized fields are not available (this is OK because this trackingService doesn't care about them, if it did, the implementation would be localized). 当它通过各层传递时,它将再次到达跟踪服务,这将期望使用GLOBAL Order,这意味着本地化字段不可用(这是可以的,因为此trackingService并不关心它们,如果这样做了,则实现将本地化)。

When I update the status of this GLOBAL Order I need to make sure that when it gets saved the localized properties are not lost. 更新此GLOBAL Order的状态时,我需要确保保存时不会丢失本地化的属性。

So my big question is how can I make this work? 因此,我的主要问题是如何进行这项工作?

Option 1: Is there any way for me to define some "selective transiency"? 选项1:我是否可以定义一些“选择性瞬态”? So I have a single Java Object with all possible properties. 所以我有一个具有所有可能属性的Java对象。 When it is used in each application, if any of those properties don't exist in the database table just ignore it and carry on. 在每个应用程序中使用它时,如果数据库表中不存在任何这些属性,则忽略它并继续。

OR 要么

Option 2: Is there a way for me to use some abstract/interfaced class that could be used at both global and local level, with local apps automatically casting the object as the implemented subclass? 选项2:我是否可以使用一些可以在全局和本地级别使用的抽象/接口类,而本地应用程序会自动将该对象转换为已实现的子类?

It is worth pointing out that the GLOBAL app as such would never actually be run by itself, it would only ever be used within the local apps. 值得指出的是,这样的GLOBAL应用程序实际上永远不会单独运行,只能在本地应用程序中使用。 But obviously cant have references to the local classes. 但是显然不能引用本地类。

Cheers for any assistance. 为任何帮助加油。

Might not be the most elegant solution, but you could make a global order class that isn't mapped as an @Entity and include it in your regional orders with @Embedded 可能不是最优雅的解决方案,但是您可以创建一个未映射为@Entity的全局订单类,并通过@Embedded将其包含在您的区域订单中

class GlobalOrder {
    @Column(name = "total")
    private Integer orderTotal;
    ...
}

@Entity
@Table("emeaorder")
class EMEAOrder {
    @Embedded
    private GlobalOrder globalOrder;
    @Column(name = "tax")
    private Integer euSalesTax;
    ...
}

OK. 好。 I think I have come up with the least elegant, but workable solution to this. 我想我已经提出了最不优雅但可行的解决方案。

The problem is allowing my global apps to know about the local Business Objects such as Orders, but not having them mapped to columns that may not exist in each country. 问题是我的全球应用程序可以了解本地业务对象(例如订单),但不能将它们映射到每个国家可能不存在的列。

So, I have included them in my Global project, and included every possible attribute, and getters/setters. 因此,我将它们包含在我的Global项目中,并包含了每个可能的属性以及getters / setters。

I then ALSO include them in my local projects with only the fields needed in that particular country. 然后,我也将它们包括在我的本地项目中,仅包含该特定国家/地区所需的字段。

Since the global apps are never actually used by themselves, whenever I build the library using maven, I exclude any of the business objects that are in my local projects. 由于全局应用程序本身从来没有真正使用过,因此每当我使用maven构建库时,我都会排除本地项目中的任何业务对象。 So when the local then includes it as a dependency there are no conflicts, and only the local version of these objects are included, meaning Hibernate doesn't complain about missing columns. 因此,当本地然后将其作为依赖项包含时,就没有冲突,并且仅包含这些对象的本地版本,这意味着Hibernate不会抱怨缺少列。

Not clean, but it works! 不干净,但是行得通!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 3 + Hibernate 4将数据库中的不同表加载到同一实体类中 - Spring 3 + Hibernate 4 Load different Tables from database into same entity class 如何在 Java/Spring/Hibernate 中从数据库中删除实体并将克隆存储在不同的数据库表中 - How to Delete Entity from Database and Store a Clone in a Different Database Table in Java/Spring/Hibernate 实体副本已分配给不同的实体(Spring/Hibernate) - Entity copy was already assigned to a different entity (Spring/Hibernate) 基于不同数据库的Spring,Hibernate类型映射 - Spring, hibernate type mapping based on different database 使用spring-hibernate的2个不同的数据库连接 - Using 2 different database connections with spring-hibernate 同一实体的两个Spring存储库-不同的数据源 - Two Spring Repositories for same entity - Different Datasources Spring jpa hibernate 如何将关系映射到同一实体的多个对象 - Spring jpa hibernate how to map a relationship to multiple objects of the same entity 冬眠,春天。 正在合并同一实体的多个表示 - Hibernate, Spring. Multiple representations of the same entity are being merged 如何从数据库加载数据并实例化实体(Hibernate和Spring) - How to load data from Database and instance the entity (Hibernate & Spring) 如何使用Spring + Hibernate将实体持久化到两个不同的表 - How to persist entity to two different tables using Spring+Hibernate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM