title: Entity not persisting via Spring(CrudRepository) on TomEE
body: <p>I have issues trying to persist an Entity to a PostgreSQL Database using Springs CrudRepository interface. I have had quite some issues setting this up with the correct values both on TomEE and adjusting the configuration of Spring itself. I tried briefly to use hibernate, but issues just got worse, so I switched back to OpenJpa that is bundled with TomEE.</p>
  
  <p>I have a bit of experience with plain JavaEE and am creating this application to learn Spring and a friend of mine needs a Spring application to fool around with deployment on a TomEE based ApplicationServer, thus those two are requirements. I might go a bit overboard with the amount of code I attach, but I rather let you see what is there so you get a good overview.</p>
  
  <p>Components I have developed and a brief description of what I'm doing, expecting and actually seeing.
  Components:</p>
  
  <ul>
  <li>CoworkerController (RESTful Spring Controller)</li>
  <li>CoworkerService (Spring Service to interact with Controller and Repository)</li>
  <li>CoworkerRepository (extends Spring CrudRepository)</li>
  <li>Coworker (Entity that is supposed to be persisted)</li>
  </ul>
  
  <p>Flow of my actions:</p>
  
  <ul>
  <li>Send a POST via curl with firstname lastname to the RESTful service</li>
  <li>Getting a Status 200 with the ID of the persisted Entity</li>
  <li>Checking PostgreSQL and RESTful service if I can query for my created Coworker</li>
  </ul>
  
  <p>What I'm expecting:</p>
  
  <ul>
  <li>Controller accepts POST and calls Service to persist the Coworker</li>
  <li>Service tells Repository to save the Coworker</li>
  <li>Repository persists Coworker Entity via Spring magic</li>
  <li>Spring/OpenJPA magic that connects to DataSource, create ID and return Entity on success</li>
  <li>See SQL queries during that happening</li>
  <li>Repository receives persisted Entity and returns to Service</li>
  <li>Service returns Entity to Controller</li>
  <li>Controller returns Entity.id to the caller of the REST service</li>
  </ul>
  
  <p>What I see and what I don't see but would expect to see:</p>
  
  <ul>
  <li>Controller works as expected</li>
  <li>Service logs correct user info</li>
  <li>@PrePersist creates a Date Object (Logger)</li>
  <li>SQL statement logs about openjpa sequence queries</li>
  <li>MISSING: No SQL INSERT statement about persisting my Entity</li>
  <li>Repository returns new Coworker to Service</li>
  <li>Service logs the supposed to be persisted Entity with creationDate and id(e.g. 1)</li>
  <li>Controller returns id to caller of REST (e.g. 1)</li>
  <li>Neither REST nor SQL Queries show any Coworker in the Database :(</li>
  </ul>
  
  <p>My configuration files have gotten quite a mess after trying all different kinds of troubleshooting found via google, SO and lots of tutorials. After spending now several days trying to figure out what the issue is, I come now here and hope you might be able to point me to my error.</p>
  
  <p><em>Coworker.java</em></p>
  
  <pre><code>@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&lt;Timeframe&gt; timeframes = new HashSet&lt;Timeframe&gt;();
  
      @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
  }
  </code></pre>
  
  <p><em>CoworkerController.java</em></p>
  
  <pre><code>@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();
      }
  }
  </code></pre>
  
  <p><em>CoworkerServiceImpl.java</em></p>
  
  <pre><code>@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;
      }
  
  }
  </code></pre>
  
  <p><em>CoworkerRepository.java</em></p>
  
  <pre><code>public interface CoworkerRepository extends CrudRepository&lt;Coworker, Long&gt; {
      // Several queries here, but nothing that should interfer with
      // CrudRepository.save(S)
  }
  </code></pre>
  
  <p><em>persistence.xml</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;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"&gt;
      &lt;persistence-unit name="timeregistration" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;non-jta-data-source&gt;java:/comp/env/jdbc/timeregistration&lt;/non-jta-data-source&gt;
          &lt;class&gt;org.xezz.timeregistration.model.Coworker&lt;/class&gt;
          &lt;class&gt;org.xezz.timeregistration.model.Customer&lt;/class&gt;
          &lt;class&gt;org.xezz.timeregistration.model.Project&lt;/class&gt;
          &lt;class&gt;org.xezz.timeregistration.model.Timeframe&lt;/class&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><em>timeregistration-servlet.xml</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;mvc:resources mapping="/static/**" location="/static/"/&gt;
      &lt;mvc:default-servlet-handler/&gt;
      &lt;mvc:annotation-driven /&gt;
      &lt;jpa:repositories base-package="org.xezz.timeregistration.repositories"/&gt;
      &lt;context:component-scan base-package="org.xezz.timeregistration" /&gt;
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
      &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;/bean&gt;
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
          &lt;property name="persistenceUnitName" value="timeregistration"/&gt;
          &lt;property name="jpaVendorAdapter"&gt;
              &lt;bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter"&gt;
                  &lt;property name="generateDdl" value="true"/&gt;
                  &lt;property name="database" value="POSTGRESQL"/&gt;
                  &lt;property name="showSql" value="true"/&gt;
              &lt;/bean&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
          &lt;property name="jndiName" value="java:/comp/env/jdbc/timeregistration"/&gt;
          &lt;property name="resourceRef" value="true" /&gt;
      &lt;/bean&gt;
      &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt;
          &lt;property name="mediaTypes"&gt;
              &lt;map&gt;
                  &lt;entry key="html" value="text/html"/&gt;
                  &lt;entry key="json" value="application/json"/&gt;
              &lt;/map&gt;
          &lt;/property&gt;
          &lt;property name="viewResolvers"&gt;
              &lt;list&gt;
                  &lt;bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
                      &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt;
                      &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
                      &lt;property name="suffix" value=".jsp"/&gt;
                  &lt;/bean&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="defaultViews"&gt;
              &lt;list&gt;
                  &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView"&gt;
                      &lt;property name="prefixJson" value="true"/&gt;
                  &lt;/bean&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><em>Consolelog</em></p>
  
  <pre><code>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 - &lt;t 1732295982, conn 850080749&gt; 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 - &lt;t 1732295982, conn 850080749&gt; [50 ms] spent
  59423  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - &lt;t 1732295982, conn 683618612&gt; executing prepstmnt 540500434 INSERT INTO OPENJPA_SEQUENCE_TABLE (ID, SEQUENCE_VALUE) VALUES (?, ?) [params=?, ?]
  59474  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - &lt;t 1732295982, conn 683618612&gt; [50 ms] spent
  59476  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - &lt;t 1732295982, conn 334967428&gt; 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 - &lt;t 1732295982, conn 334967428&gt; [3 ms] spent
  59480  timeregistration  TRACE  [http-bio-8080-exec-1] openjpa.jdbc.SQL - &lt;t 1732295982, conn 334967428&gt; 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 - &lt;t 1732295982, conn 334967428&gt; [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
  </code></pre>
  
  <p><em>tomee.xml</em></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;tomee&gt;
      &lt;Resource id="jdbc/timeregistration" type="DataSource"&gt;
          JdbcDriver org.postgresql.Driver
          JdbcUrl jdbc:postgresql://localhost/timeregdb
          UserName timereg
          Password supersecretpassword
          jtaManaged false
          InitialSize 0
          MaxWait 50
          MaxActive 20
          MaxIdle 20
      &lt;/Resource&gt;
  &lt;/tomee&gt;
  </code></pre>
  
  <p><em>pom.xml</em></p>
  
  <pre><code>&lt;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"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;org.xezz&lt;/groupId&gt;
      &lt;artifactId&gt;timeregistration&lt;/artifactId&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;properties&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;junit.version&gt;4.11&lt;/junit.version&gt;
          &lt;javax-persistence.version&gt;2.0.0&lt;/javax-persistence.version&gt;
          &lt;javax-inject.version&gt;1&lt;/javax-inject.version&gt;
          &lt;spring.version&gt;3.2.2.RELEASE&lt;/spring.version&gt;
          &lt;spring-data.version&gt;1.3.1.RELEASE&lt;/spring-data.version&gt;
          &lt;jackson.version&gt;2.1.4&lt;/jackson.version&gt;
          &lt;jackson-mapper-asl.version&gt;1.9.12&lt;/jackson-mapper-asl.version&gt;
          &lt;jaxb-api.version&gt;2.2.7&lt;/jaxb-api.version&gt;
          &lt;slf4j-api.version&gt;1.7.1&lt;/slf4j-api.version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;${junit.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
              &lt;version&gt;${slf4j-api.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;javax.persistence&lt;/artifactId&gt;
              &lt;version&gt;${javax-persistence.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${spring.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
              &lt;version&gt;${spring-data.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.inject&lt;/groupId&gt;
              &lt;artifactId&gt;javax.inject&lt;/artifactId&gt;
              &lt;version&gt;${javax-inject.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;${jstl.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.codehaus.jackson&lt;/groupId&gt;
              &lt;artifactId&gt;jackson-mapper-asl&lt;/artifactId&gt;
              &lt;version&gt;${jackson-mapper-asl.version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
              &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
              &lt;version&gt;${jaxb-api.version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.3.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;1.6&lt;/source&gt;
                      &lt;target&gt;1.6&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>Thanks for any help in advance, I have wasted so much time right now trying to track down different errors (especially following hibernate stuff, which was more time consuming than I dare to tell), so right now I just have to ask someone else to look over this and share wisdom with me and don't hesitate if you need more information (I can update github with the current codebase if needed).</p>