无法从数据库使用休眠获取所有数据

无法从数据库使用休眠获取所有数据

问题描述:

我正在尝试学习Hibernate和Spring。我想要做的第一件事就是使用Hibernate从数据库中获取一些数据。无法从数据库使用休眠获取所有数据

我想要做的是从数据库中获取所有数据,但我得到空指针异常。

这是我的代码;

City.java(com.hopyar.dao包下)

@Entity 
@Table(name = "Cities") 
public class City implements Serializable{ 

    private static final long serialVersionUID = 2637311781100429929L; 

    @Id 
    @GeneratedValue 
    @Column(name = "c_id") 
    int id; 

    @Column(name = "c_name") 
    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; 
    } 
} 

CityService.java(下com.hopyar.service封装)

@Service 
public class CityService { 

    @PersistenceContext 
    private EntityManager em; 

    @Transactional 
    public List<City> getAllCities(){ 
     List<City> result = em.createQuery("Select c From Cities c", City.class) 
       .getResultList(); // This is where I get the exeption. 
     System.out.println(); 
     return result; 
    } 
} 

弹簧context.xml中(下的web应用/ WEB-INF)

<?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.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 annotation-based Spring MVC controllers (eg: @Controller annotation) --> 
    <mvc:annotation-driven/> 

    <!-- Classpath scanning of @Component, @Service, etc annotated class --> 
    <context:component-scan base-package="com.hopyar" /> 

    <!-- Resolve view name into jsp file located on /WEB-INF --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/" /> 
    <property name="suffix" value=".jsp" /> 
    </bean> 

    <!-- MySQL Datasource with Commons DBCP connection pooling --> 
    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
    <property name="url" value="jdbc:mysql://localhost:3306/myproject"/> 
    <property name="username" value="username"/> 
    <property name="password" value="password"/> 
    </bean> 

    <!-- EntityManagerFactory --> 
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> 
    <property name="persistenceUnitName" value="persistenceUnit"/> 
    <property name="dataSource" ref="dataSource"/> 
    </bean> 

    <!-- Transaction Manager --> 
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean> 

    <!-- Enable @Transactional annotation --> 
    <tx:annotation-driven/> 

</beans> 

Main.java(com.hopyar.test包下)

public class Main { 

    public static void main(String[] args) { 
     CityService service = new CityService(); 

     List<City> cities = service.getAllCities(); 

     System.out.println(cities.size()); 

    } 

} 
+0

堆栈跟踪粘贴请螺纹 – Pracede 2014-12-04 13:19:38

+0

异常 “主” 显示java.lang.NullPointerException \t在com.hopyar.service.CityService.getAllCities(CityService.java:21)在com.hopyar.test.Main.main \t (Main.java:17) 只是这个,但没有别的 – xxlali 2014-12-04 13:21:21

+0

尝试自动装配而不是PersistenceContext – Pracede 2014-12-04 13:34:14

您正在实例化servicenew CityService(),这是错误的,因为您绕过了Spring。这意味着您的注释不会被处理,并且em为空。你需要从春天的环境中得到你的service

CityService service = applicationContext.getBean("cityService"); 
+0

或者使用一些Spring测试功能创建JUnitTest。这会为你提供一切。 – 2014-12-04 13:25:37

+0

我想我明白了。感谢帮助。 – xxlali 2014-12-04 13:29:07

+0

@xxlali没问题,很高兴我能帮到你。 – 2014-12-04 13:31:34

您可以尝试在CityService上使用autowired注释,并且Spring将实例化它。