Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
spring-modules-jcr - Implementing Spring-based DAOs without callbacks Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-modules-jcr - Implementing Spring-based DAOs without callbacks

    Hi all,

    I am trying to implement the following example (Implementing Spring-based DAOs without callbacks), as here: https://springmodules.dev.java.net/d...ingle/#d0e1669

    Within my DAO (which extends JackrabbitDAOSupport, which itself extends JcrDaoSupport), getSession(true) is throwing the following exception: java.lang.IllegalArgumentException: No sessionFactory specified.

    Here is my code:

    DAO:
    Code:
    public class ProviderDAOImpl extends JackrabbitDAOSupport
    {
    
    	private static org.apache.log4j.Logger _lLogger = org.apache.log4j.Logger.getLogger(ProviderDAOImpl.class);
    
    
    	public ProviderDAOImpl()
    	{
    		super();
    	}
    
    	public ArrayList<ProviderBean> listProviders()
    	{
    		Session jcrSession = getSession(true);
    		ArrayList<ProviderBean> providers = new ArrayList<ProviderBean>();
    
    		try
    		{
    			Workspace ws = jcrSession.getWorkspace();
    			QueryManager qm = ws.getQueryManager();
    
    			// Specify a query using the XPATH query language
    			Query q = qm.createQuery("//provider[@isDeleted!='true']", Query.XPATH);
    			QueryResult res = q.execute();
    
    			// Obtain a node iterator
    			NodeIterator provs = res.getNodes();
    			_lLogger.debug(provs.getSize());
    
    			while (provs.hasNext())
    			{
    				Node providerNode = provs.nextNode();
    				providers.add(new ProviderBean(providerNode.getUUID(), providerNode.getProperty("name").getString(), providerNode.getProperty("GUID")
    						.getString(), providerNode.getProperty("isDeleted").getBoolean()));
    			}
    
    		} catch (Exception e)
    		{
    			_lLogger.fatal(e.getCause(), e);
    		} finally
    		{
    		}
    
    		return providers;
    	}
    }
    JackrabbitDAOSupport:
    Code:
    public class JackrabbitDAOSupport extends JcrDaoSupport
    {
    	JcrSessionFactory sessionFactory;
    	JcrTemplate template;
    	
    	public JackrabbitDAOSupport()
    	{
    		
    	}
    }
    applicationContext-jcr.xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                               http://www.springframework.org/schema/context
                               http://www.springframework.org/schema/context/spring-context.xsd">
    
    	<!-- Register Annotation-based Post Processing Beans -->
    	<context:annotation-config />
    
    	<!-- Creates a session factory based on the respository -->
    	<bean id="sessionFactory" class="org.springmodules.jcr.JcrSessionFactory">
    		<property name="repository" ref="repository" />
    		<property name="credentials">
    			<bean class="javax.jcr.SimpleCredentials">
    				<constructor-arg index="0" value="userid" />
    				<!-- create the credentials using a bean factory -->
    				<constructor-arg index="1">
    					<bean factory-bean="password" factory-method="toCharArray" />
    				</constructor-arg>
    			</bean>
    		</property>
    	</bean>
    
    	<!-- create the password to return it as a char[] -->
    	<bean id="password" class="java.lang.String">
    		<constructor-arg index="0" value="" />
    	</bean>
    
    	<!-- Creates a JcrTemplate using the session factory -->
    	<bean id="jcrTemplate" class="org.springmodules.jcr.JcrTemplate">
    		<property name="sessionFactory" ref="sessionFactory" />
    		<property name="allowCreate" value="true" />
    	</bean>
    
    	<!-- DAO configurations -->
    	<bean id="jackrabbitDAOSupport"
    		class="net.gb.mds.atl.ecommerce.dao.JackrabbitDAOSupport">
    		<property name="sessionFactory" ref="sessionFactory" />
    		<property name="template" ref="jcrTemplate" />
    	</bean>
    
    	<!--
    		normal repository The first bean definition defines defines the
    		Jackrabbit repository by specifying the configuration file to use and
    		the location of the repository. If the repository doesn't already
    		exist, it will be created on startup.
    	-->
    	<bean id="repository" class="org.springmodules.jcr.jackrabbit.RepositoryFactoryBean">
    		<!-- normal factory beans params -->
    		<property name="configuration" value="classpath:repository.xml" />
    		<!-- use the target folder which will be cleaned  -->
    		<property name="homeDir" value="file:C:/repository1" />
    
    	</bean>
    
    </beans>
    I understand that I may need to inject the jcrTemplate into my DAO (as getSession() eventually calls template.getSessionFactory(), and in the debugger template == null).

    Please could someone help me out? I've tried with and without using the JackrabbitDAOSupport class (i.e. in the spring config file). I've read over and over the tutorial example but it seems that some information is missing (or more likely I am doing something daft). Running through the debugger I can see that the initial call to JcrDaoSupport's setSessionFactory() is correctly instantiating the member template.

    Thanks in advance for any help.

  • #2
    Is anyone able to advise a Spring newbie, please? I'm still stuck with this and have spent quite some time trying to work it out. It seems that when my web app starts, the DAO's template is correctly set but later when I instantiate and use my DAO, getSession(true) fails as the DAO's template is null.

    Where / how should I set the DAO's template / template's sessionFactory?

    Comment

    Working...
    X