2

我在 SpringMVC 中将数据刷新到 PostgreSQL 数据库时遇到问题。当我使用 entityManager.flush() 时。

该表是在 PosgreSQL 中创建的,我可以使用 pgadmin3 并且它存在,但问题是在我将元素从控制器传递到 ImageDaoImpl 后,它没有将记录推送到数据库;/

@Repository

@Transactional(propagation = Propagation.REQUIRED)
public class ImageDaoImpl implements ImageDao {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void register(Image image) { 
        em.persist(image);
        em.flush(); 
        return;
    }

}

当我想做注册方法时,它给了我一个错误。 javax.persistence.TransactionRequiredException: no transaction is in progress

我试着做:

    @Transactional
public void register(Image image) {
    em.getTransaction().begin();
    em.persist(image);
    em.getTransaction().commit();
    return;
}

但它把我扔了:

threw exception: java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead

首先,我认为有问题并首先添加@PersistenceContext

我发现EntityManager 不能使用持久保存元素到数据库

并尝试添加

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

<bean id="transactionManager"
         class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

但后来我想怎么不可能附加经理:然后我收到这样的错误:

java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.JdbcTransactionObjectSupport

接下来做什么?我如何<jee:jndi-lookup jndi-name="java:jboss/spring-quickstart/persistence" id="entityManagerFactory" ...与 transactionManager 集成?它们之间有什么区别?我应该使用哪一个来连接数据库?

也许还有另一个我看不到的问题。

我已经设置了所有数据库连接,但是刷新问题让我发疯了。我没有尝试从浏览器中的实体读取信息,在此之前我想将信息推送到数据库。

我读了 Spring jta-transaction-manager

并试图评论该行

但它把我扔了:(MSC服务线程1-2)上下文初始化失败:java.lang.NoClassDefFoundError:org/springframework/orm/jpa/JpaTransactionManager$JpaTransactionObject

为什么找不到合适的解决方案: EntityManager persist() 方法不向数据库插入记录 我应该在哪里注册类 Image 以进行事务?

这是控制器的片段。

 @RequestMapping(value = "/save", method = RequestMethod.POST)
        public String save(
                @ModelAttribute("uploadForm") ImageDaoImpl uploadForm,
                        Model map, BindingResult result) throws IllegalStateException, IOException {
                           ...
                           String filePath = fileCatalog + orgName;                                        
                        Image memberImage = new Image();
                        memberImage.setFilePath(filePath);             
                        imageDao.register(memberImage);

                                 ...
                }
            }

我创建对象并在数据库中设置一个我需要的值。我得到的文件保存在硬盘中。我稍后需要它用于我的另一个程序。这是在应用程序中注册对象的好方法。也许这是它没有刷新到数据库的原因?

最后我附上我的配置文件:

图像域:

@Entity
@Table(name="IMAGES")
public class Image {

    @Id
    @Column(name="ID")
    @GeneratedValue
    private Integer id;

    @Column(name="NAME")
    private String name;

    @Column(name="CONTENTTYPE")
    private String contentType;

    @Column(name="LENGTH")
    private Integer length;

    @Column(name="ISPOCESSED")
    private boolean isprocessed;

    @Column(name="CONTENT")
    @Lob
    private Blob content;

    @Column(name="FILEPATH")
    private String filepath;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }

    public void setFilePath(String filepath) {
        this.filepath = filepath;
    }

    public String getFilePath(){
        return filepath;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getContentType() {
        return contentType;
    }
    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
    public Blob getContent() {
        return content;
    }
    public void setContent(Blob content) {
        this.content = content;
    }
    public Integer getLength() {
        return length;
    }
    public void setLength(Integer length) {
        this.length = length;
    }

}

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

    <context:component-scan base-package="org.springmvc.trophy.domain"/>

    <context:component-scan base-package="org.springmvc.trophy.repo"/>

    <tx:annotation-driven />

</beans>

META-INF/spring/infrastructure.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:jee="http://www.springframework.org/schema/jee"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx.xsd
               http://www.springframework.org/schema/jee 
               http://www.springframework.org/schema/jee/spring-jee.xsd">

    <!-- JDNI name for EntityManagerFactory is defined in src/main/resources/META-INF/persistence.xml -->


    <jee:jndi-lookup jndi-name="java:jboss/spring-quickstart/persistence" id="entityManagerFactory"
                     expected-type="javax.persistence.EntityManagerFactory" />

    <bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:jta-transaction-manager /> 

</beans>

META-INF/Persistance.xml

<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="primary" transaction-type="RESOURCE_LOCAL">
        <!-- If you are running in a production environment, add a managed data 
            source, this example data source is just for development and testing! -->
        <!-- The datasource is deployed as WEB-INF/spring-quickstart-ds.xml, you 
            can find it in the source at src/main/webapp/WEB-INF/spring-quickstart-ds.xml -->
         <jta-data-source>java:jboss/datasources/ImagesDS</jta-data-source>

        <properties>
           <property name="jboss.entity.manager.factory.jndi.name"
                value="java:jboss/spring-quickstart/persistence" />             
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />


        </properties>
</persistence-unit>

</persistence>

webapp/WEB-INF/images-ds.xml

<?xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.jboss.org/ironjacamar/schema http://docs.jboss.org/ironjacamar/schema/datasources_1_0.xsd">
    <!-- The datasource is bound into JNDI at this location. We reference this 
        in META-INF/persistence.xml -->
     <datasource jndi-name="java:jboss/datasources/ImagesDS"
        pool-name="kitchensink-quickstart" enabled="true" jta="false" use-java-context="true" use-ccm="false">
        <connection-url>jdbc:postgresql://localhost:5432/dermadb</connection-url>
        <driver-class>org.postgresql.Driver</driver-class>
        <driver>postgresql-9.1-901.jdbc4.jar</driver>
        <security>
            <user-name>username</user-name>
            <password>dbpassword</password>
        </security>
    </datasource> 
</datasources>

我为 PostgreSQL 部署了模块:在 webapp/WEB-INF/jboss-deployment-structure.xml

<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.0">
   <deployment>
       <dependencies>
            <module name="org.postgresql"/>
            <!-- <module name="com.h2database.h2"/> -->
            <module name="org.codehaus.jackson.jackson-core-asl"/>
            <module name="org.codehaus.jackson.jackson-mapper-asl"/>
            <module name="org.slf4j"/>
       </dependencies>
   </deployment>
</jboss-deployment-structure>

提前感谢您的回答。

4

2 回答 2

0

看起来像类路径问题。您是否尝试共享 Spring 库?如果没有,请验证您是否spring-orm在依赖项中。

于 2013-06-15T18:23:52.203 回答
0

我终于解决了这个问题。

首先,我认为事务管理器有问题,我将 aspectj 添加到项目中。

META-INF/spring/infrastructure.xml

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

我还将库添加到 Maven - 文件:POM.XML

 <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>2.5</version>
    </dependency>

最后我意识到我不需要使用提交和关闭事务等。正如你在我的第一篇文章中看到的那样。实体工厂和管理器被传递@PersistenceContext,我可以entityManager.persist(object)用来将对象传递到数据库和事务将结束。

我的错误是在persistence.xml中设置与数据库的连接时,我在这一行中使用了本地资源:

<persistence-unit name="primary" transaction-type="RESOURCE_LOCAL">

在下一步中,我使用 JTA 连接数据库。这就是整个问题。

我将该行更改为

<persistence-unit name="primary" transaction-type="JTA">

并且发生一切都很好并且对象被刷新到数据库中。

感谢 Pavel Horal 的帮助,我希望这个答案对其他人有帮助。你需要时刻注意细节。

于 2013-07-07T13:07:54.027 回答