NAVIGATION
Home
Gallery
Java
Linux
Web
Scripts And Utilities
Mobile And Sms
Misc
Contact
pixelWIKI
Nabaz Tag




Jboss Hibernate

Jboss and Hibernate

Using the in-built hibernate capabilities of jboss:
  1. Config file
  2. Sessions Management
  3. packaging

Config file
Using hibernate in JBoss is fairly straighforward:
instead of an hibernate.cfg.xml file, create a META-INF/hibernate-service.xml file. This should be based on this template:


1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
<server>
	 <mbean code="org.jboss.hibernate.jmx.Hibernate" name="jboss.har:service=<identifying_name>Hibernate">
		  <attribute name="DatasourceName">java:/<data_source_name></attribute>
		  <attribute name="Dialect">net.sf.hibernate.dialect.OracleDialect</attribute>
		  <attribute name="SessionFactoryName">java:/hibernate/<identifying_name>SessionFactory</attribute>
		  <attribute name="CacheProviderClass">
				net.sf.hibernate.cache.HashtableCacheProvider
		  </attribute>
		  <attribute name="ShowSqlEnabled">false</attribute>
		  <!-- <attribute name="Hbm2ddlAuto">create-drop</attribute> -->
	 </mbean>
</server>


mapping files will automatically be found and loaded, providing they end in .hbm.xml


Session management
Session management is vastly simplified over standalone hibernate:
A Session must be retrieved from within a transaction (as it is bound to the transaction)

UserTransaction transaction = (UserTransaction) ctx.lookup( "UserTransaction" );
Session session = HibernateContext.getSession( <SessionFactoryName> );


when finished, commit or rollback the transaction. Don't close a Session. Ever. Let jboss deal with that.

session.commit();
session.rollback();


the SessionFactoryName was defined in the hibernate-service.xml

See, isn't that easy :)


Pacakging
your hibernate code, POJOs, mappings and hibernate-service.xml must be packaged in an har file, with the same structure as a standard jar. Rather than referencing this har in the application.xml, it is referenced in jboss-app.xml which lives with the application.xml in the EARs META-INF folder:


1:
2:
3:
4:
5:
<jboss-app>
	<module>
		<har>hibernate.har</har>
	</module>
</jboss-app>