Announcement Announcement Module
Collapse
No announcement yet.
Can't access the database in my Spring MVC project Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can't access the database in my Spring MVC project

    I am working with STS in eclipse Juno ,Spring 3.1.1 ,hibernate 4.1, tomcat 7 and mySQL.

    I created a simple MVC template project. my purpose is that the user will enter some data to a form, and that data will
    be saved in the database.

    I have created:

    web layer:

    1. the simple form.
    2. the controller which recieve the data from the form and pass it to the service layer.

    service layer:

    a service class which contains a DAO field and operate a dao method.

    data access layer:

    1. A mock DAO implementation which doesn't commincate with the database.
    2. A Real DAO implementation which doesn commincate with the database.

    When i checked the system with the mock DAO implementation, everything was OK - going from the web layer to the mock DAO.

    but when injected the real DAO, i just got an 404 error, and nothing in the happened in the Database.

    I will show only the DAO implementation and the root-context.xml because this is where i think the problem.

    My DAO Implementation:

    Code:
        @Repository
        public class Presentation_page_dao_hibernate_Impl implements Presentation_page_dao {
        	
        	private  SessionFactory sessionFactory;
        	
        	@Autowired
        	public Presentation_page_dao_hibernate_Impl(SessionFactory sessionFactory) {
        		this.sessionFactory=sessionFactory;
        		System.out.println("Hi! i'm in ActionDao_HibernateImpl constructor");
        		}
        
        	private Session currentSession() {
        		return sessionFactory.getCurrentSession();
        	}
        
        
        	public void create(Presentation_page pp) {	
        		currentSession().beginTransaction();
        		currentSession().save(pp);
        		currentSession().getTransaction().commit();
        		currentSession().close();		
        	}
        
        
        	public Presentation_page read(int pageid) throws PresentationPageNotFoundException {
        		
        		
        		currentSession().beginTransaction();
        		
        		Criteria criteria=currentSession().createCriteria(Presentation_page.class);
        		
        		criteria.add(Restrictions.eq("page_id", pageid));
        		List<Presentation_page> list_of_pages=criteria.list();
        		
        		currentSession().getTransaction().commit();
        		currentSession().close();
        		
        		for(Presentation_page pp:list_of_pages) {
        			if (pp.getPage_id()==pageid){
        				return pp;
        			}
        		}
        		return null;
        	}
        
        	public void update(Presentation_page pp) throws PresentationPageNotFoundException {
        
        		currentSession().beginTransaction();
        		currentSession().update(pp);
        		currentSession().getTransaction().commit();
        		currentSession().close();	
        		
        	}
        
        	@Override
        	public void delete(Presentation_page  pp) throws PresentationPageNotFoundException {
        
        		currentSession().beginTransaction();
        		currentSession().delete(pp);
        		currentSession().getTransaction().commit();
        		currentSession().close();
        	}
        
        }
    This is my root-context.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"
        	xmlns:jee="http://www.springframework.org/schema/jee"
        	xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">	
        	
        	<!-- Root Context: defines shared resources visible to all other web components -->
        	
        	
        	<!-- For annotations -->
        	<context:component-scan 
        		base-package="my.topLevel.pack">
        	</context:component-scan> 
        	
        	<import resource="hibernate.xml"/>
        	
        
        </beans>
    This is my hibernate.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:jee="http://www.springframework.org/schema/jee"
        	xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">	
        	
            
        
        	  <!-- Datasources -->
         	<bean id="dataSource_id" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        		<property name="url" value="jdbc:mysql://localhost:3306/spring_presentation"/>
        		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        		<property name="username" value="rotemya"/>
        		<property name="password" value="*******"/> 	
           </bean> 
        	
        	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        		
        		<property name="dataSource" ref="dataSource_id"/>
        		
        		<property name="packagesToScan" value="my.topLevel.pack.Domain"/>
        		
        		<property name="hibernateProperties">
        			<props>
        			
        				<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
               			
        				<!--JDBC connection pool (use the built-in)-->        		
        				<prop key="connection.pool_size">1</prop>
        
                		<!--Enable the second-level cache -->
               			<prop key="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>  
                
                		<!--Echo all executed SQL to stdout-->
                		<prop key="show_sql">true</prop>
        
                		<!--Drop and re-create the database schema on startup-->
                		<prop key="hbm2ddl.auto">create</prop>
        			</props>
        		</property>
        		
        	</bean>
        
        </beans>
    I have the following dependencies to the pom.xml:

    Code:
        <?xml version="1.0" encoding="UTF-8"?>
        <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        	<modelVersion>4.0.0</modelVersion>
        	<groupId>my.topLevel</groupId>
        	<artifactId>pack</artifactId>
        	<name>SpringSTS_Sample_Project</name>
        	<packaging>war</packaging>
        	<version>1.0.0-BUILD-SNAPSHOT</version>
        	<properties>
        		<java-version>1.6</java-version>
        		<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
        		<org.aspectj-version>1.6.10</org.aspectj-version>
        		<org.slf4j-version>1.6.6</org.slf4j-version>
        	</properties>
        	<dependencies>
        		<!-- Spring -->
        		<dependency>
        			<groupId>org.springframework</groupId>
        			<artifactId>spring-context</artifactId>
        			<version>${org.springframework-version}</version>
        			<exclusions>
        				<!-- Exclude Commons Logging in favor of SLF4j -->
        				<exclusion>
        					<groupId>commons-logging</groupId>
        					<artifactId>commons-logging</artifactId>
        				 </exclusion>
        			</exclusions>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework</groupId>
        			<artifactId>spring-webmvc</artifactId>
        			<version>${org.springframework-version}</version>
        		</dependency>
        				
        		<dependency>
        			<groupId>org.springframework</groupId>
        			<artifactId>spring-orm</artifactId>
        			<version>3.0.3.RELEASE</version>
        		</dependency>
        		<dependency>
        			<groupId>org.springframework</groupId>
        			<artifactId>spring-jdbc</artifactId>
        			<version>3.0.3.RELEASE</version>
        		</dependency>
        		<dependency>
        			<groupId>org.eclipse.persistence</groupId>
        			<artifactId>javax.persistence</artifactId>
        			<version>2.0.0</version>
        		</dependency>
        		<dependency>
        			<groupId>mysql</groupId>
        			<artifactId>mysql-connector-java</artifactId>
        			<version>5.1.13</version>
        		</dependency>
        	</dependencies>
          
        </project>
    This are my hibernate jar files:
    Attachment

    I'm getting the following error:

    java.lang.NoClassDefFoundError: org/hibernate/criterion/Criterion

    Any ideas?
    Attached Files
    Last edited by rotemya; Mar 4th, 2013, 05:22 AM.

  • #2
    Hey!

    I moved this thread over to the web forum since I think it is more related to the application itself than the tooling.

    HTH,
    Martin

    Comment


    • #3
      Check your logs... If you get a 404 yuor application didn't start properly, which indicates a configuration problem (in general). One thing I notice is you are adding hibernate4 as a dependency and using hibernate3 for your configuration classes, that obviously isn't going to work. Spring hasn't bean tested with 4.2 (AFAIK) I suggest sticking with 4.1.x for now (4.2 isn't final either it is still in development).

      Also why do you have such weird classnames, not related but not common to use _ in classnames.

      Final note your connection.* properties on hibernate are useless as you are injecting a datasource so those do nothing.

      Comment


      • #4
        Hi Marten and thanks,

        I didn't understand why my - connection.* properties on hibernate are useless

        I've changed the jar files like you said, so i edited the question and showed the error that i'm getting.

        Comment


        • #5
          I didn't understand why my - connection.* properties on hibernate are useless
          You are injecting a spring managed datasource so hibernate isn't managing the datasource and as such properties regarding connections and datasources on hibernate are useless.

          Also have you read my post?

          Originally posted by Marten Deinum
          One thing I notice is you are adding hibernate4 as a dependency and using hibernate3 for your configuration classes, that obviously isn't going to work.
          Check your configuration that uses a class from the sprnig hibernate3 package not hibernate4.

          Also I mentioned Hibernate 4.1 not 4.0. It should also be enough to just add hibernate-entitymanager as a dependency everything else will get pulled in as a transitive dependency.

          Comment


          • #6
            Martin,

            1. I changed the jar files like you said - have a look at the png file in the question, do you still think there is problem with the jars?


            2. Can you copy+paste the code where i'm injecting a spring managed datasource (this configuration as worked for me in other projects with hibernate, and everything was working there)..

            3. Do you know what is causing the error: java.lang.NoClassDefFoundError: org/hibernate/criterion/Criterion

            Comment


            • #7
              I'm realy stuck here..help? anybody?

              Comment


              • #8
                Hey!

                In addition to what Marten suggested it looks like you have your Hibernate JARs configured in your project as coming from a Library that is configured within your IDE (and not via Maven as the rest of your project). I wonder if the m2e (and m2e-wtp) integration is able to deal with this situation correctly and deploy those libs into your webapp when you deploy it to your tc Server.

                You could check this by looking at the deployed app inside your tc Server instance folder. You should find your app there, along with all the libs that got deployed. And I guess hibernate is not there.

                HTH,
                Martin

                Comment


                • #9
                  Thanks Martin,

                  So you're saying that i should remove this hibernate library file:
                  Attachment
                  And add all the hibernate dependencies via maven?
                  Attached Files

                  Comment


                  • #10
                    Hey!

                    I think that would make sense, yes... But I think you don't need to add every single entry as a dependency, since Maven takes care of resolving transitive dependencies for you.

                    You could also take a look at the "Deployment Assembly" settings of your project (in the project properties). It shows you how the libs are deployed into the web app. Maybe the Hibernate library container from your classpath is missing there.

                    HTH
                    -Martin

                    Comment


                    • #11
                      Thanks Martin - i removed the hibernate library path and the error was gone!!

                      I think i'm realy close to access the data base. but now i'm getting the follwing error :

                      Code:
                      org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [application-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [application-context.xml] cannot be opened because it does not exist
                      First of all, my context file is root-context.xml and not application-context.xml (i don't know why spring is looking for THIS file).

                      Here is a snapshot of my project path:

                      Attachment

                      Here is my web.xml file (you can see that i'm calling root-context.xml, and not application-context.xml):

                      Code:
                      <?xml version="1.0" encoding="UTF-8"?>
                      <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
                      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
                      
                      	<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
                      	<context-param>
                      		<param-name>contextConfigLocation</param-name>
                      		<param-value>/WEB-INF/spring/root-context.xml</param-value>
                      	</context-param>
                      	
                      	<!-- Creates the Spring Container shared by all Servlets and Filters -->
                      	<listener>
                      		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                      	</listener>
                      
                      	<!-- Processes application requests -->
                      	<servlet>
                      		<servlet-name>appServlet</servlet-name>
                      		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                      		<init-param>
                      			<param-name>contextConfigLocation</param-name>
                      			<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
                      		</init-param>
                      		<load-on-startup>1</load-on-startup>
                      	</servlet>
                      		
                      	<servlet-mapping>
                      		<servlet-name>appServlet</servlet-name>
                      		<url-pattern>/</url-pattern>
                      	</servlet-mapping>
                      
                      </web-app>

                      Any ideas why i'm getting the "file does not exist" error?
                      Attached Files
                      Last edited by rotemya; Mar 4th, 2013, 10:00 AM.

                      Comment

                      Working...
                      X