没有注解的 SpringData 映射实体 [英] SpringData mapping entities without annotations

查看:21
本文介绍了没有注解的 SpringData 映射实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含实体的 jar,其中的代码我不能修改或做任何事情,只能使用它们,我必须使用那个 jar.

I have a jar with entities which code I cannot modify or do anything other than use them and I must use that jar.

我正在尝试使用这些实体将它们保存在 mongodb 中,并且我正在使用 Spring,所以我使用 SpringData 而不是 Hibernate(也是因为我阅读了一些让我选择 SpringData 而不是 Hibernate NoSQL impl 的东西).无论如何,我一直无法让它工作,因为我还没有找到任何方法来设置集合的属性,比如索引、唯一性、复合索引等.现在我可以存储它和所有东西,但由于无法正确配置我的实体,我失去了许多 mongo 功能.

I'm trying to use those entities to persist them in mongodb and I'm using Spring, so I went with SpringData instead of Hibernate (also because I read some things that led me to choose SpringData over Hibernate NoSQL impl). Anyway, I haven't been able to make it work because I haven't find any way to set the properties of a collection on them, like the indexing, uniqueness, compound indexes, etc. Right now I can store it and everything but I'm losing a lot of mongo features by not been able to properly configure my entities.

我一直在寻找不需要修改现有代码的 xml 配置或某种其他形式的映射.

I've looked for an xml configuration or some other form of mapping that doesn't require modification of the existing code.

到目前为止,我能想到的唯一解决方案"是

So far the only "solutions" I could came up with was

  1. 通过插入一些虚拟数据来创建集合,删除它,然后使用 WriteConverter 和 ReadConverter 进行映射.

  1. Create the collections by inserting some dummy data, delete it and then use WriteConverter's and ReadConverter's for the mapping.

在我的代码中复制完全相同的实体,并为它们添加相应的注释,然后将它们转换为 jar 中的注释,但这将是我最后的手段.

Duplicating the exact same entities on my code and add them the respective annotations and then convert them to the ones in the jar, but this would be my last resort.

任何想法或替代解决方案?

Any ideas or alternative solutions?

谢谢.

推荐答案

您可以在 orm.xml 中使用带有 xml 映射的 Hibernate,它包含将类映射到关系数据库表所需的所有配置详细信息.这些详细信息包括实体类的主键以及要应用于主键的各种约束/规则.

You can use Hibernate with xml mappings in a orm.xml, it contains all the configuration details required for mapping a class to a relational database table. These details include the primary key of the entity class and the various constraints/ rules to be applied for the primary key.

有关更多信息,您可以查看此链接在此处输入链接说明

For more information you can check this link enter link description here

在此处输入链接描述是如何使用 Spring 数据执行此操作的示例

This enter link description here is an example of how to do it with Spring data

这篇关于没有注解的 SpringData 映射实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆