英文:
Hibernate mapping between different database and domain model
问题
我已经创建了一个领域模型和数据库模型。领域模型用于一个 REST API。两者都具有类似的组件,如产品、类别、顾客、零售商等等... 但是,如果您仔细查看这两个模型,您会看到一些差异。例如,您可以看到在数据库模型中,零售商拥有多个产品。但在领域模型中,您可以看到每个产品只有一个零售商。之所以采用这种方式,是因为我需要能够返回带有不同零售商的多个产品的列表。
您还可以在领域模型中看到产品具有产品属性。在数据库中,这也与领域模型不同的方式进行。数据库模型有一个属性列(例如:高度、重量、分辨率等等)。在数据库中,产品具有一个产品属性,该属性具有值和属性。
领域模型:
数据库模型/设计:
我对 Hibernate 进行了一些研究,并制作了一个测试项目来了解这个映射是如何工作的。我已经成功地创建了一个简单的项目。但当我开始创建一个包含这些模型的项目时,我陷入了困境。我找不到使其正常工作的方法。我读到了关于“POJOs”的一些内容。因此,一种解决方案可能是为数据库创建POJO,然后将其映射到我的领域模型,但我对此不太确定。所以我的问题是,如何使用Hibernate映射创建类似领域模型的数据库模型?
附注:我知道数据库模型与领域模型并不完全一致。
英文:
I have created a domain and database model. The domain model is for a rest api. The both have similar components like product, category, customer, retailer, etc... But if you take a closer look at both models. You can see some differences. For example, you can see that in the database model the retailer has multiple products. But in the domain model you can see that each product has a single retailer. The reason for doing it this way is because i need to be able to return a list of multiple products with different retailers.
You can also see in the domain model that the product has productproperties. In the database this is also done in a different way than the domain model. The database model has a column of properties(for example: height, weight, resolution, etc..). In the database a product has a productproperty which has a value and and a property.
Domain model:
Database model/design:
I did some research on Hibernate and made a test project to see how this mapping works. I got a simple project working. But when i started on creating a project with these models i got stuck. I couldn't find a way to make it work.I did read something about "POJOs" So maybe a solution could be to create pojos for the database and then kind of map these to my domain models, but i'm not sure about that. So my question is how do create a database like the model with domain model with hibernate mapping?
PS: i know that the database model is not completely up to date with the domain model.
答案1
得分: 0
对于Hibernate映射,您应该使用数据库模型,因为这些模型直接映射到数据库字段。领域模型/POJO应该在Controller方法中使用,这也提供了一层安全性,因为如果决定修改数据库表,您的API不会受到影响。
英文:
For Hibernate mapping, you should go with the database models as these models directly map with the database fields. Domain models/POJOs should be used in Controller methods which also provide a layer of security as your apis won't be affected in case you decide to modify the database table.
答案2
得分: 0
这个问题没有简单的答案,因为你正在寻求类似这样的内容:
> 请让这个工作起来。
你可以选择从你的 Hibernate Java 类(自顶向下)生成 DDL,或者从你的 DB-Schema 生成 Hibernate Java 类(自底向上)。
自顶向下的方法直接由 Hibernate 支持 - 只需要在搜索引擎中搜索 "使用 Hibernate 生成 DDL"。
自底向上的方法在 IntelliJ IDEA 中得到支持。这篇博文解释了如何实现:https://jpdevelopment.blogspot.com/2015/11/create-entity-objects-from-db-schema.html
我建议你采用自顶向下的方法,因为这是与工具无关且得到更好支持的方式。
为不同目的开发 Hibernate 类和数据库模式会导致映射问题,这是你不想要的。
此外,你谈到了用于 REST API 的 domain
模型。我建议你将 Hibernate Java 类模型用作内部域模型 - 可能不是完美的,但足够好。REST API 类模型暴露给外部世界,对该模型的更改应谨慎进行,因为更改该模型可能会影响到一些消费者。
你应该如何继续?
你应该选择自顶向下的方法。创建反映你的实体关系图的 Java 类,尽量添加 Hibernate 注解,使用 drop-create
在启动时删除并重新创建模式(参考 https://thorben-janssen.com/standardized-schema-generation-data-loading-jpa-2-1/ ),确保应用程序能够启动,尽快成功地创建模式。
采用这种方法,你可以获得即时反馈以验证你的映射是否正确。
更进一步:使用针对真实数据库的单元测试,确保你的 Hibernate 模型按预期工作。
英文:
This is question dosen't have a simple answer, because you are asking for something like:
> please make this work.
You should either generate the DDL from your hibernate java classes (top down) or you generated the hibernate java classes form your DB-Schema (bottom up).
The top down approach is supported by hibertnate directly - just google for generate DDL with hibernate.
The bottom up approach is supported with IntelliJ IDEA. This blog posts explains how this can be done: https://jpdevelopment.blogspot.com/2015/11/create-entity-objects-from-db-schema.html
I would suggest that you follow the top down approach, because this is tool independet and better supported.
Developing hibernate classes and data base schema for different purposes lead to mapping issues you don't want to have.
Furthermore, you are talking about your domain
model that is used for a REST API.
I would suggest, that you use your hibernate java class model as an internal domain model - may be not perfect but good enough. The REST API class model is exposed to the outer world and changes to that model should be done with care, because you may break some of your consumers when you change that model.
How should you continue?
You should go for the top down approach.
Create java classes reflecting your Entity Relation Diagramm es much as possible, add hibernate annotations to them, use drop-create
to drop and recerate the schema at startup (read https://thorben-janssen.com/standardized-schema-generation-data-loading-jpa-2-1/) and make sure the application starts, create the schema successfully as fast as possible.
With that approach you get immediate feedback if you mapping works.
More advance: Use Unit Tests against a real database to make sure your hibernate model works as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论