6

我在尝试使用 Springs CrudRepository 接口将实体持久保存到 PostgreSQL 数据库时遇到问题。在 TomEE 上设置正确的值和调整 Spring 本身的配置时,我遇到了很多问题。我尝试简单地使用休眠,但问题变得更糟,所以我切换回与 TomEE 捆绑的 OpenJpa。

我对普通的 JavaEE 有一些经验,并且正在创建这个应用程序来学习 Spring,我的一个朋友需要一个 Spring 应用程序来在基于 TomEE 的 ApplicationServer 上进行部署,因此这两个是必需的。我附加的代码量可能有点过火,但我宁愿让你看看那里有什么,以便你得到一个很好的概述。

我开发的组件以及我正在做的、期望的和实际看到的内容的简要描述。组件:

  • CoworkerController(RESTful Spring 控制器)
  • CoworkerService(与 Controller 和 Repository 交互的 Spring Service)
  • CoworkerRepository(扩展 Spring CrudRepository)
  • 同事(应该被持久化的实体)

我的行动流程:

  • 通过 curl 将带有 firstname lastname 的 POST 发送到 RESTful 服务
  • 使用持久化实体的 ID 获取状态 200
  • 如果我可以查询我创建的同事,请检查 PostgreSQL 和 RESTful 服务

我所期待的:

  • Controller 接受 POST 并调用 Service 来持久化 Coworker
  • 服务告诉存储库保存同事
  • 存储库通过 Spring 魔法持久化同事实体
  • 连接到 DataSource、创建 ID 并在成功时返回实体的 Spring/OpenJPA 魔术
  • 在此期间查看 SQL 查询
  • Repository 接收持久化的 Entity 并返回到 Service
  • 服务将实体返回给控制器
  • 控制器将 Entity.id 返回给 REST 服务的调用者

我看到的和我没有看到但希望看到的:

  • 控制器按预期工作
  • 服务记录正确的用户信息
  • @PrePersist 创建一个日期对象(记录器)
  • 关于 openjpa 序列查询的 SQL 语句日志
  • 缺失:没有关于持久化我的实体的 SQL INSERT 语句
  • 存储库将新同事返回到服务
  • 服务使用creationDate和id(例如1)记录应该是持久化的实体
  • 控制器将 id 返回给 REST 的调用者(例如 1)
  • REST 和 SQL 查询都不显示数据库中的任何同事 :(

在尝试了通过 google、SO 和大量教程找到的所有不同类型的故障排除后,我的配置文件变得非常混乱。在花了几天时间试图找出问题所在之后,我现在来到这里,希望您能指出我的错误。

同事.java

@Entity
public class Coworker implements Serializable {

    @Transient
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long coworkerId;
    private String firstName;
    private String lastName;
    @Temporal(TemporalType.DATE)
    private Date creationDate;
    @Temporal(TemporalType.DATE)
    private Date lastUpdatedDate;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "coworker", fetch = FetchType.EAGER)
    private Set<Timeframe> timeframes = new HashSet<Timeframe>();

    @PrePersist
    private void setDateBeforePersisting() {
        logger.info("Inside prepersisting a Coworker");
        creationDate = new Date();
        // Set the last updated Date to the creation date
        lastUpdatedDate = (Date) creationDate.clone();
    }
    // ...snip.. Getters, Setters, equals, hashcode created by IDEA here
}

CoworkerController.java

@Controller
@RequestMapping(value = "coworker")
public class CoworkerController {
    @Autowired
    CoworkerService service;
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Long create(@RequestBody Coworker coworker) {
        logger.info("Post to create Coworker.");
        if (coworker == null) {
            logger.info("Coworker == null");
        } else {
            logger.info("Coworker.firstName: " + coworker.getFirstName() + " Coworker.lastName: " + coworker.getLastName());
        }
        final Coworker coworker1 = service.addNewCoworker(coworker);
        logger.info("Persisted coworker, id: " + coworker1.getCoworkerId());
        return coworker1.getCoworkerId();
    }
}

CoworkerServiceImpl.java

@Service
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public class CoworkerServiceImpl implements CoworkerService  {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Coworker addNewCoworker(Coworker coworker) {
        logger.info("Service saving coworker: " + coworker.getFirstName() + " " + coworker.getLastName());
        final Coworker save = repo.save(coworker);
        logger.info("Service saved coworker, is it != null? " + (save != null));
        logger.info("First name: " + save.getFirstName() + " Last name: " + save.getLastName() + " Date created: " + save.getCreationDate() + " id: " + save.getCoworkerId());
        return save;
    }

}

CoworkerRepository.java

public interface CoworkerRepository extends CrudRepository<Coworker, Long> {
    // Several queries here, but nothing that should interfer with
    // CrudRepository.save(S)
}

持久性.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="timeregistration" transaction-type="RESOURCE_LOCAL">
        <non-jta-data-source>java:/comp/env/jdbc/timeregistration</non-jta-data-source>
        <class>org.xezz.timeregistration.model.Coworker</class>
        <class>org.xezz.timeregistration.model.Customer</class>
        <class>org.xezz.timeregistration.model.Project</class>
        <class>org.xezz.timeregistration.model.Timeframe</class>
    </persistence-unit>
</persistence>

timeregistration-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:resources mapping="/static/**" location="/static/"/>
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven />
    <jpa:repositories base-package="org.xezz.timeregistration.repositories"/>
    <context:component-scan base-package="org.xezz.timeregistration" />
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="persistenceUnitName" value="timeregistration"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
                <property name="generateDdl" value="true"/>
                <property name="database" value="POSTGRESQL"/>
                <property name="showSql" value="true"/>
            </bean>
        </property>
    </bean>
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:/comp/env/jdbc/timeregistration"/>
        <property name="resourceRef" value="true" />
    </bean>
    <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="mediaTypes">
            <map>
                <entry key="html" value="text/html"/>
                <entry key="json" value="application/json"/>
            </map>
        </property>
        <property name="viewResolvers">
            <list>
                <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                    <property name="prefix" value="/WEB-INF/jsp/"/>
                    <property name="suffix" value=".jsp"/>
                </bean>
            </list>
        </property>
        <property name="defaultViews">
            <list>
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                    <property name="prefixJson" value="true"/>
                </bean>
            </list>
        </property>
    </bean>
</beans>

控制台日志

Mai 09, 2013 11:16:03 PM org.xezz.timeregistration.controller.CoworkerController create
Information: Post to create Coworker.
Mai 09, 2013 11:16:03 PM org.xezz.timeregistration.controller.CoworkerController create
Information: Coworker.firstName: Bastian Coworker.lastName: Koch
Mai 09, 2013 11:16:03 PM org.xezz.timeregistration.services.impl.CoworkerServiceImpl addNewCoworker
Information: Service saving coworker: Bastian Koch
Mai 09, 2013 11:16:03 PM org.xezz.timeregistration.model.Coworker setDateBeforePersisting
Information: Inside prepersisting a Coworker
Mai 09, 2013 11:16:03 PM org.xezz.timeregistration.services.impl.CoworkerServiceImpl addNewCoworker
Information: Service saved coworker, is it != null? true
59370  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - <t 1732295982, conn 850080749> executing prepstmnt 553961214 SELECT SEQUENCE_VALUE FROM OPENJPA_SEQUENCE_TABLE WHERE ID = ? FOR UPDATE [params=?]
59421  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - <t 1732295982, conn 850080749> [50 ms] spent
59423  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - <t 1732295982, conn 683618612> executing prepstmnt 540500434 INSERT INTO OPENJPA_SEQUENCE_TABLE (ID, SEQUENCE_VALUE) VALUES (?, ?) [params=?, ?]
59474  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - <t 1732295982, conn 683618612> [50 ms] spent
59476  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - <t 1732295982, conn 334967428> executing prepstmnt 512431903 SELECT SEQUENCE_VALUE FROM OPENJPA_SEQUENCE_TABLE WHERE ID = ? FOR UPDATE [params=?]
59479  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - <t 1732295982, conn 334967428> [3 ms] spent
59480  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - <t 1732295982, conn 334967428> executing prepstmnt 1033569251 UPDATE OPENJPA_SEQUENCE_TABLE SET SEQUENCE_VALUE = ? WHERE ID = ? AND SEQUENCE_VALUE = ? [params=?, ?, ?]
59493  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - <t 1732295982, conn 334967428> [12 ms] spent
Mai 09, 2013 11:16:04 PM org.xezz.timeregistration.services.impl.CoworkerServiceImpl addNewCoworker
Information: First name: Bastian Last name: Koch Date created: Thu May 09 23:16:03 CEST 2013 id: 1
Mai 09, 2013 11:16:04 PM org.xezz.timeregistration.controller.CoworkerController create
Information: Persisted coworker, id: 1

tomee.xml

<?xml version="1.0" encoding="UTF-8"?>
<tomee>
    <Resource id="jdbc/timeregistration" type="DataSource">
        JdbcDriver org.postgresql.Driver
        JdbcUrl jdbc:postgresql://localhost/timeregdb
        UserName timereg
        Password supersecretpassword
        jtaManaged false
        InitialSize 0
        MaxWait 50
        MaxActive 20
        MaxIdle 20
    </Resource>
</tomee>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.xezz</groupId>
    <artifactId>timeregistration</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.11</junit.version>
        <javax-persistence.version>2.0.0</javax-persistence.version>
        <javax-inject.version>1</javax-inject.version>
        <spring.version>3.2.2.RELEASE</spring.version>
        <spring-data.version>1.3.1.RELEASE</spring-data.version>
        <jackson.version>2.1.4</jackson.version>
        <jackson-mapper-asl.version>1.9.12</jackson-mapper-asl.version>
        <jaxb-api.version>2.2.7</jaxb-api.version>
        <slf4j-api.version>1.7.1</slf4j-api.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-api.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>${javax-persistence.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring-data.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>${javax-inject.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson-mapper-asl.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>${jaxb-api.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

感谢您提前提供的任何帮助,我现在浪费了很多时间试图追踪不同的错误(尤其是在休眠的东西之后,这比我敢说的要费时),所以现在我只需要问别人查看此内容并与我分享智慧,如果您需要更多信息,请不要犹豫(如果需要,我可以使用当前代码库更新 github)。

4

1 回答 1

5

I think you are using the wrong transactionManager in your spring configuration xml. Since you want to use spring with jpa, you should use the JpaTransactionManager.

For your example, I think your configuration should look as follows:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
于 2013-05-14T01:09:36.517 回答