batmat wrote:
Just remember what you need as a foundation to your hibernate use: build a SessionFactory object. From this one, you then will be able to create the session objects that you need to execute queries and so on.
You're going to need to create the Configuration programmatically:
Code:
cfg = new Configuration();
cfg.addDirectory(yourFile));
...
cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.YourDialect");
Then, create a SessionFactory:
Code:
SessionFactory sessionFactory= cfg.buildSessionFactory();
And you're done :-)
Thanks for the response. I read the following in the documentation
An alternative (sometimes better) way is to let Hibernate load a mapping file using getResourceAsStream():
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class);
Then Hibernate will look for mapping files named /org/hibernate/autcion/Item.hbm.xml and /org/hibernate/autcion/Bid.hbm.xml in the classpath. This approach eliminates any hardcoded filenames.
A Configuration also specifies various optional properties:
Properties props = new Properties();
...
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class)
.setProperties(props);
So going with that, I can get ensure that the user puts hbm.xml mapping files in the classpath. The user can thus change the mappings if it decides to use another database. But what about the hibernate.cfg.xml. Your solution above is putting the dialect etc directly in the source code which the end user wont have access to. Remember, the problem that we are trying to solve is the following :-
Today, the end user uses mySQL database but tomorrow he may switch to Sybase. I dont want to recreate the war file then. The hibernate XML files have to be outside the war file so that the user can simply change the db information and the O/R mappings.
thanks
RK