Я пытаюсь изучить спящий режим и весну. Первое, что я хочу сделать, это получить некоторые данные из базы данных с помощью Hibernate.Невозможно получить все данные из базы данных с помощью Hibernate
То, что я пытаюсь сделать, это получить все данные из базы данных, но я получаю исключение из null-указателя.
Это мой код;
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-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());
}
}
Вставить StackTrace пожалуйста – Pracede
Exception в потоке "главный" java.lang.NullPointerException \t на com.hopyar.service.CityService.getAllCities (CityService.java:21) \t в com.hopyar.test.Main.main (Main.java:17) только это, но ничего больше – xxlali
Попробуйте Autowired вместо PersistenceContext – Pracede