Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC 3.1.2 + Hibernate 3 + Google App Engine Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC 3.1.2 + Hibernate 3 + Google App Engine

    Hello,

    Having basic experience with Java EE, I've decided to create my engineering project for university with Spring MVC, extended Hibernate to deal with persistance layer, and host it at Google App Engine. Also, I've hosted separate MySQL database, outside Google, of course with remote access.

    My config looks like that:

    Spring Core 3.2
    Spring MVC 3.1.2 Release
    Hibernate 3.6.10
    AppEngine 1.7.3

    IntelliJ IDEA JDK

    My goal for now is gain of acces to database data. I can't manage to get it.


    AppContext:
    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:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://mydatabaseurl.com:3306/68_cmsSpringDatabase?useUnicode=yes&amp;characterEncoding=UTF-8"/>
            <property name="username" value="68_databaseRoot" />
            <property name="password" value="password"/>
        </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="packagesToScan" value="entity"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
        </bean>
        <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    
        <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
        <context:component-scan base-package="util.dao.hibernate" />
    </beans>
    Dispatcher
    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:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            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-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
    
        <mvc:annotation-driven/>
        <context:component-scan base-package="component.controller"/>
    
        <!-- View Resolver -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    
    </beans>
    UserDAO
    Code:
    @Repository
    @Transactional
    public class UserDAO implements UserDAOInterface {
    
        private SessionFactory sessionFactory;
    
        @Autowired
        public UserDAO(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    
        public void add(UserEntity userEntity) {
            sessionFactory.getCurrentSession().save(userEntity);
        }
    
        public void update(UserEntity userEntity) {
            sessionFactory.getCurrentSession().update(userEntity);
        }
    
        public void delete(UserEntity userEntity) {
            sessionFactory.getCurrentSession().delete(userEntity);
        }
    
        public UserEntity getById(Integer uid) {
            return (UserEntity) sessionFactory.getCurrentSession().get(UserEntity.class, uid);
        }
    
        public List<UserEntity> getAll(UserEntity userEntity) {
            return sessionFactory.getCurrentSession().createCriteria(UserEntity.class).list();
        }
    }

    UserController
    Code:
    @Controller
    @RequestMapping("/user")
    public class UserController {
    
    
        @Autowired
        private UserDAO userDAO;
    
        @RequestMapping(method = RequestMethod.GET)
        public String listUsers(Map<String, Object> model) {
    
            UserEntity userEntity = new UserEntity();
    
            /*userEntity.setEmail("[email protected]");
            userEntity.setName("Piotr");
            userEntity.setSurname("Paul");
            userEntity.setPassword("admin1");
            userEntity.setRegistrationDate(new Timestamp(new Date().getTime()));*/
    
            userEntity = userDAO.getById(2);
    
            model.put("user", userEntity);
            return "user/display";
    }


    And I'm getting that error, when trying to get localhost:8080/user:


    Code:
    SEVERE: Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private util.dao.hibernate.UserDAO component.controller.UserController.userDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [util.dao.hibernate.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

    I'd really appriciate any help, because after 3 days I can't manage it..

    Thanks in advice,
    Piotr

  • #2
    I've noticed, that when I add @Transactional to the Serviece, I'm getting bean wiring problem, but in the other way

    Code:
        No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    Comment

    Working...
    X