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

2.2k views Asked by At

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. 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.

My question is, how do I create hbm.xml for view? Because it has some different structure than the other hbm.xml files of entities. 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?

1

There are 1 answers

0
milind_db On BEST ANSWER

Got the answer:

I created the hibernate mapping i.e. hbm.xml file for view by using standard templates of hbm file and add it to hibernate configuration object. 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.

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. This is weird but it is the simplest and only way to acquire the same.