ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

Using the DAO object of ZetaDB Form Builder ?

TLHP
5 Jun 2009 08:40:37 GMT
5 Jun 2009 08:40:37 GMT

Hi everybody,

I want to know how can we use the class DAO generated by ZetaDB Form Builder for other purpose?
For example in my class, i want to use a object DAO to to the access into database, but this object is always null, how can I initialize it ???

public class Test
{
  @Resource
  ItemDAO itemDAO; 
  
  public boolean checkExistance(int idItem)
  {    
    try
    {     
      Item item  = itemDAO.find(idItem);  (Here java.lang.NullPointerException is raised)
      System.out.print(item.getName());
    } 
    catch (EntityNotFoundException e)
    {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
}

Thanks in advance

robertpic71Top Contributor
5 Jun 2009 17:20:16 GMT
5 Jun 2009 17:20:16 GMT

Could you check/post your spring-config?

<!-- ZK Spring config -->
<zksp-annot:component-scan base-package="org.zkoss.spring.jpa,org.zkforge.todo" />
<zksp:zk-config />

The DAO's extends the BasicDAO inside the org.zkoss.spring.jpa-package. You have to configure spring to scan the zk-package. W/o zk-package no EntityManager will be injected.

/Robert

TLHP
7 Jun 2009 22:07:27 GMT
7 Jun 2009 22:07:27 GMT

thanks Robert, here my test

- I create a ZK project, use ZETA Form Builder to build a CRUD application
- CheckDAO.java , i just want to use a DAO class generated by ZETA to search a bean from database and show one of those attribut via showTheName method.

package testDAO;

import javax.annotation.Resource;
import org.zkoss.spring.jpa.EntityNotFoundException;

import test.help_topic.Help_topicDAO;
import test.help_topic.base.Help_topic;

public class CheckDAO {
	@Resource
	Help_topicDAO helpDAO;
	public String showTheName()
	{
		try {
			Help_topic helpBean = helpDAO.find(1); // find bean with ID=1
			return helpBean.getName();
		} catch (EntityNotFoundException e) {			
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "Bean not found";
		}		
	}	
}

- application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:sec="http://www.springframework.org/schema/security"
    xmlns:zksp="http://www.zkoss.org/2008/zkspring"
    xmlns:zksp-annot="http://www.zkoss.org/2008/zkspring-annot"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd
    http://www.zkoss.org/2008/zkspring http://www.zkoss.org/2008/zkspring/zkspring.xsd
    http://www.zkoss.org/2008/zkspring-annot http://www.zkoss.org/2008/zkspring-annot/zkspring-annot.xsd
	">
	
	<!--
	ZK Spring config 
	 -->    
	<zksp-annot:component-scan base-package="org.zkoss.spring.jpa,test,testDAO"/>
	<zksp:zk-config/>
	
	<!-- 
    Injected properties
    --> 
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	    <property name="locations">
	    <list>            
            <value>classpath:/config/datasource.properties</value>
	    </list>
	    </property>
        
    </bean>
</beans>

Robert, if you did that work, let me know or show me some code, I appreciate that

Thanks

TLHP
19 Jun 2009 19:33:50 GMT
19 Jun 2009 19:33:50 GMT

Still need help ...

Thanks

iantsai
19 Nov 2009 01:09:20 GMT
19 Nov 2009 01:09:20 GMT

You declared a Test class here means it also need to be managed by Spring.

Why not just use Spring BeanFactory to get ItemDAO instance back at very first?

jeanher
19 Nov 2009 21:51:32 GMT
19 Nov 2009 21:51:32 GMT

I have done this but when I do it I can get the bean but the basic dao service inside it is null.

- by Jacques

soumaya
24 Aug 2011 12:03:27 GMT
24 Aug 2011 12:03:27 GMT

Hi ,

I am having the same probleme. Did you find a solution??


Thanks