Hibernate - 如何将新类映射到两个现有表
我有两个实体:车辆和武器类。它们分别映射到Vehicle.hbm.xml和Weapon.hbm.xml。两者都有各自的 DAO。一切都工作正常。
这是我的想法。我想创建一个类 VehicleWeapon ,它具有车辆和武器实体的所有属性。我想声明一个 Hibernate 映射文件:VehicleWeapon.hbm.xml。我希望映射文件中的属性与武器和车辆实体的属性相匹配。
这样,当我添加、删除或保存 VehicleWeapon 实体时,相应的更改将反映在支持的 Vehicle 和 Weapon 表上。 (我可能可以进行 HQL 查询,但我想首先知道我所问的想法是否可行)。
这可能吗?如果是的话,你能给我举个例子吗? (当然,车辆和武器类是假设的类。我确实有一个实际场景,我需要实现这个想法)。
或者还有什么更好的选择?您能提供直接链接或具体示例吗?多谢。
I have two entities: Vehicle and Weapon classes. They are mapped respectively to Vehicle.hbm.xml and Weapon.hbm.xml. Both have their respective DAOs. All are working fine.
Here's my idea. I want to create a class VehicleWeapon that has all the properties of the Vehicle and Weapon entities. I want to declare a Hibernate mapping file: VehicleWeapon.hbm.xml. I want the properties in the mapping file to match the properties of the Weapon and Vehicle entities.
So that when I add, delete, or save a VehicleWeapon entity, the corresponding changes will reflect on the backing Vehicle and Weapon tables. (I could probably do an HQL query, but I want to know first if the idea I'm asking is doable).
Is this possible? If yes, can you show me an example? (Of course, the Vehicle and Weapon classes are hypothetical classes. I do have an actual scenario where I need this idea to be implemented).
Or what better options are there? Can you provide a direct link or concrete example? Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我找到了一个可行的解决方案。正如我之前提到的,我不允许修改车辆和武器实体以及相应的映射文件。甚至连一个注释都不允许。
我通过谷歌搜索本的建议找到了这个答案,这让我找到了这些文章:
http://www.mkyong.com/hibernate/hibernate -一对一关系示例/
和 http://www.vaannila .com/hibernate/hibernate-example/hibernate-mapping-one-to-one-1.html
mkyong 的建议很好,但我不喜欢像我一样保留和分配主键的想法无法编辑车辆和武器类的映射文件。请参阅评论“一对一关系的主要困难是确保为两者分配相同的主键”
vaannila 的建议在某种程度上很有趣。它建立一对一的关系,但不需要对其他映射文件进行任何编辑。
首先,我声明了我的 VehicleWeapon.hbm.xml,如下所示:
然后我声明了我的标准 VehicleWeapon POJO 类。以及相应的DAO实现。
我运行了几个 Junit/Spring 测试。我能够毫无问题地保存、删除、检索。所有操作都会级联到相应的武器和车辆表。
此方法的唯一缺点是 Hibernate 将创建第三个表vehicle_weapon,其中分别包含车辆和武器表的 id 作为参考。
好处是我没有编辑任何现有实体或映射文件。我创建了一个新对象,并从两个表组成了一个新对象。
我仍然希望能够直接映射到武器和车辆的属性,而不是直接映射到武器和车辆实体。但就目前而言,我认为这是可以接受的。
I found a working solution. As I mentioned earlier, I'm not allowed to modify the Vehicle and Weapon entities and the corresponding mapping files. Not even a single annotation is allowed.
I found this answer by Googling Ben's suggestion above which lead me to these articles:
http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example/
and http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-one-1.html
The suggestion from mkyong is good but I didn't like the idea of keeping and assigning primary keys both ways as I have no way of editing the mapping files from the Vehicle and Weapon classes. See the comment "the main difficulty with one-to-one relationship is ensuring both are assigned the same primary key"
The suggestion from vaannila is somehow intriguing. It does a one-to-one relationship but without requiring any edits on other mapping files.
First, I declared my VehicleWeapon.hbm.xml as follows:
Then I declared my standard VehicleWeapon POJO class. And the corresponding DAO implementation.
I run a couple of Junit/Spring tests. I was able to save, delete, retrieve without problems. All actions cascade to the corresponding Weapon and Vehicle tables.
The only downside with this method is Hibernate will create a third table vehicle_weapon that contains the id for the vehicle and weapon tables respectively as a reference.
The good thing is I didn't edit any existing entities or mapping files. I created a new one and compose a new object from two tables.
I still like to be able to map directly to the Weapon and Vehicle's properties instead of mapping directly to the Weapon and Vehicle entities. But for now, I think this is acceptable.
Hibernate 支持使用
元素(或使用注释时的@SecondaryTable
注释)跨多个表持久保存一个实体类。从文档中:像您一样使用组合是另一种选择,但它显然会引入另一个表(用于“聚合”实体)。
Hibernate provides support to persist one entity class across multiple tables using the
<join>
element (or the@SecondaryTable
annotation when using annotations). From the documentation:Using composition as you did is another option but it will obviously introduce another table (for the "aggregating" entity).
不确定这是否是答案,但 Hibernate 确实允许“复合”关系,即您将一个类嵌入到另一个类中,然后将其映射。
例如,在这里我们创建“可嵌入”类(车辆),然后将其“嵌入”到 VehicleWeapon 中:
嵌入此处:
这对您的场景有帮助吗?
Not sure whether this is the answer, but Hibernate does allow for “compound” relationships, whereby you embed one class within another, and that gets mapped.
For example, here we create the "embeddable" class (Vehicle) and then "embed" it in VehicleWeapon:
Embedded here:
Would that help in your scenario?
使用 joincolumn 和 inverseJoincolumn 注释来连接两个表(Vehicle 和 Weapon)并将它们放入事务表(VehicleWeapon)中。
使用任何映射(onetoone、onetomany 等)通过 ondeletecascade 和 fetch 映射车辆 amd waepon 表。
use joincolumn and inverseJoincolumn annotations for join two tables (Vehicle and Weapon) and put those in to transaction table (VehicleWeapon).
use any mapping(onetoone, onetomany, etc) to map vehicle amd waepon tables with ondeletecascade and fetch.