hibernate甚至没有尝试连接?

时间:2013-02-01 05:15:15

标签: hibernate spring-mvc

您好我正在尝试学习hibernate 4和SPRING 3.2

但是我甚至无法通过hibernate来尝试连接到数据库。由于我故意提供了错误的凭据,我仍然没有收到任何错误。我试过这个是因为我在控制器上得到了一个nullpointer异常:

@Controller
public class HibernateDemoController {

private MovieService movieService;


@RequestMapping(value="/hibernatedemo", method= RequestMethod.GET)
public String index(ModelMap map) {

    //map.addAttribute("hibernate", "Hibernate with DAO");
    Map model = new HashMap();
    //try{
        model.put ( "movies", movieService.getAllMovies() );
    //}catch(Exception ex){
    //    model.put("movies", ex.toString());
    //}

    return "hibernate";
  }
}

movieService

@Service("movieService")
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)
public class MovieServiceImpl implements MovieService {


private HibernateMovieDao movieDao;


@Override
public void addMovie(Movie movie) {
    throw new UnsupportedOperationException("Not supported yet.");
}


@Override
  public List<Movie> getAllMovies() {
    return movieDao.getAllMovies();
  }
}

电影DAO

@Repository("movieDao")
public class HibernateMovieDaoImpl implements HibernateMovieDao {

private SessionFactory sessionFactory;

@Autowired
public HibernateMovieDaoImpl(SessionFactory sessionFactory){
    this.sessionFactory = sessionFactory;
}

@Override
public void saveMovie(Movie movie) {
    throw new UnsupportedOperationException("Not supported yet.");
}

@Override
   public List<Movie> getAllMovies() {
    return (List<Movie>) sessionFactory.getCurrentSession().createCriteria (
    Movie.class ).list();

   }
 }

我的dispatcher-xml

的一部分
 <context:component-scan base-package="com.outbottle" /> 
 <context:property-placeholder location="classpath:jdbc.properties" />
 <mvc:annotation-driven />

<bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${database.driver}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"  
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <!--<value>com.springmvc.model.User</value>-->
            <value>com.outbottle.hellospring.entities.Movie</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${dataSource.dialect}</prop>
            <prop key="hibernate.show_sql">${dataSource.show_sql}</prop>
            <prop key="hibernate.connection.pool_size">${dataSource.connection.pool_size}</prop>
        </props>
    </property>
</bean>

<bean id="hibernatemovieDao" class="com.outbottle.hellospring.dao.impl.HibernateMovieDaoImpl" >
    <constructor-arg ref="sessionFactory"/>
</bean>

<bean id="transactionManager" 
      class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>'

关于我错过的任何想法?

0 个答案:

没有答案