persistence.xml<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!--事务类型:local的还是global(JTA)的事务 -->
<persistence-unit name="jpa_test" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 自动装配注解Bean后置处理器 -->
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <!-- JPA注解Bean后置处理器 -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <!-- 利用Spring的实体管理器工厂来创建JPA实体管理器 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="/META-INF/persistence.xml"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="showSql" value="true" />
<!-- <property name="generateDdl" value="true" /> -->
</bean>
</property>
</bean> <bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="driverUrl">
<value>jdbc:oracle:thin:@192.168.99.211:1521:oradb1</value>
</property>
<property name="user"><!-- 必须在这里也设置,但是 proxool却不使用它,或许是个bug-->
<value>user_app</value>
</property>
<property name="password"><!-- 必须在这里也设置,但是 proxool却不使用它,或许是个bug-->
<value>app!@#</value>
</property>
<property name="alias">
<value>oradb1</value>
</property>
<property name="prototypeCount">
<value>5</value>
</property>
<property name="maximumConnectionCount">
<value>100</value>
</property>
<property name="minimumConnectionCount">
<value>10</value>
</property>
<property name="trace">
<value>true</value>
</property>
<property name="verbose">
<value>true</value>
</property>
</bean> <!-- 声明一个Spring提供的JPA事务管理器,传入的参数是Spring中的实体管理器工厂 -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean> <!-- 开启Spring提供的基于注解的声明式事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- 直接使用Spring的 JpaTemplate -->
<bean id="jpaTemplate"
class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<bean id="test" class="cn.digifun.validate.pojo.Test">
<property name="jpaTemplate" ref="jpaTemplate"/>
</bean>
</beans>web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置字符编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>User.javapackage cn.digifun.validate.pojo;import java.io.Serializable;
import java.util.Date;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity(name="t_portal_user")
@Table(name="t_portal_user")
public class User implements Serializable{
private int userID;
private String userName;
private String password;
private Date createTime;
private String realName;
private String gender;
private String birthDay;
private String mobile;
private String email;
private String bindMac;
private Date lastLoginTime;
private String lastLoginIP;
private int statusId;
@Id
@Column(name="userid")
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name="s_portal_user")
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
@Column(name="username")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name="password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="createtime")
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Column(name="realname")
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
@Column(name="gender")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Column(name="birthday")
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
@Column(name="mobile")
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Column(name="email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="bindmac")
public String getBindMac() {
return bindMac;
}
public void setBindMac(String bindMac) {
this.bindMac = bindMac;
}
@Column(name="lastlogintime")
public Date getLastLoginTime() {
return lastLoginTime;
}
public void setLastLoginTime(Date lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}
@Column(name="lastloginip")
public String getLastLoginIP() {
return lastLoginIP;
}
public void setLastLoginIP(String lastLoginIP) {
this.lastLoginIP = lastLoginIP;
}
@Column(name="statusid")
public int getStatusId() {
return statusId;
}
public void setStatusId(int statusId) {
this.statusId = statusId;
}
}
package cn.digifun.validate.pojo;
import org.springframework.orm.jpa.JpaTemplate;
public class Test {
private JpaTemplate jpaTemplate; public void test(){
User user = jpaTemplate.find(User.class, 1);
System.out.println(user.getUserID());
}
public void setJpaTemplate(JpaTemplate jpaTemplate) {
this.jpaTemplate = jpaTemplate;
}
}index.jsp<%@ page language="java" import="java.util.*,cn.digifun.validate.pojo.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(application);
Test test=(Test)ac.getBean("test");
test.test();
%>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!--事务类型:local的还是global(JTA)的事务 -->
<persistence-unit name="jpa_test" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!-- 自动装配注解Bean后置处理器 -->
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <!-- JPA注解Bean后置处理器 -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <!-- 利用Spring的实体管理器工厂来创建JPA实体管理器 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="/META-INF/persistence.xml"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
<property name="jpaVendorAdapter">
<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="showSql" value="true" />
<!-- <property name="generateDdl" value="true" /> -->
</bean>
</property>
</bean> <bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="driverUrl">
<value>jdbc:oracle:thin:@192.168.99.211:1521:oradb1</value>
</property>
<property name="user"><!-- 必须在这里也设置,但是 proxool却不使用它,或许是个bug-->
<value>user_app</value>
</property>
<property name="password"><!-- 必须在这里也设置,但是 proxool却不使用它,或许是个bug-->
<value>app!@#</value>
</property>
<property name="alias">
<value>oradb1</value>
</property>
<property name="prototypeCount">
<value>5</value>
</property>
<property name="maximumConnectionCount">
<value>100</value>
</property>
<property name="minimumConnectionCount">
<value>10</value>
</property>
<property name="trace">
<value>true</value>
</property>
<property name="verbose">
<value>true</value>
</property>
</bean> <!-- 声明一个Spring提供的JPA事务管理器,传入的参数是Spring中的实体管理器工厂 -->
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean> <!-- 开启Spring提供的基于注解的声明式事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" /> <!-- 直接使用Spring的 JpaTemplate -->
<bean id="jpaTemplate"
class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean>
<bean id="test" class="cn.digifun.validate.pojo.Test">
<property name="jpaTemplate" ref="jpaTemplate"/>
</bean>
</beans>web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置字符编码过滤器 -->
<filter>
<filter-name>encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>User.javapackage cn.digifun.validate.pojo;import java.io.Serializable;
import java.util.Date;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity(name="t_portal_user")
@Table(name="t_portal_user")
public class User implements Serializable{
private int userID;
private String userName;
private String password;
private Date createTime;
private String realName;
private String gender;
private String birthDay;
private String mobile;
private String email;
private String bindMac;
private Date lastLoginTime;
private String lastLoginIP;
private int statusId;
@Id
@Column(name="userid")
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name="s_portal_user")
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
@Column(name="username")
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name="password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name="createtime")
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
@Column(name="realname")
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
@Column(name="gender")
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Column(name="birthday")
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
@Column(name="mobile")
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Column(name="email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Column(name="bindmac")
public String getBindMac() {
return bindMac;
}
public void setBindMac(String bindMac) {
this.bindMac = bindMac;
}
@Column(name="lastlogintime")
public Date getLastLoginTime() {
return lastLoginTime;
}
public void setLastLoginTime(Date lastLoginTime) {
this.lastLoginTime = lastLoginTime;
}
@Column(name="lastloginip")
public String getLastLoginIP() {
return lastLoginIP;
}
public void setLastLoginIP(String lastLoginIP) {
this.lastLoginIP = lastLoginIP;
}
@Column(name="statusid")
public int getStatusId() {
return statusId;
}
public void setStatusId(int statusId) {
this.statusId = statusId;
}
}
package cn.digifun.validate.pojo;
import org.springframework.orm.jpa.JpaTemplate;
public class Test {
private JpaTemplate jpaTemplate; public void test(){
User user = jpaTemplate.find(User.class, 1);
System.out.println(user.getUserID());
}
public void setJpaTemplate(JpaTemplate jpaTemplate) {
this.jpaTemplate = jpaTemplate;
}
}index.jsp<%@ page language="java" import="java.util.*,cn.digifun.validate.pojo.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(application);
Test test=(Test)ac.getBean("test");
test.test();
%>
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause org.springframework.dao.InvalidDataAccessApiUsageException: Unknown entity: cn.digifun.validate.pojo.User; nested exception is java.lang.IllegalArgumentException: Unknown entity: cn.digifun.validate.pojo.User
org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:271)
org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:97)
org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:212)
org.springframework.orm.jpa.JpaAccessor.translateIfNecessary(JpaAccessor.java:152)
org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:189)
org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:231)
cn.digifun.validate.pojo.Test.test(Test.java:10)
org.apache.jsp.index_jsp._jspService(index_jsp.java:52)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause java.lang.IllegalArgumentException: Unknown entity: cn.digifun.validate.pojo.User
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:193)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:358)
$Proxy5.find(Unknown Source)
org.springframework.orm.jpa.JpaTemplate$1.doInJpa(JpaTemplate.java:233)
org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:184)
org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:231)
cn.digifun.validate.pojo.Test.test(Test.java:10)
org.apache.jsp.index_jsp._jspService(index_jsp.java:52)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause org.hibernate.MappingException: Unknown entity: cn.digifun.validate.pojo.User
org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:91)
org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)
org.hibernate.impl.SessionImpl.get(SessionImpl.java:835)
org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:182)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:358)
$Proxy5.find(Unknown Source)
org.springframework.orm.jpa.JpaTemplate$1.doInJpa(JpaTemplate.java:233)
org.springframework.orm.jpa.JpaTemplate.execute(JpaTemplate.java:184)
org.springframework.orm.jpa.JpaTemplate.find(JpaTemplate.java:231)
cn.digifun.validate.pojo.Test.test(Test.java:10)
org.apache.jsp.index_jsp._jspService(index_jsp.java:52)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:88)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)然后我访问下index.jsp的时候,拉出一堆错了。在网上找了好半天都是扯淡。。哪位大虾帮着看看啊。。小弟谢谢了
@@@@@此处添加使用的entity实体@@@@@@
</persistence-unit>
<provider>
org.eclipse.persistence.jpa.PersistenceProvider
</provider>
<class>cn.digifun.validate.pojo.User</class>
</persistence-unit> provider是追加的,不知道适用于你的工程不试试看你加了之后报的什么错?
错误很清楚了,实体映射没配
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="entityPU" transaction-type="RESOURCE_LOCAL">
<provider>
<!-- 这个根据你直接的情况自己定 -->
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider> <class>net.xxx.entity.User</class>
</persistence-unit></persistence>
这个name改成你自己的
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!--事务类型:local的还是global(JTA)的事务 -->
<persistence-unit name="jpa_test" transaction-type="RESOURCE_LOCAL">
<provider>
org.eclipse.persistence.jpa.PersistenceProvider
</provider>
<class>cn.digifun.validate.pojo.User</class>
</persistence-unit>
</persistence>
你们所得配成这样了,但服务器启动的时候又报下面这个错
Caused by: java.lang.IllegalStateException: JPA PersistenceProvider returned null EntityManagerFactory - check your JPA provider setup!
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<!-- 持久化单元名称配置 -->
<persistence-unit name="kugou" transaction-type="RESOURCE_LOCAL">
<!-- 指定用的是hibernate的驱动 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/kugou?useUnicode=true&characterEncoding=UTF-8" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.jdbc.fetch_size" value="18"/>
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.format_sql" value="false"/>
</properties>
</persistence-unit></persistence>
applicationContext.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 打开自动扫描 -->
<context:component-scan base-package="cn.kugou" />
<!-- 集成JPA -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="kugou"></property>
</bean>
<!-- 集成Spring事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 声明利用注解来配置事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
这样多简洁
也不用在persistence.xml配置要entity实体,自动管理了。
最后我改成 <persistence-unit name="oradb1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>cn.digifun.validate.pojo.User</class>
</persistence-unit>就通过了。。
就是 在 dao.getlistall(); 时 包 空指针一场 这是为什么