0

I want to create Hibernate SessionFactory/Session without generating Java pojos and hbm.xml files, Is there any way to do the same?

Is there any way available to create pojo or hbm files on the fly i.e dynamically and so that further we can create the SessionFactory to get Session and do queries?

I do not want to generate the pojos or any hbm files on the system, If anyone have any idea please share with me.

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
milind_db
  • 1,274
  • 5
  • 34
  • 56
  • 1
    got the answer, http://stackoverflow.com/questions/17298763/how-to-add-mapping-resource-to-hibernate-configuration-programatically-for-sessi – milind_db Jun 26 '13 at 09:42
  • Possible duplicate of [How to add mapping resource to hibernate configuration programatically for session factory creation?](https://stackoverflow.com/questions/17298763/how-to-add-mapping-resource-to-hibernate-configuration-programatically-for-sessi) – milind_db Oct 11 '17 at 09:56

2 Answers2

0

I am afraid it is not possible. You may have to rely on JDBC in order to do that.

In the .NET equivalent (NHibernate), you can still do the mappings in code (i.e. without having the xml files) - but the POCOs need to be present. This is called 'fluent' configuration, and I doubt if any Java frameworks have that facility.

aquaraga
  • 4,138
  • 23
  • 29
0

Well depending on what you need to do the following approach could be a work around. You could create a hbm.xml file during runtime, using whatever caters to your need and save that file to a "know" place. Then you need to add that file to the Configuration that you use to set up your SessionFactory.

Configuration hibConfiguration = new Configuration()
.addResource(<filepath to custom.hbm.xml>)

After that you would just delete the created file and go on with what you're doing.

I did not try this yet but I guess it should be no different from having a predefined hbm.xml and doing all instantiation during runtime.

Unfortunately I don't know of any other way to set up the mapping without the hbm.xml hitting the disc at least for a short time.

Carsten
  • 1,511
  • 2
  • 13
  • 24