facebook

Spring DAO-Hibernate 3 Integration

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #267192 Reply

    jwalkerc
    Member

    I am dying here for some instructions on how to do a simple spring dao-hibernate project with JSF. I have spent the last two days trying to configure the two together but without success. I have tried this with specifying that it use the separate hibernate.cfg.xml file and also trying it where it uses only the applicationContext.xml file. ( I am letting it create a HibernateSessionFactory class).

    Either way, hibernate does not initialize and so in my DAO class, getHibernateTemplate() only returns a null pointer. In the debugger, I have tried at that point to execute HibernateSessionFactory.rebuildSession, but usually this walks back as well. It says that it cannot locate my <class>.hbm.xml file. (It does exist and was created by the reverse engineering wizard).

    Can anyone tell me what are the basic steps to use the wizards to correctly generate the xml files and how do I get hibernate initialized either up front when Tomcat starts or when the first jsp is called.

    I have used this on myeclipse 5.1 and 5.5. I am using Spring 2.0, Spring DAO, hibernate 3, and JSF capabilities.

    Thanks.

    #267195 Reply

    Riyad Kalla
    Member

    So as not to muddy the water let’s do the simplest scenario here:

    1) Create a test project
    2) Add hibernate capabilities to it
    3) Rev-eng your DB to it, generate Default DAOs

    Does that work? Can you run the code?

    If yes, now try another project:

    1) Create a project
    2) Add spring capabilities
    3) Add hibernate capabilities
    4) Try the rev-eng process again, does the code still work?

    If any of these steps aren’t working, full stack traces or exact exceptions will be helpful in figuring out what is going on.

    #267234 Reply

    jwalkerc
    Member

    @support-rkalla wrote:

    So as not to muddy the water let’s do the simplest scenario here:

    1) Create a test project
    2) Add hibernate capabilities to it
    3) Rev-eng your DB to it, generate Default DAOs

    Does that work? Can you run the code?

    If yes, now try another project:

    1) Create a project
    2) Add spring capabilities
    3) Add hibernate capabilities
    4) Try the rev-eng process again, does the code still work?

    If any of these steps aren’t working, full stack traces or exact exceptions will be helpful in figuring out what is going on.

    I created a hibernate only project and it worked.
    I did a new project following the same steps. It did not work because the statement in the DAO, this.getHibernateTemplate() still fails. I added this code in the DAO:
    HibernateTemplate ht = this.createHibernateTemplate(HibernateSessionFactory.getSessionFactory());
    This produces a hibernate template, but if I follow in the next statement with:
    ht=this.getHibernateTemplate();
    it still returns null.

    Here is my applicationContext.xml:

    <bean id=”sessionFactory” class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
    <property name=”configLocation”>
    <value>file:src/hibernate.cfg.xml</value>
    </property>
    </bean>
    <bean id=”NotesDAO” class=”com.test.NotesDAO”>
    <property name=”hibernateTemplate”>
    <ref bean=”hibernateTemplate” />
    </property>
    </bean>
    <bean id=”hibernateTemplate” class=”org.springframework.orm.hibernate3.HibernateTemplate”>
    <property name=”sessionFactory”>
    <ref bean=”sessionFactory” />
    </property>
    </bean>

    Somehow it still neither creates a hibernate template, nor does it remember the template.

    Any suggestions?

    Thanks for the previous post.

    #267235 Reply

    Haris Peco
    Member

    jwalkerc,

    Try change line :

    <value>file:src/hibernate.cfg.xml</value>

    to

    <value>classpath:/hibernate.cfg.xml</value>

    Regards,

    #267242 Reply

    jwalkerc
    Member

    I changed the line in the applicationContext.xml to read: <value>classpath:/hibernate.cfg.xml</value>

    It made absolutely no difference.

    #267245 Reply

    Haris Peco
    Member

    Have you initialized sping application context ?
    Could you try start simple test:

    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
            YourDAO dao = (YourDAO) ctx.getBean("YourDAO");
            YourPOJO pojo = dao.findById("AR");
            System.out.println(pojo);
        }
    }
    

    Regards,

    #267260 Reply

    jwalkerc
    Member

    @support-snpe wrote:

    Have you initialized sping application context ?
    Could you try start simple test:

    
    public class Test {
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
            YourDAO dao = (YourDAO) ctx.getBean("YourDAO");
            YourPOJO pojo = dao.findById("AR");
            System.out.println(pojo);
        }
    }
    

    Regards,

    Actually, doing the above does work. The problem is that it doesn’t work in my app.

    Here is my web.xml:

    <?xml version=”1.0″ encoding=”UTF-8″?>
    <web-app xmlns=”http://java.sun.com/xml/ns/j2ee&#8221;
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; version=”2.4″
    xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”&gt;
    <context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>
    <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet>
    <servlet-name>SpringContextServlet</servlet-name>
    <servlet-class>
    org.springframework.web.context.ContextLoaderServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/applicationContext.xml</param-value>
    </context-param>
    </web-app>

    Here is my applicationContext.xml:

    <?xml version=”1.0″ encoding=”UTF-8″?>
    <beans xmlns=”http://www.springframework.org/schema/beans&#8221;
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
    xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd”&gt;
    <bean id=”sessionFactory”
    class=”org.springframework.orm.hibernate3.LocalSessionFactoryBean”>
    <property name=”configLocation”>
    <value>classpath:/hibernate.cfg.xml</value>
    </property>
    </bean>
    <bean id=”hibernateTemplate”
    class=”org.springframework.orm.hibernate3.HibernateTemplate”>
    <property name=”sessionFactory”>
    <ref bean=”sessionFactory” />
    </property>
    </bean>
    <bean id=”hibernateDaoSupport”
    class=”org.springframework.orm.hibernate3.support.HibernateDaoSupport”
    abstract=”true”>
    <property name=”sessionFactory”>
    <ref bean=”sessionFactory” />
    </property>
    <property name=”hibernateTemplate”>
    <ref bean=”hibernateTemplate” />
    </property>
    </bean>
    <bean id=”NotesDAO” class=”com.test.NotesDAO” parent=”hibernateDaoSupport”>
    <property name=”hibernateTemplate”>
    <ref bean=”hibernateTemplate” />
    </property>
    </bean>
    </beans>

    Here is my faces-config.xml

    <?xml version=”1.0″ encoding=”UTF-8″?>
    <!DOCTYPE faces-config PUBLIC “-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN” “http://java.sun.com/dtd/web-facesconfig_1_1.dtd”&gt;
    <faces-config>
    <navigation-rule>
    <from-view-id>/index.jsp</from-view-id>
    </navigation-rule>
    <managed-bean>
    <managed-bean-name>notesBean</managed-bean-name>
    <managed-bean-class>com.test.Notes</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    <managed-property>
    <property-name>id</property-name>
    <property-class>java.lang.Integer</property-class>
    <value />
    </managed-property>
    <managed-property>
    <property-name>description</property-name>
    <property-class>java.lang.String</property-class>
    <value />
    </managed-property>
    </managed-bean>
    <managed-bean>
    <managed-bean-name>notesListingBean</managed-bean-name>
    <managed-bean-class>com.test.NotesDAO</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
    </managed-bean>
    </faces-config>

    Here is my jsp page:

    <%@ page language=”java” pageEncoding=”ISO-8859-1″%>
    <%@ taglib uri=”http://java.sun.com/jsf/html&#8221; prefix=”h”%>
    <%@ taglib uri=”http://java.sun.com/jsf/core&#8221; prefix=”f”%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + “://”
    + request.getServerName() + “:” + request.getServerPort()
    + path + “/”;
    %>
    <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
    <html>
    <head>
    <base href=”<%=basePath%>”>
    <title>Notes Listing</title>
    <!–
    <link rel=”stylesheet” type=”text/css” href=”styles.css”>
    –>
    </head>
    <body>
    <f:view>
    <h:outputText value=”Notes Listing” />
    <h:form id=”listingForm”>
    <h:dataTable id=”table”
    value=”#{notesListingBean.notes}” var=”note”>
    <h:column>
    <f:facet name=”header”>
    <h:outputText value=”Note ID” />
    </f:facet>
    <h:outputText value=”#{note.id}” />
    </h:column>
    <h:column>
    <f:facet name=”header”>
    <h:outputText value=”Note Description” />
    </f:facet>
    <h:outputText value=”#{note.description}” />
    </h:column>
    </h:dataTable>
    </h:form>
    </f:view>
    </body>
    </html>

    I would believe that somehow when my NotesDAO is called, either there is no context, or it is in the wrong context.

    Thanks.

    #267286 Reply

    Haris Peco
    Member

    You have to use Spring’s DelegatingVariableResolver class for Spring-JSF integration.
    Look at on this link http://static.springframework.org/spring/docs/2.0.x/reference/webintegration.html

    You would use ContextLoaderListener (and not ContextLoaderServlet) for initialization Spring context.Check the link before for using DAO in Spring-JSF integration.You have to car on hibernate lazy loading as well.See this http://www.jroller.com/page/kbaum?entry=orm_lazy_initialization_with_dao.

    Unfortunately, MyEclipse can’t help for this integration in this moment.

    Regards,

Viewing 8 posts - 1 through 8 (of 8 total)
Reply To: Spring DAO-Hibernate 3 Integration

You must be logged in to post in the forum log in