Spring Eclipselink持久性问题(无插入)

时间:2013-07-11 16:38:08

标签: spring-mvc persistence eclipselink

我使用Spring 3.1和Eclipselink 2与Tomcat,但由于某种原因数据不会持久.. 该表正在生成。

调度-servlet.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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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
   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.myapp.user.controllers" />
<mvc:annotation-driven />

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

</beans>

的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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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 
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<aop:aspectj-autoproxy />

<context:load-time-weaver aspectj-weaving="on"/>


<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="loadTimeWeaver" class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />

<bean id="jpaVendorAdapter"
      class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
    <property name="database" value="MYSQL"/>
    <property name="showSql" value="true"/>
    <property name="generateDdl" value="false"/>
    <property name="databasePlatform" value="org.eclipse.persistence.platform.database.MySQLPlatform"/>
</bean>

<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
      p:persistenceUnitName="myAppPU"
      p:persistenceXmlLocation="classpath*:META-INF/persistence.xml"
      p:jpaVendorAdapter-ref="jpaVendorAdapter"
      p:loadTimeWeaver-ref="loadTimeWeaver"
      p:dataSource-ref="dataSource"
/>

<bean id="txManager"
      class="org.springframework.orm.jpa.JpaTransactionManager"
      p:entityManagerFactory-ref="entityManagerFactory"
      p:dataSource-ref="dataSource"
/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/qpdb?zeroDateTimeBehavior=convertToNull"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
</bean>

<tx:annotation-driven transaction-manager="txManager" />

</beans>

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
  <persistence-unit name="myAppPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.myapp.entities.GenericUser</class>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/qpdb?zeroDateTimeBehavior=convertToNull"/>
  <property name="javax.persistence.jdbc.password" value=""/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <!--<property name="eclipselink.weaving" value="static"/>-->
</properties>
</persistence-unit>
</persistence>

WelcomeController.java

package com.myapp.user.controllers;
import com.myapp.entities.GenericUser;
import java.math.BigInteger;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

@Transactional(propagation=Propagation.SUPPORTS)
@Controller
public class WelcomeController {

@PersistenceContext
private EntityManager entityManager;

public WelcomeController() {
}

@RequestMapping({"/"})
public String showPublicPage(Map<String, Object> model) {
    GenericUser gu = new GenericUser();
    addUser(gu);
    return "welcome";
}

@Transactional(propagation= Propagation.REQUIRED,readOnly=false)
public void addUser(GenericUser gu) {
    gu.setUserID(BigInteger.ONE);
    gu.setEmail("asdfadf");
    entityManager.persist(gu);
}
}

GenericUser.java

package com.myapp.entities;

import java.io.Serializable;
import java.math.BigInteger;
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.Table;
import javax.persistence.Temporal;

@Entity
@Table(name="generic_user")
public class GenericUser implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID")
private BigInteger userID;
@Column(name = "USERNAME")
private String username;
@Column(name = "EMAIL")
private String email;
@Column(name = "PASSWORD")
private String password;

public BigInteger getUserID() {
    return userID;
}

public void setUserID(BigInteger id) {
    this.userID = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (userID != null ? userID.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof GenericUser)) {
        return false;
    }
    GenericUser other = (GenericUser) object;
    if ((this.userID == null && other.userID != null) || (this.userID != null && !this.userID.equals(other.userID))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.myapp.entities.GenericUser[ id=" + userID + " ]";
}
}

我用-javaagent启动tomcat:path / to / spring-agent-2.5.6.SEC03.jar

Tomcat Log

  

2013年7月11日9:30:36 org.apache.catalina.core.ApplicationContext log

     

信息:销毁Spring FrameworkServlet&#39;调度程序&#39;

     

2013年7月11日9:30:36 org.apache.catalina.core.ApplicationContext log

     

INFO:关闭Spring root WebApplicationContext

     

2013年7月11日下午9:33:13 org.apache.catalina.core.ApplicationContext log

     

信息:没有在类路径上检测到Spring WebApplicationInitializer类型

     

2013年7月11日下午9:33:13 org.apache.catalina.core.ApplicationContext log

     

INFO:初始化Spring root WebApplicationContext

     

2013年7月11日下午9:33:13 org.apache.catalina.core.StandardContext listenerStart

     

SEVERE:将上下文初始化事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例的异常

     

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自ServletContext资源[/WEB-INF/applicationContext.xml]的XML文档中的第52行无效;嵌套异常是org.xml.sax.SAXParseException; lineNumber:52; columnNumber:6;处理指令目标匹配&#34; [xX] [mM] [lL]&#34;是不允许的。

     

at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)

     

at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)

     

at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)

     

at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)

     

at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)

     

at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)

     

at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)

     

at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)

     

at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:131)

     

at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:522)

     

at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:436)

     

at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385)

     

at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284)

     

at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)

     

at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4779)

     

at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5273)

     

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

     

at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)

     

at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)

     

at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)

     

at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:649)

     

at org.apache.catalina.startup.HostConfig $ DeployDescriptor.run(HostConfig.java:1585)

     

at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471)

     

at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:334)

     

at java.util.concurrent.FutureTask.run(FutureTask.java:166)

     

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)

     

at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)

     

在java.lang.Thread.run(Thread.java:722)

     

引起:org.xml.sax.SAXParseException; lineNumber:52; columnNumber:6;处理指令目标匹配&#34; [xX] [mM] [lL]&#34;是不允许的。

     

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)

     

at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)

     

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:441)

     

at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)

     

at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1388)

     

at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanPIData(XMLScanner.java:675)

     

at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanPIData(XMLDocumentFragmentScannerImpl.java:980)

     

at com.sun.org.apache.xerces.internal.impl.XMLScanner.scanPI(XMLScanner.java:643)

     

at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl $ PrologDriver.next(XMLDocumentScannerImpl.java:913)

     

at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)

     

at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)

     

at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)

     

at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)

     

at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)

     

at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)

     

at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:237)

     

at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:300)

     

at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)

     

at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)

     

... 27更多

     

2013年7月11日下午9:33:13 org.apache.catalina.core.ApplicationContext log

     

INFO:关闭Spring root WebApplicationContext

     

2013年7月11日下午9:33:13 org.apache.catalina.core.StandardContext listenerStop

     

SEVERE:异常将上下文销毁事件发送到类org.springframework.web.context.ContextLoaderListener的侦听器实例

     

java.lang.IllegalStateException:BeanFactory未初始化或已经关闭 - 调用&#39;刷新&#39;在通过ApplicationContext访问bean之前

     

at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)

     

at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1066)

     

at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1040)

     

at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:988)

     

at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:556)

     

at org.springframework.web.context.ContextLoaderListener.contextDestroyed(ContextLoaderListener.java:142)

     

at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4819)

     

at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5466)

     

at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)

     

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)

     

at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)

     

at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:871)

     

at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)

     

at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:649)

     

at org.apache.catalina.startup.HostConfig $ DeployDescriptor.run(HostConfig.java:1585)

     

at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:471)

     

at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:334)

     

at java.util.concurrent.FutureTask.run(FutureTask.java:166)

     

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)

     

at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)

     

在java.lang.Thread.run(Thread.java:722)

     

2013年7月11日下午9:33:15 org.apache.catalina.core.ApplicationContext log

     

信息:没有在类路径上检测到Spring WebApplicationInitializer类型

     

2013年7月11日下午9:33:15 org.apache.catalina.core.ApplicationContext log

     

INFO:初始化Spring root WebApplicationContext

     

2013年7月11日下午9:33:18 org.apache.catalina.core.ApplicationContext log

     

信息:初始化Spring FrameworkServlet&#39;调度程序&#39;

     

2013年7月11日下午9:34:32 org.apache.catalina.core.ApplicationContext log

     

信息:销毁Spring FrameworkServlet&#39;调度程序&#39;

     

2013年7月11日下午9:34:32 org.apache.catalina.core.ApplicationContext log

     

INFO:关闭Spring root WebApplicationContext

     

2013年7月11日下午9:34:34 org.apache.catalina.core.ApplicationContext log

     

信息:没有在类路径上检测到Spring WebApplicationInitializer类型

     

2013年7月11日下午9:34:34 org.apache.catalina.core.ApplicationContext log

     

INFO:初始化Spring root WebApplicationContext

     

2013年7月11日下午9:34:37 org.apache.catalina.core.ApplicationContext log

     

信息:初始化Spring FrameworkServlet&#39;调度程序&#39;

该应用程序成功运行并打开“欢迎”#39;查看,但数据(GenericUser gu)不会持续......

3 个答案:

答案 0 :(得分:1)

可能p:persistenceXmlLocation="classpath*:META-INF/persistence.xml"这是错误的.. 尝试将xml放在WEB-INF /文件夹中,并将其链接为:persistenceXmlLocation =“myxml.xml”

答案 1 :(得分:1)

您可以检查依赖关系罐是否在正确的位置可用吗?

答案 2 :(得分:0)

终于解决了......发布我的解决方案以供将来参考..

在到达工作解决方案之前我改变了很多东西..我不记得到底是什么问题..所以我提到了我所做的所有改变。

  1. 将所有xsd版本更改为3.1 ..(可能没有关系)

  2. 如果您使用tomcat,请使用spring-tomcat-weaver.jar进行加载时间编织,如上所述here

  3. 主要问题:

  4. Spring文档说明了以下内容:

      

    <tx:annotation-driven/>仅在其定义的相同应用程序上下文中查找bean上的@Transactional。这意味着,如果您为DispatcherServlet放入WebApplicationContext,它只检查控制器中的@Transactional bean,而不是你的服务。有关更多信息,请参见第15.2节“DispatcherServlet”。

    由于在<tx:annotation-driven>中声明了applicationContext.xml,因此必须在应用程序上下文中发现所有想要使用@Transactional的类。因此,您还需要在应用程序上下文中指定<context:component-scan>。但请确保您没有重新发现dispatcher-servlet.xml中已指定的控制器。您可以使用include-filter元素中的exclude-filter<context:component-scan>元素来实现此目的。 详细解释here

    您将无法在控制器中使用@Transactional ..(您不应该使用IMO)