【发布时间】:2016-09-05 16:19:32
【问题描述】:
我是网络开发的新手。我正在尝试使用 spring-mvc、jpa/hibrenate、oracle db 和 tomcat v7.0 服务器创建非常简单的应用程序。应用程序只会提取一些数据并显示在页面上。问题是当我尝试提取数据时,页面上没有结果。我不知道我做错了什么。
实体类。
@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>
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
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;
}
}
我的 .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>
//..
和控制器
@Controller
public class ProductController {
@Autowired
public ProductService proService;
@RequestMapping("/")
public String list(Model model) {
model.addAttribute("products", proService.findAll());
return "products";
}
}
** 编辑 **
我做了一些改动
添加了 ProductRepository
public interface ProductRepository extends Repository<Product, String>{
public List<Product> findAll();
}
更改的服务类
@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;
}
这次没有 NPE 或任何其他错误,只是我仍然无法在列表中获取这些元素。我的查询或配置一定有问题,我不知道。
编辑
Hibernate show_sql 结果在下面,但为什么看起来是这样?应该是这样吗?
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_
** 编辑 **
终于成功了。令我惊讶的是,只是简单的更新 maven 项目有所帮助。
【问题讨论】:
-
如果您至少尝试过调试,您可能会有所收获?例如,在 JPA 查询调用之后,打印出返回的数据。如果没有,则查看该查询调用的 SQL 并检查它是否正确。如果它是正确的,那么你检查你在数据库中的数据......
-
当我尝试打印数据时,会出现 NullPointerExeption。我不知道查询或我的代码是否有问题,但是当我尝试使用简单的 jdbc oracle 驱动程序在没有持久性的情况下提取数据时,一切正常。
-
你得到一个 NPE,那么什么是 null?为什么它是空的?还是叫调试
-
尝试使用hibernate配置中的show_sql,你就会知道数据库是否被查询。同样对于选择查询,您可能不需要事务
标签: java spring-mvc jpa