Java Spring mvc/jpa。从数据库中提取数据后没有结果
我是网络开发新手。我正在尝试使用SpringMVC、jpa/hibrenate、OracleDB和TomcatV7.0服务器创建非常简单的应用程序。应用程序将只拉一些数据并显示在页面上。问题是当我试图提取数据时,页面上没有结果。我不知道我做错了什么 实体类。Java Spring mvc/jpa。从数据库中提取数据后没有结果,java,spring-mvc,jpa,Java,Spring Mvc,Jpa,我是网络开发新手。我正在尝试使用SpringMVC、jpa/hibrenate、OracleDB和TomcatV7.0服务器创建非常简单的应用程序。应用程序将只拉一些数据并显示在页面上。问题是当我试图提取数据时,页面上没有结果。我不知道我做错了什么 实体类。 @Entity @Table(name="Product") public class Product { @Id @Column(name="productId") private String productId; @Column(
@Entity
@Table(name="Product")
public class Product {
@Id
@Column(name="productId")
private String productId;
@Column(name="name")
private String name;
@Column(name="unitPrice")
private int unitPrice;
@Column(name="description")
private String description;
@Column(name="manufacturer")
private String manufacturer;
@Column(name="category")
private String category;
@Column(name="unitsInStock")
private int unitsInStock;
@Column(name="unitsInOrder")
private long unitsInOrder;
@Column(name="condition")
private String condition;
//constructors getters and setters
带有配置的.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.packt.webstore" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="URL" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="user" value="crossing" />
<property name="password" value="123" />
</bean>
<bean id="hibernateJpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="persistence-webstore" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
**编辑**
我做了一些改变
添加了ProductRepository
public interface ProductRepository extends Repository<Product, String>{
public List<Product> findAll();
}
**编辑**
终于起作用了。令我惊讶的是,只是简单的更新maven项目起了作用。ravius
我认为您需要添加存储库类
存储库
public interface DocumentRepository extends PagingAndSortingRepository<Document, Integer> {
}
public interface DocumentRepository扩展了分页和排序存储库{
}
服务->daoImpl
@Override
public List<Document> findAllDocument() {
return documentRepository.findAll();
}
@覆盖
公共列表findAllDocument(){
返回documentRepository.findAll();
}
.xml
org.hibernate.dialogue.mysqldialogue
4.
真的
真的
更新
你在努力工作
如果不工作
我需要你的错误信息 拉维乌斯
我认为您需要添加存储库类
存储库
public interface DocumentRepository extends PagingAndSortingRepository<Document, Integer> {
}
public interface DocumentRepository扩展了分页和排序存储库{
}
服务->daoImpl
@Override
public List<Document> findAllDocument() {
return documentRepository.findAll();
}
@覆盖
公共列表findAllDocument(){
返回documentRepository.findAll();
}
.xml
org.hibernate.dialogue.mysqldialogue
4.
真的
真的
更新
你在努力工作
如果不工作
我需要你的错误信息 也许如果你至少试着调试一下,你会有所收获?例如,在JPA查询调用之后,打印出返回的数据。如果没有,则查看该查询调用的SQL并检查它是否正确。如果它是正确的,那么检查数据库中的数据……当我试图打印数据时,有一个NullPointerExeption。我不知道查询或我的代码是否有问题,但当我尝试使用简单的jdbc oracle驱动程序在没有持久性的情况下提取数据时,一切都很顺利。您得到了一个NPE,那么什么是null?为什么它是空的?仍然使用hibernate配置中的show_sql调用debuggingTry,您将知道数据库是否被查询。此外,对于select查询,您可能不需要事务处理。如果您至少尝试过调试,您可能会有什么收获?例如,在JPA查询调用之后,打印出返回的数据。如果没有,则查看该查询调用的SQL并检查它是否正确。如果它是正确的,那么检查数据库中的数据……当我试图打印数据时,有一个NullPointerExeption。我不知道查询或我的代码是否有问题,但当我尝试使用简单的jdbc oracle驱动程序在没有持久性的情况下提取数据时,一切都很顺利。您得到了一个NPE,那么什么是null?为什么它是空的?仍然使用hibernate配置中的show_sql调用debuggingTry,您将知道数据库是否被查询。此外,对于select查询,您可能不需要事务处理
@Service
public class ProductService implements ProductRepository{
@PersistenceContext
private EntityManager em;
@Override
@Transactional
public List<Product> findAll() {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("persistence-webstore");
em = factory.createEntityManager();
List<Product> listPersons = em.createQuery("SELECT p FROM Product p",Product.class).getResultList();
if (listPersons.isEmpty()) {
System.out.println("List contains no data");
}
return listPersons;
}
Hibernate:
select
product0_.productId as productId1_0_,
product0_.category as category2_0_,
product0_.condition as condition3_0_,
product0_.description as description4_0_,
product0_.manufacturer as manufacturer5_0_,
product0_.name as name6_0_,
product0_.unitPrice as unitPrice7_0_,
product0_.unitsInOrder as unitsInOrder8_0_,
product0_.unitsInStock as unitsInStock9_0_
from
Product product0_
public interface DocumentRepository extends PagingAndSortingRepository<Document, Integer> {
}
@Override
public List<Document> findAllDocument() {
return documentRepository.findAll();
}
<jpa:repositories base-package="com.test.faas.repositoryex"
entity-manager-factory-ref="entityManagerFactory"></jpa:repositories>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.default_schema">test</prop> -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.pool_size">4</prop>
<prop key="hibernate.connection.shutdown">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> <prop key="hibernate.ddl_auto">auto</prop> -->
</props>
</property>
<!-- entity define class package -->
<property name="packagesToScan" value="com.text.faas.dtoex" />
</bean>