在下拉菜单中尝试使用hibernate从数据库获取数据时,Spring-mvc错误505

时间:2015-03-05 17:40:32

标签: spring hibernate jsp spring-mvc

我在尝试启动应用程序时遇到了困难,我被困在代码中的某个地方。

我的实体类是:

@Entity
@Table(name="metalocation")
public class Location implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="ID")
private int id;
@Column(name="DB_ID")
private String dbId;
@Column(name="GEO_LAT")
private double geoLAT;
@Column(name="GEO_LNG")
private double geoLNG;
@Column(name="IN_LOCATION")
private int inLocation;
@Column(name="ISO")
private String iso;
@Column(name="LOCAL_NAME")
private String localName;
@Column(name="TYPE")
private char type;

@OneToMany(cascade=CascadeType.PERSIST)
@JoinColumn(name="meta_address_fk")
private Set<Address> address;

public Location() {

}
.......... All setters and getters

我的服务界面是:

import java.util.List;

import com.platformhouse.clinicsystem.web.model.entity.Address;
import com.platformhouse.clinicsystem.web.model.entity.Location;

public interface AddressService {


public List<Location> getAllCountries();
}

我的服务类是:

 @Service("adressService")
   @Transactional
   public class AddressServiceImplementation implements AddressService {

    private AddressDAO addressDAO;


   //
    public void setPatientDao(AddressDAO addressDAO) {
        this.addressDAO = addressDAO;
    }

@Transactional
public List<Location> getAllCountries() {

    return this.addressDAO.getAllCountries();
 }
 }

我的DAO是:

@Repository
@Transactional
public class AddressDAO {


@Autowired
private SessionFactory session;

public void setSession(SessionFactory session) {
    this.session = session;
}

public Session session(){
    return session.getCurrentSession();
}

@SuppressWarnings("unchecked")
public List<Location> getAllCountries() {
    Query query =   session().createQuery("from Location where inLocation <=246");
      List<Location> results = query.list();
      return results;
     }  
  }

我的控制器是:

@Controller
@RequestMapping(value = "/usersignup")
public class PatientSignupController {

private PatientService patientService;
private AddressService addressService;


@RequestMapping(value = "/usersignup")
public String signup() {
    return "usersignup";
}


@RequestMapping( method = RequestMethod.GET)
public List<Location> getCountries() {

    return this.addressService.getAllCountries();

}
}

我的hibernate xml:

<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/mydb" />
      <property name="username" value="username" />
      <property name="password" value="password" />
</bean>

    <context:component-scan base-package="com.platformhouse.clinicsystem.web.dao">
</context:component-scan>


<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate4AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
          <list>

<value>com.platformhouse.clinicsystem.web.model.entity.User</value>
            <value>com.platformhouse.clinicsystem.web.model.entity.UserType</value>
            </value>
            <value>com.platformhouse.clinicsystem.web.model.entity.Location
            </value>
            <
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="javax.persistence.validation.group.pre-persist">com.platformhouse.clinicsystem.web.validation.PersistenceValidationGroup
                </prop>
                <prop key="javax.persistence.validation.group.pre-update">com.platformhouse.clinicsystem.web.validation.PersistenceValidationGroup
                </prop>
                <prop key="javax.persistence.validation.group.pre-remove">com.platformhouse.clinicsystem.web.validation.PersistenceValidationGroup
                </prop>
        </props>
    </property>

        <property name="packagesToScan">
            <list>
                <value>com.platformhouse.clinicsystem.web.dao</value>
                <value>com.platformhouse.clinicsystem.web.service.implementation</value>
            </list>
        </property>
</bean>


<tx:annotation-driven transaction-manager="transactionManager" />


    <bean id="exceptionTranslator"
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor">
    </bean>

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

我的JSp代码是:

<form method="Get">
        <label>Country/region:</label>

        <select id="iCountry" name="iCountry">
        <c:forEach items="${locationList}" var="country">
        <option selected="selected" value="">please select country</option>
                                            <option value="${country.id}">${country.localName}</option>
                                        </c:forEach>
                                        <select />

我的结果是:

      HTTP Status 500 - Request processing failed; nested exception is java.lang.NullPointerException


   **type Exception report
   message Request processing failed; nested exception is java.lang.NullPointerException
   description The server encountered an internal error that prevented it from fulfilling this request.
   exception
   org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
   root cause
     java.lang.NullPointerException


----------

//控制器错误

        com.platformhouse.clinicsystem.web.controller.PatientSignupController.getCountries(PatientSignupController.java:40)


----------


    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

1 个答案:

答案 0 :(得分:0)

您忘记为服务添加@Autowired注释。这就是为什么你得到NullPointerException。请查看以下

@Autowired
private PatientService patientService;
@Autowired
private AddressService addressService;

<强>更新

您已将其称为@Service("adressService")。 addressService拼写不正确。 将其更改为@Service("addressService")