0

I'm trying to learn hibernate and spring. The first thing I want to do is get some data from database using Hibernate.

What I am trying to do is getting all data from data base but I get null pointer exception.

Here is my code;

City.java (under com.hopyar.dao package)

@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 (under com.hopyar.service package)

@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;
    }
}

spring-context.xml(under webapp/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(under com.hopyar.test package)

public class Main {

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

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

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

    }

}
4
  • Paste the stacktrace please Commented Dec 4, 2014 at 13:19
  • Exception in thread "main" java.lang.NullPointerException at com.hopyar.service.CityService.getAllCities(CityService.java:21) at com.hopyar.test.Main.main(Main.java:17) just this but nothing else Commented Dec 4, 2014 at 13:21
  • Try Autowired instead of PersistenceContext Commented Dec 4, 2014 at 13:34
  • It does not work. Still gives the same exception. Commented Dec 4, 2014 at 13:38

2 Answers 2

3

You are instantiating service as new CityService(), which is wrong because you are bypassing Spring. This means your annotations are not proccessed, and em is null. You need to get your service from spring context.

CityService service = applicationContext.getBean("cityService");
Sign up to request clarification or add additional context in comments.

2 Comments

Or create a JUnitTest using some of the Spring test functionality. This would wire everything up for you.
I guess I got the idea. Thanks for help.
1

You can try to use autowired annotation on CityService, and Spring will instantiate it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.