-->
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.  [ 7 posts ] 
Author Message
 Post subject: Using AspectJ to open and close sessions in Hibernate
PostPosted: Wed Jul 21, 2004 12:26 pm 
Newbie

Joined: Wed Jul 21, 2004 12:18 pm
Posts: 9
Hello,

I am a newbie in AspectJ and Hibernate and have been working on writing a aspect ( using aspectj) which basically calls openSession and closeSession from a HibernateUtil class. Somehow, the pointcut in aspect program never gets invoked. Can anyone help me with this?

Thanks in advance

Madhuri


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 1:41 pm 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
If you could post the code here, probably somebody can help. I am new to Hibernate but I have been using AspectJ for sometime now.

Thanks,
Binil


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 2:35 pm 
Newbie

Joined: Wed Jul 21, 2004 12:18 pm
Posts: 9
Hello Everyone and Bipin!


I have a HibernateUtil class which is an example given in HIbernate documentation. I have a aspect which upon calling any method in TopicUserAdmin.java should invoke the Hibernate session. I want the session to be available in java class. Any help on this will be great.

---------------------------------------------------------------------------------

Sample code for TopicUserAdmin

public class TopicUserAdmin{

Session session;
public String addTopic(){

//some code.
session.save();
return "success";

}

}

------------------------------------------------------------------------------------
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}


-----------------------------------------------------------------------------------
package example.aspect;

import example.businessModel.TopicUserAdmin;


public aspect HibernateAspect
{

pointcut hibernateFetch(TopicUserAdmin tua) : call
(public * *.*(..)) && target(tua);

before(TopicUserAdmin tua) : hibernateFetch(tua)
{
tua.sess = HibernateHelper.openSession();
}

after(TopicUserAdmin tua) returning : hibernateFetch(tau)
{
HibernateHelper.closeSession();
}

after(TopicUserAdmin tua) throwing : hibernateFetch(tua)
{
HibernateHelper.closeSession();
}
}

--------------------------------------------------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 21, 2004 3:28 pm 
Newbie

Joined: Wed Jul 21, 2004 12:18 pm
Posts: 9
madhuri.sayana wrote:
Hello Everyone and Bipin!


I have a HibernateUtil class which is an example given in HIbernate documentation. I have a aspect which upon calling any method in TopicUserAdmin.java should invoke the Hibernate session. I want the session to be available in java class. Any help on this will be great.

---------------------------------------------------------------------------------

Sample code for TopicUserAdmin

public class TopicUserAdmin{

public String addTopic(){

//some code.
return "success";

}

}

------------------------------------------------------------------------------------
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Configuration problem: " + ex.getMessage(), ex);
}
}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}


-----------------------------------------------------------------------------------
package example.aspect;

import example.businessModel.TopicUserAdmin;


public aspect HibernateAspect
{
Session sess;
pointcut hibernateFetch(TopicUserAdmin tua) : call
(public * *.add(..)) && target(tua);

before(TopicUserAdmin tua) : hibernateFetch(tua)
{
sess = HibernateUtil.currentSession();
}

after(TopicUserAdmin tua) returning : hibernateFetch(tau)
{
sess.save();
HibernateUtil.closeSession();
}

after(TopicUserAdmin tua) throwing : hibernateFetch(tua)
{
sess.rollback();
HibernateUtil.closeSession();
}
}

--------------------------------------------------------------------------------


Top
 Profile  
 
 Post subject: /.
PostPosted: Wed Jul 21, 2004 3:36 pm 
Newbie

Joined: Wed Jul 21, 2004 12:18 pm
Posts: 9
.'k


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 22, 2004 6:03 am 
Beginner
Beginner

Joined: Tue Jul 20, 2004 1:53 am
Posts: 43
Location: India
madhuri.sayana wrote:
Hello Everyone and Bipin!
I have a HibernateUtil class which is an example given in HIbernate documentation. I have a aspect which upon calling any method in TopicUserAdmin.java should invoke the Hibernate session. I want the session to be available in java class. Any help on this will be great.


This really is an AspectJ question, so probably you should have posted it to AspectJ lists.

Your pointcut is :
Code:
pointcut hibernateFetch(TopicUserAdmin tua) : call (public * *.add(..)) && target(tua);


This pointcut does not
1. Consider the package structure of TopicUserAdmin class
2. Advises the call of add() method, rather that addTopic() method

Try fixing these issues with the pointcut and I hope that the Aspect will work for you.

Thanks,
Binil


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 22, 2004 7:20 am 
Newbie

Joined: Wed Jul 21, 2004 12:18 pm
Posts: 9
Hello Binil,

Thanks a lot..Yes, I was AspectJ issue and I messed around corecting the syntax and it works. Once again, thanks.

Madhuri


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.