类没有在休眠

时间:2015-07-03 18:35:30

标签: java hibernate

我' m遵循简单教程并获取类未映射错误。我发现了很多关于这个的帖子,他们说我必须使用我的实体类名。但是我在这里使用实体类名称作为Pizza(我的数据库表名称是pizzas)。我仍然得到错误。可以有人帮忙吗?

道上课。

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.kaveen.model.Pizza;

public class PizzaDao {

@Autowired
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sf){
    this.sessionFactory = sf;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Transactional
public List<Pizza> findAll() {
    Session session = this.sessionFactory.getCurrentSession();
    List pizzas = session.createQuery("from Pizza").list();

    return pizzas;
   }
  }

实体类

package com.kaveen.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="pizzas")
public class Pizza {

 @Id
 @GeneratedValue
 private int id;

@Column
private String name;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
   }

 }

控制器

package com.kaveen.controller;

  import java.util.List;

  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;

  import com.kaveen.dao.PizzaDao;
  import com.kaveen.model.Pizza;

  @Controller
  @RequestMapping("/")
  public class PizzaController {

  @Autowired
  private PizzaDao pizzaDao;

  @RequestMapping(method = RequestMethod.GET)
  public String list(Model model) {
    List<Pizza> pizzas = pizzaDao.findAll();
    model.addAttribute("pizzas", pizzas);
    return "index";
  }
 }




servlet-context.xml

    <?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:beans="http://www.springframework.org/schema/beans"
    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.2.xsd
    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.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!-- Enable @Controller annotation support -->
    <mvc:annotation-driven />

    <!-- Map simple view name such as "test" into /WEB-INF/test.jsp -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="pizzaDao" class="com.kaveen.dao.PizzaDao">
    </bean>

    <!-- Scan classpath for annotations (eg: @Service, @Repository etc) -->
    <context:component-scan base-package="com.kaveen.controller" />
    <!-- <context:component-scan base-package="com.kaveen.dao" />
    <context:component-scan base-package="com.kaveen.model" /> -->

    <!-- JDBC Data Source. It is assumed you have MySQL running on localhost 
        port 3306 with username root and blank password. Change below if it's not 
        the case -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/pizzashop" />
        <property name="username" value="root" />
        <property name="password" value="kaveen" />
        <property name="validationQuery" value="SELECT 1" />
    </bean>

    <!-- Hibernate Session Factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <array>
                <value>com.kaveen.dao</value>
            </array>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
            </value>
        </property>
    </bean>

    <!-- Hibernate Transaction Manager -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Activates annotation based transaction management -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

web.xml

    <?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">

    <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/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>
        <url-pattern>/index</url-pattern>

    </servlet-mapping>

  </web-app>

0 个答案:

没有答案