myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alejandro Valdez <alejandro.val...@gmail.com>
Subject MyFaces Orchestra: How to Integrate with Hibernate ?
Date Sat, 06 Jun 2009 15:46:44 GMT
Hello list, I'm trying to integrate MyFaces Orchestra with Hibernate
(and Spring 2.5.6), I have already downloaded the example application
from http://code.google.com/p/myfaces-orchestra-goodies/ where an
HibernatePersistentContextFactory is provided, there is also an
example of a DAO that extends the Spring's HibernateDaoSupport class.

I don't like the idea of implement my DAOs extending a Spring class,
so instead I wrote them as the example shown below.

It seems that I'm doing something wrong, because I'm having two kind
of HibernateExceptions at different points of my application, in one
case it's telling that a collection is used in another session, in the
other case that a session is closed.

The application is layered in this way: [conversation score beans] ->
[service layer] -> [dao layer] -> [hibernate]

I can't figure out how HibernatePersistentContextFactory is assigning
the same session to the different DAOs in the dao layer, I think it
isn't happening and that is the cause of the exceptions.

Hope someone can point me in the right direction.

Examples:

@Repository("userDAO")
public class HibernateUserDAO implements UserDAO {

	@Autowired(required=true)
	private SessionFactory sessionFactory;
	
	public HibernateUserDAO() {}
	
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public List<UserDto> getAll() {
		Session session = getSession();
		Query query = session.createQuery("FROM UserDto AS user ORDER BY
user.username ASC");
		return (List<UserDto>) query.list();
	}

// other methods...
}


@Service("userService")
public class UserServiceImpl implements UserService {
	
	@Autowired(required=true)
	private UserDAO userDAO;
	
	@Transactional(readOnly = true)
	public List<UserDto> getAll(){
		return userDAO.getAll();
	}
// other methods...
}


@Component("userAdministrationBean")
@Scope("conversation.access")
@ConversationName("userAdministrationBean")
public class userAdministrationBean {
	
	UserDto currentUser;
	@Autowired(required=true)
	private UserService userSvc;

	public List<UserDto> getAllUsers() {
		return this.userSvc.getAll();
	}
// other methods...
}


If you are still reading this, this is the interesting part of the
Spring application-context.xml:

    <tx:annotation-driven/>

    <!-- 3. the "entity manager" manager -->
    <bean id="persistentContextConversationInterceptor"
        class="org.apache.myfaces.orchestra.conversation.spring.PersistenceContextConversationInterceptor">
      <property name="persistenceContextFactory"
ref="persistentContextFactory"/>
    </bean>

    <!-- 4. conversation - persistence adapter -->
    <bean id="persistentContextFactory"
        class="my.application.db.HibernatePersistenceContextFactory">
      <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

	<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="entityManagerFactory"/>
	</bean>

	<bean id="entityManagerFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation">
			<value>classpath:hibernate.cfg.xml</value>
		</property>
	</bean>

Mime
View raw message