These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: hbm2dao
PostPosted: Thu Mar 16, 2006 11:18 am 
Newbie

Joined: Wed Mar 08, 2006 12:25 pm
Posts: 16
Adding a <hbm2dao> line within my Ant hibernatetool task generates XxxHome classes.

These DAO classes expect the SessionFactory to be available from JNDI. Is there any attribute (or any way) that will enable me to override this and use the thread-local session pattern instead?

What attributes (other than jdk5/ejb3) does this task accept?

David


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 16, 2006 11:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
not currently, but patches for controlling how the session is aquired is welcome

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 30, 2006 8:10 am 
Newbie

Joined: Wed Aug 30, 2006 7:45 am
Posts: 2
Well, I've no patch to deliver but a workaround using <hbmtemplate>. It's going this way:

<hbm2dao> simply triggers an exporter (http://anonhibernate.labs.jboss.com/tru ... orter.java). This exporter uses a template file you can find in the hibernate-tools.jar at dao/daohome.ftl.

I've simply copied this template and modified it a little bit to suit my needs: Receive Hibernate's SessionFactory through a constructor parameter - in a dependency injection-style fashion. I've named it my-dao-template.ftl.

Removing this part
---------------
private final ${pojo.importType("org.hibernate.SessionFactory")} sessionFactory = getSessionFactory();

protected ${pojo.importType("org.hibernate.SessionFactory")} getSessionFactory() {
try {
return (${pojo.importType("org.hibernate.SessionFactory")}) new ${pojo.importType("javax.naming.InitialContext")}().lookup("${sessionFactoryName}");
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
---------------
and adding this one
---------------
private final ${pojo.importType("org.hibernate.SessionFactory")} sessionFactory;

/**
* Dependency injecting constructor.
*/
public ${declarationName}Home (final ${pojo.importType("org.hibernate.SessionFactory")} sessionFactory) {
super();
this.sessionFactory = sessionFactory;
}
---------------
should yield the required code.

Now add this code inside your ANT build.xml file's <hibernatetool>-Target:

<property key="ejb3" value="false"/>
<property key="jdk5" value="true"/>
<hbmtemplate
template="my-dao-template.ftl"
filepattern="{package-name}/{class-name}Home.java"/>

It is required to set some properties (namely ejb3 and jdk5) usually set by the DAOExporter class.
First make sure your template file "my-dao-template.ftl" is found in the classpath of hibernatetool. Second be aware of setting the filepattern correctly: It has to match the "Home"-suffixed class name used in the template.

This solution should open up your DAO to be configurable externally.

Greetings

Christian


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.