Grails
  1. Grails
  2. GRAILS-8011

can't save domain classes using JPA entity manager

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.0-M2
    • Fix Version/s: None
    • Component/s: None
    • Labels:
    • Testcase included:
      yes

      Description

      Background

      This project is using a JPA domain model as described here:
      http://blog.springsource.com/2010/08/26/reuse-your-hibernatejpa-domain-model-with-grails/

      The JPA domain classes and the service layer are in separate JAR files that are configured as dependencies in BuildConfig.groovy. The Spring config is loaded from an XML file within the service layer JAR by adding the following to resources.groovy

      importBeans('classpath:/applicationContext-services.xml')
      

      In Grails 2.0.0M1, JPA domain classes were not being recognised as Grails domain classes. I have verified that this problem has been resolved. However, it now seems that it is not possible to persist domain classes using the JPA EntityManager.

      Run Test Case

      To run the attached application, unzip dependencies.zip and run the following from the root

      mvn clean install -DrefreshDB -DskipTests
      

      This will install the domain and service layers dependencies in your Maven repository and create an 'esd' MySQL database on localhost. Creating the database assumes that the local MySQL server runs on the default port and has a blank root password. If this isn't convenient, remove -DrefreshDB from the command above and manually create a database on localhost using the following DDL

          create database esd;
          use esd;
          create table user (
              id bigint not null auto_increment,
              email varchar(255),
              password varchar(255),
              primary key (id)
          );
      

      Then unzip and run the Grails app in jpabug2.zip

      Reproduce Bug

      Once the app starts, if you click on the SignUpController links, the following code is executed:

      class SignUpController {
      
          UserManager userManager
      
          def signUp = {
      
              User user = new User(email: 'foo@bar.com', password: 'password')
      
              if (params.gorm) {
                  user.save(failOnError: true)
                  render(text: "Number of users: ${User.count()}")
      
              } else {
                  userManager.saveOrUpdate(user)
                  render(text: "Number of users: ${User.count()}")
              }
          }
      }
      

      userManager.saveOrUpdate(user) tries to save a User using the JPA EntityManager

      @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
      public E saveOrUpdate(E entity) {
          return entityManager.merge(entity);
      }
      

      You can attempt to save a user using the GORM save() method (instead of the JPA EntityManager) by appending "?gorm=true" to the URL above. As you can see from the output, the GORM save method is successful, but the JPA EntityManager manager fails.

      Perhaps I'm mistakenly assuming that it should be possible to use the EntityManager when using a JPA domain model?

        Activity

        Hide
        Donal Murtagh added a comment -

        Just in case the explanation above is unclear, once the application has started, use the URL:
        http://localhost:8080/jpabug2/signUp/signUp to try and save a user with the EntityManager (this should fail)

        Use the URL http://localhost:8080/jpabug2/signUp/signUp?gorm=true to try and save a user with GORM (which should succeed)

        Show
        Donal Murtagh added a comment - Just in case the explanation above is unclear, once the application has started, use the URL: http://localhost:8080/jpabug2/signUp/signUp to try and save a user with the EntityManager (this should fail) Use the URL http://localhost:8080/jpabug2/signUp/signUp?gorm=true to try and save a user with GORM (which should succeed)
        Hide
        Donal Murtagh added a comment -

        After reading a bit more about the subject, it seems that if you want to use the EntityManager you must install the GORM-JPA plugin. However, judging by the example persistence.xml shown on that plugin's page, it looks like it only supports JPA 1.0, is that right?

        Show
        Donal Murtagh added a comment - After reading a bit more about the subject, it seems that if you want to use the EntityManager you must install the GORM-JPA plugin . However, judging by the example persistence.xml shown on that plugin's page, it looks like it only supports JPA 1.0, is that right?

          People

          • Assignee:
            Unassigned
            Reporter:
            Donal Murtagh
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Last Reviewed:

              Development