简体   繁体   English

Spring的MVC / JPA。 从数据库中提取数据后没有结果

[英]Spring-mvc/jpa. No result after data extraction from database

I'm new to web development. 我是网络开发的新手。 I'm trying to create very simple application using spring-mvc, jpa/hibrenate, oracle db and tomcat v7.0 server. 我正在尝试使用spring-mvc,jpa / hibrenate,oracle db和tomcat v7.0服务器创建非常简单的应用程序。 App will just pull some data and display on page. 应用程序将只提取一些数据并显示在页面上。 The problem is when i'm trying to pull data there is no result on page. 问题是当我试图提取数据时,页面上没有结果。 I have no clue what i'm doing wrong. 我不知道我做错了什么。

Entity class. 实体类。

@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 file with config 带配置的.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>

persistence.xml persistence.xml中

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="persistence-webstore"
    transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.dialect"value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        <property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver" />
        <property name="hibernate.connection.username" value="crossing" />
        <property name="hibernate.connection.password" value="123" />
        <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
    </properties>
</persistence-unit>
</persistence>

Service class 服务类

@Service
public class ProductService {
  @PersistenceContext
  private EntityManager em;

  @Transactional
  public List<Product> getAll() {
    List<Product> result = new ArrayList<Product>();
    result = em.createQuery("SELECT e FROM Product e", Product.class)
            .getResultList();

    return result;

  }
}

My .jsp file 我的.jsp文件

//....
<c:forEach items="${products}" var="product">
            <div class="col-sm-6 col-md-3" style="padding-bottom: 15px">
                <div class="thumbnail">
                    <div class="caption">                           
                            <h3>${product.name}</h3>
                            <p>${product.description}</p>
                            <p>${product.unitPrice}PLN</p>
                            <p>Units in stock: ${product.unitsInStock}</p>
                    </div>
                </div>
            </div>
</c:forEach>
//..

And controller 和控制器

@Controller
public class ProductController {
  @Autowired
  public ProductService proService;

  @RequestMapping("/")
  public String list(Model model) {

    model.addAttribute("products", proService.findAll());

    return "products";
  }

}

** EDIT ** ** 编辑 **

I've made some changes 我做了一些改变

Added ProductRepository 添加了ProductRepository

public interface ProductRepository extends Repository<Product, String>{

   public List<Product> findAll();  

}

Changed Service class 更改了服务类

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

No NPE this time or any other error, just still i can't get those elements on list. 这次没有NPE或任何其他错误,我仍然无法将这些元素列入清单。 There must be something with my query or configuration, I dunno. 我的查询或配置必须有一些东西,我不知道。

Edit 编辑
Hibernate show_sql result is down below, but why is it looks in that way? Hibernate show_sql结果在下面,但为什么它看起来那样? Is this supposed to be? 这应该是?

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_

** EDIT ** ** 编辑 **

Finally works. 最后工作。 To my surprise just simple update maven project helped. 令我惊讶的是,简单的更新maven项目得到了帮助。

ravius, ravius,

I think you need to add repository class. 我认为你需要添加存储库类。

repository 知识库

public interface DocumentRepository extends PagingAndSortingRepository<Document, Integer> {

}

service - > daoImpl 服务 - > daoImpl

@Override
public List<Document> findAllDocument() {
    return documentRepository.findAll();
}

.xml .XML

<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>

You trying to work. 你想工作。

If not work. 如果不行。

I need you error message. 我需要你的错误信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM