简体   繁体   English

如何为mysql视图创建hbm.xml文件以及如何在hibernate配置中添加它?

[英]How to create hbm.xml file for mysql view and how to add it in hibernate configuration?

I have created mysql view for two tables which are in different schema using JDBC, because hibernate is not providing any facility do the same. 我已经使用JDBC为处于不同模式的两个表创建了mysql视图 ,因为hibernate没有提供任何功能来做同样的事情。 I am using entity_mode as dynamic-map , so I have to create hibernate configuration by adding documents of hbm.xml files of all my entities from my schema. 我将entity_mode用作动态映射 ,因此必须通过从架构中添加所有实体的hbm.xml文件的文档来创建休眠配置。

My question is, how do I create hbm.xml for view? 我的问题是,如何创建hbm.xml进行查看? Because it has some different structure than the other hbm.xml files of entities. 因为它具有与实体的其他hbm.xml文件不同的结构。 How to add view mapping to the hibernate configuration, So I can do at least read operations using hibernate queries? 如何将视图映射添加到休眠配置,所以至少可以使用休眠查询执行读取操作?

Is this possible in hibernate? 这可能在休眠状态吗? If yes then how can I achieve this? 如果是,那我该如何实现?

Got the answer: 得到了答案:

I created the hibernate mapping ie hbm.xml file for view by using standard templates of hbm file and add it to hibernate configuration object. 我使用hbm文件的标准模板创建了用于查看的hibernate映射(即hbm.xml文件),并将其添加到hibernate配置对象中。 But for mapping, hibernate must need one id field in the hbm.xml file to work, so that I have to made one of the id field from both tables as the id field of the view and it worked. 但是对于映射,hibernate必须在hbm.xml文件中需要一个id字段才能工作,因此我必须将两个表中的id字段之一设为视图的id字段,然后它才能工作。

It is giving the multiple result because the id is repeated, So you need to have two constraints for the view query so it will give you unique result. 因为id是重复的,所以它给出多个结果,因此您需要对视图查询有两个约束,以便它给您唯一的结果。 This is weird but it is the simplest and only way to acquire the same. 这很奇怪,但是这是获得该信息的最简单也是唯一的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM